Centos7部署MySQL-router实现读写分离及从库负载均衡

简介

MySQL Router是处于应用client和dbserver之间的轻量级代理程序,它能检测,分析和转发查询到后端数据库实例,并把结果返回给client,是mysql-proxy的一个替代品
Router可以实现读写分离,程序不是直接连接数据库IP,而是固定连接到mysql router,MySQL Router对前端应用是透明的,应用程序把MySQL Router当作是普通的mysql实例,把查询发给MySQL Router,而MySQL Router会把查询结果返回给前端的应用程序
当从数据库服务器故障时,业务也可以正常运行,MySQL Router可实现自动下线不可用服务器,程序配置不需要任何修改
若主数据库发生故障,由MySQL Router来决定主从自动切换,业务同样可以正常访问,程序配置不需要做任何修改
读写分离(Read/Write Splitting)
让一部分数据库服务器处理事务性增、改、删操作(INSERT、UPDATE、DELETE),另一部分数据库服务器处理SELECT查询操作

前期准备

准备四台Centos7虚拟机,配置IP地址和hostname,关闭selinux和防火墙,同步系统时间,修改IP地址和hostname映射

ip hostname 部署服务
192.168.29.132 master mysql
192.168.29.138 bak1 mysql
192.168.29.131 bak2 mysql
192.168.29.133 mid mysql mysql-router

四台虚拟机部署mysql服务

[root@master ~]# yum install mysql-server mysql -y
[root@bak1~]# yum install mysql-server mysql -y
[root@bak2~]# yum install mysql-server mysql -y
[root@mid ~]# yum install mysql-server mysql -y

mid结点部署mysql-router
从官网下载rpm包

[root@mid ~]# yum localinstall mysql-router-community-8.0.21-1.el7.x86_64.rpm -y

master结点和两个bak结点配置主从复制
详细配置步骤可参考:https://blog.51cto.com/14832653/2500735
启动MySQL服务

[root@master ~]# systemctl start mysqld
[root@bak1~]# systemctl start mysqld
[root@bak2~]# systemctl start mysqld
[root@mid ~]# systemctl start mysqld

配置mysql-router

[root@mid ~]# vi /etc/mysqlrouter/mysqlrouter.conf
#添加高可用标签,配置master结点为写结点
[routing:failover]
bind_address = 0.0.0.0
bind_port = 7001
max_connections = 1024
mode = read-write
#实际MySQL地址
destinations = 192.168.29.132:3306

#轮询标签,从库进行负载均衡的轮询访问
[routing:balancing]
bind_address = 0.0.0.0
bind_port = 7002
max_connections = 1024
mode = read-only
#实际MySQL地址
destinations = 192.168.29.138:3306,192.168.29.131:3306

启动服务

[root@mid ~]# systemctl start mysqlrouter.service

测试验证

查看服务情况

[root@mid ~]# netstat -tnlp |grep mysql
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      32715/mysqlrouter   
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      32715/mysqlrouter   

测试连接主库

[root@mid ~]# mysql -u root -h 192.168.29.133 -p -P 7001
#master结点查看连接情况
mysql>select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;
+-----------+----------+
| ip        | count(*) |
+-----------+----------+
| mid       |        1 |
+-----------+----------+

测试从库负载均衡

[root@mid ~]# mysql -u root -h 192.168.29.133 -p -P 7002
#bak1结点查看情况
mysql>select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;
+-----------+----------+
| ip        | count(*) |
+-----------+----------+
| mid       |        1 |
+-----------+----------+

#再次连接
[root@mid ~]# mysql -u root -h 192.168.29.133 -p -P 7002
#bak2结点查看情况
mysql>select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;
+-----------+----------+
| ip        | count(*) |
+-----------+----------+
| mid       |        1 |
+-----------+----------+

Centos7部署MySQL-router实现读写分离及从库负载均衡

上一篇:SQL 查询语句先执行 SELECT?


下一篇:android 原生dialog对话框