1.下载
[root@centos7 ~]# wget https://downloads.mysql.com/archives/get/file/mysql-router-2.1.6-linux-glibc2.12-x86-64bit.tar.gz
2.安装
[root@centos7 ~]# tar -xzvf mysql-router-2.1.6-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/
[root@centos7 ~]# ln -s /usr/local/mysql-router-2.1.6-linux-glibc2.12-x86-64bit.tar.gz /usr/local/mysql-router
配置环境变量:
[root@centos7 ~]# vim /etc/profile.d/mysqlrouter.sh
添加如下内容:
export MYSQLROUTER_HOME=/usr/local/mysql-router
export PATH=$MYSQLROUTER_HOME/bin/:$PATH
3、测试安装是否成功
[root@centos7 ~]# mysqlrouter --help
4、配置mysql-router(这里为了搭建方便使用多实例方式)
[root@centos7 ~]# mkdir /usr/local/mysql-router/etc -pv
[root@centos7 ~]# vim /usr/local/mysql-router/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.56.142:3308, 192.168.56.142:3311
# 一个用于复杂均衡的标签
[routing:balancing]
bind_address = 0.0.0.0
bind_port = 7002
max_connections = 1024
# 用于负载均衡的只读模式
mode = read-only
# 这里的两个socket是轮询用的
destinations = 192.168.56.142:3309, 192.168.56.142:3310
5.创建相关目录
[root@centos7 ~]# mkdir -pv /var/log/mysqlrouter
6.启动
[root@centos7 ~]# mysqlrouter --config /usr/local/mysqlrouter/etc/mysqlrouter.cnf &
[root@centos7 mysql-router]# netstat -tnlp | grep 700*
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 18693/mysqlrouter
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 18693/mysqlrouter
7.测试
[root@centos7 ~]# mysql -u root -h 192.168.56.142 -p -P 7001
Enter password:
mysql> select @@port;
+--------+
| @@port |
+--------+
| 3308 |
+--------+
1 row in set (0.00 sec)
##从机有负载均衡的效果
[root@centos7 ~]# mysql -h 192.168.56.142 -u root -pxsy123 -e "select @@port" -P 7002
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| @@port |
+--------+
| 3309 |
+--------+
[root@centos7 ~]# mysql -h 192.168.56.142 -u root -pxsy123 -e "select @@port" -P 7002
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| @@port |
+--------+
| 3310 |
+--------+
8.备注
按照配置,读写操作在master,读操作轮询到slave1和slave2
如果master宕机,master则不能写入,master服务重启后,需要也重启下MySQLrouter