MySQL Router是一个轻量级的中间件,提供了应用程序与后端数据库的透明路由,是mysql用来实现负载均衡和高可用功能。同时router也提供了使用fabric 高可用的方式。
我们先去mysql官网下载mysql router
配置mysql router
tar -zxvf mysql-router-8.0.11-el6-x86-64bit.tar.gz mv mysql-router-8.0.11-el6-x86-64bit /usr/local/mysql-router
配置日志目录和配置文件目录
cd /usr/local/mysql-router mkdir -p logs mkdir -p etc
复制配置模版
cp share/doc/mysqlrouter/sample_mysqlrouter.ini ./etc/mysqlrouter.ini
配置项内容如下:
[DEFAULT] logging_folder = /usr/local/mysql-router/logs plugin_folder = /usr/local/mysql-router/lib/mysqlrouter config_folder = /usr/local/mysql-router/etc runtime_folder = /usr/local/mysql-router/run [logger] level = INFO [routing:read_write] bind_address = 192.168.128.140 bind_port = 7001 mode = read-write destinations = 192.168.128.131:3306 #读写库,多个读写库用逗号隔开 max_connections = 65535 max_connect_errors = 100 client_connect_timeout = 9 [routing:read_only] bind_address = 192.168.128.140 bind_port = 7002 mode = read-only destinations = 192.168.128.132:3306,192.168.128.133:3306 #读库,多个只读库用逗号隔开 max_connections = 65535 max_connect_errors = 100 client_connect_timeout = 9 [keepalive] interval = 60
启动mysql router
cd /usr/local/mysql-router/bin ./mysqlrouter -c /usr/local/mysqlrouter/etc/mysqlrouter.ini &
注:参数说明
bind_address 指定mysql router绑定的服务器 bind_port 指定绑定的端口 destinations 指定后端mysql server 列表 (ip:port 格式,使用逗号分隔) mode 读写模式(read-write, read_only)
还有一点需要注意,要实现同时现在mysql的读写分离和负载均衡需要在框架上进行拆分读写操作,因为mysql router路由实现读写分离只有需要配置两条路由,一条用来读写,一条用来只读的负载均衡,这样会有两个监听的连接端口,一般框架都自带读写分离模式,分别对应两个开放的监听端口来各自操作,单纯的实现读写分离还是建议使用mysql-proxy实现。
mysql-proxy的配置请查看这里:https://sulao.cn/post/340.html