1、下载
https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-2.0.4-linux-glibc2.12-x86-64bit.tar.gz
2、安装
tar -xzvf mysql-router-2.0.4-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/ ln -s /usr/local/mysql-router-2.0.4-linux-glibc2.12-x86-64bit /usr/local/mysqlrouter export PATH=/usr/local/mysqlrouter/bin/:$PATH
3、测试安装是否成功
mysqlrouter --help
4、配置mysql-router
/usr/local/mysqlrouter/etc/mysqlrouter.cnf
[DEFAULT] # 定义日志目录 logging_folder = /var/log/mysqlrouter [logger] # 定义日志等级 level = INFO # 一个高可用的标签 [routing:failover] bind_address = 0.0.0.0 bind_port = 7001 max_connections = 1024 # 目前就支持两种 : read-write 和 read-only # read-write:用于高可用,用于可读可写 # read-only:用于负载均衡,只读 mode = read-write # 实际转发的地址 # 第一个socket如果可用,就一直会使用第一个 # 如果第一个socket无法连接了,才会连接到第二个socket destinations = 192.168.192.139:3306 # 一个用于复杂均衡的标签 [routing:balancing] bind_address = 0.0.0.0 bind_port = 7002 max_connections = 1024 # 用于负载均衡的只读模式 mode = read-only # 这里的两个socket是轮询用的 destinations = 192.168.192.136:3306, 192.168.192.138:3306
5.创建相关目录
mkdir -pv /var/log/mysqlrouter
6.启动
mysqlrouter --config /usr/local/mysqlrouter/etc/mysqlrouter.cnf &
7.测试
mysql -h192.168.192.135 -uadmin -pAbc_123456 -e "select @@server_id" -P 7002
mysql -h192.168.192.135 -uadmin -pAbc_123456 -e "select @@server_id" -P 7001
8.备注
按照配置,读写操作在master,读操作轮询到slave1和slave2
如果master宕机,master则不能写入,master服务重启后,需要也重启下MySQLrouter