Mysql router是Mysql-proxy的替代方案,现在mysql-proxy的下载已经不支持,MYSQL官方也建议下载mysql-router。
Mysql-router主要是用来对读操作进行负载均衡,读写操作进行高可用
下面我们先搭建一个example来看看它的功能,我这儿预先搭建了MHA环境
环境如下:
Mysql master:192.168.10.229
Mysql slave1:192.168.10.228
Mysql slave2:192.168.10.230
MHA NODE: 192.168.10.229,192.168.10.228,192.168.10.230,192.168.10.231
MHA MANAGER: 192.168.10.231
MYSQL ROUTER: 192.168.10.231
1、下载mysql-router
进入官方下载,下载rpm包
http://dev.mysql.com/downloads/router/
2、安装rpm包
rpm -ivh mysql-router-2.0.3-1.el6.x86_64.rpm
安装后配置文件位置:/etc/mysqlrouter/mysqlrouter.ini
3、修改配置如下
[DEFAULT]
logging_folder = /var/log/mysqlrouter/
plugin_folder = /usr/lib64/mysqlrouter
runtime_folder = /var/run/mysqlrouter
config_folder = /etc/mysqlrouter
[logger]
level = info
# If no plugin is configured which starts a service, keepalive
# will make sure MySQL Router will not immediately exit. It is
# safe to remove once Router is configured.
[keepalive]
interval = 60
[routing:read_only]
bind_address = 192.168.10.231
bind_port = 7001
destinations = 192.168.10.228:3306,192.168.10.229:3306,192.168.10.230:3306
mode = read-only
[routing:read_write]
bind_address = 192.168.10.231
bind_port = 7002
destinations = 192.168.10.229:3306,192.168.10.228:3306
mode = read-write
4、配置说明
[routing:read_only]
bind_address = 192.168.10.231
bind_port = 7001
destinations = 192.168.10.228:3306,192.168.10.229:3306,192.168.10.230:3306
mode = read-only
#该配置是配置读操作的负载均衡,会采取轮询的方式连接destinations的配置mysql server 列表
#bind_address指定mysql router绑定的服务器
#bind_port 指定绑定的端口
#destinations 指定后端mysql server 列表
#mode 指定该服务只能做read操作
[routing:read_write]
bind_address = 192.168.10.231
bind_port = 7002
destinations = 192.168.10.229:3306,192.168.10.228:3306
mode = read-write
#该配置是配置写操作的高可用,会采取轮询的方式连接destinations的配置mysql server 列表
#bind_address指定mysql router绑定的服务器
#bind_port 指定绑定的端口
#destinations 指定后端mysql server 列表,会选择第一个可用节点,除非第一个节点不可用,才会考虑第二个节点,这样就可以保证高可用
#mode 指定该服务能做read和write操作
5、启动mysql router
mysqlrouter -c /etc/mysqlrouter/mysqlrouter.ini &
6、测试负载均衡
打开两个session,登录231的7001端口,分别执行下面的语句:
mysql -u root -p123456 -h 192.168.10.231 --port 7001
我们登录192.168.10.229和192.168.10.228,查看show full processlist,分别多了一个连接,说明轮训有效:
| 169377 | root | 192.168.10.231:45427 | NULL | Sleep | 2025 | | NULL |
| 21 | root | 192.168.10.231:52171 | NULL | Sleep | 210 | | NULL |
7、测试读高可用
保证mha manger处于启动状态:
[root@yw31 app1]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:28606) is running(0:PING_OK), master:192.168.10.229
停止229的mysql:
service mysqld stop
最后发现master转移到了192.168.10.228,现在我们连接192.168.10.231:7002,发现mysql router连接到了228节点:
| 36 | root | 192.168.10.231:53030 | NULL | Sleep | 8 | | NULL |
关于mha的搭建方法,后面我会加上。