CentOS 6.5下MySQL5.7.25的安装、主从复制和读写分离(三)
概述:本文主要讲述CentOS 6.5下MYSQL5.7.25通过中间件实现读写分离的步骤,仅供参考
主从服务器说明:
主服务器:192.168.3.101
从服务器:192.168.3.11
maxscale所在服务器:192.168.3.12
1、安装maxscale
rpm -ivh https://downloads.mariadb.com/files/MaxScale/1.3.0/rhel/6/x86_64/maxscale-1.3.0-1.rhel6.x86_64.rpm
安装完成后,输入max,按下Tab键会出现
maxadmin maxctrl maxscale maxavrocheck maxkeys maxscale_generate_support_info.py maxbinlogcheck maxpasswd
2.主服务器上创建账号
#监控账号 create user [email protected]'192.168.3.%' identified by 'scalemon'; grant replication slave,replication client on *.* to [email protected]'192.168.3.%'; #路由账号 create user [email protected]'192.168.3.%' identified by 'maxscale'; grant select on mysql.* to [email protected]'192.168.3.%';
要给密码加密,可以操作如下
#生成加密器 maxkeys #将明文密码root加密 maxpasswd /var/lib/maxscale root #结果为 AB8A76D48AD05821462EB1405E22A1A5
3、修改maxscale配置
vi /etc/maxscale
根据情况配置如下
[maxscale] threads=4 #服务器配置 [server1] type=server address=192.168.3.11 port=3306 protocol=MySQLBackend [server2] type=server address=192.168.3.101 port=3306 protocol=MySQLBackend [server3] type=server address=192.168.3.12 port=3306 protocol=MySQLBackend [MySQL Monitor] type=monitor module=mysqlmon servers=server1 user=scalemon passwd=scalemon #单位:ms monitor_interval=1000 #读写分离须去掉只读模块 #[Read-Only Service] #type=service #router=readconnroute #servers=server1 #user=myuser #passwd=mypwd #router_options=slave [Read-Write Service] type=service router=readwritesplit servers=server1,server2,server3 user=maxscale passwd=maxscale max_slave_connections=100% #集群中剔除延迟较大的从库 max_slave_replication_lag=60 [MaxAdmin Service] type=service router=cli #读写分离已经包含读负载均衡,可以去掉只读监听 #[Read-Only Listener] #type=listener #service=Read-Only Service #protocol=MySQLClient #port=4008 [Read-Write Listener] type=listener service=Read-Write Service protocol=MySQLClient port=4006 [MaxAdmin Listener] type=listener service=MaxAdmin Service protocol=maxscaled port=6603
4、启动MaxScale服务,查看运行情况
maxscale --config=/etc/maxscale.cnf maxadmin --user=admin --password=mariadb #默认用户名密码
结果如下:
5、测试
#主库上执行 mysql -h 192.168.3.12 -P 4006 -umaxscale -pmaxscale #执行查看数据库服务器名的操作来知道当前实际所在的数据库 select @@hostname; #----------------- start transaction; select @@hostname; rollback; select @@hostname;