目录
MySQL集群读写分离方案B-(MySQL+GTID+MaxScale+Keepalive)之2/3:maxscale安装配置
一、前提条件
另:在所有的mysql数据库上配置maxscale(一般主库配置即可,从库)
create user 'maxscale'@'%' identified by 'maxscale123'; grant SELECT on mysql.user to 'maxscale'@'%'; GRANT SELECT ON mysql.db TO 'maxscale'@'%'; GRANT SELECT ON mysql.tables_priv TO 'maxscale'@'%'; GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%'; grant REPLICATION CLIENT on *.* to 'maxscale'@'%'; GRANT replication slave, replication client,SELECT ON *.* TO maxscale@'%'; SHOW GRANTS FOR 'maxscale'@'%';
二、Maxsale安装配置(192.168.21.166/192.168.21.167)
1、maxscale下载
(1)官网下载
mkdir -p /soft cd /soft wget https://dlm.mariadb.com/3232407/MaxScale/23.02.2/yum/centos/7/x86_64/maxscale-23.02.2-1.rhel.7.x86_64.rpm
(2)站内下载
cd /soft https://download.topunix.com/MySQL/Software-Cluster/Soft-Maxscale/maxscale-23.02.2-1.rhel.7.x86_64.rpm
(3)和内网下载
cd /soft wget http://192.168.20.3/soft-database/mysql/maxscale-23.02.2-1.rhel.7.x86_64.rpm
(4)其他版本下载
https://downloads.mariadb.com/MaxScale
2、创建用户和目录
mkdir -p /mysql/app/maxscale/{data,cache,logs,tmp,etc} mkdir -p /mysql/app/maxscale/logs/{binlog,trace} #maxkeys /mysql/app/maxscale/data #maxpasswd /mysql/app/maxscale/data maxscale123 groupadd maxscale useradd -g maxscale maxscale chown -R maxscale:maxscale /mysql/app/maxscale chmod -R 777 /mysql/app/maxscale echo "rm -rf /var/lib/maxscale" >> /etc/rc.d/rc.local echo "mkdir -p /var/lib/maxscale/maxscale.cnf.d" >> /etc/rc.d/rc.local echo "chown -R maxscale:maxscale /var/lib/maxscale" >> /etc/rc.d/rc.local
3、安装maxscale包
(1)安装依赖
mount /dev/cdrom /mnt yum install libcurl libaio openssl gnutls libatomic -y
(2)删除老的maxscale
rpm -e maxscale*
(3)安装maxsale
cd /soft yum install maxscale-23.02.2-1.rhel.7.x86_64.rpm -y
(4)查看新安装版本和位置
[root@mysqlm01 soft]# maxscale --version MaxScale 23.02.2 [root@mysqlm01 soft]# which maxscale /usr/bin/maxscale
4、配置自动启动服务
注:yum安装后会自动创建,我们可以做微调,把文件放到我们想要的地方。
(1)备份原服务
mv /usr/lib/systemd/system/maxscale.service /usr/lib/systemd/system/maxscale.service.bak
(2)配置自动启动服务
vi /usr/lib/systemd/system/maxscale.service
[Unit] Description=MariaDB MaxScale Database Proxy Description=MySQL Router After=syslog.target After=network.target [Service] Type=simple Restart=on-abort PIDFile=/mysql/app/maxscale/tmp/maxscale.pid User=maxscale Group=maxscale ExecStart=/usr/bin/maxscale -f /etc/maxscale.cnf PrivateTmp=true [Install] WantedBy=multi-user.target
(3)生效自动启动
systemctl enable maxscale systemctl daemon-reload
四、MaxScale读写分离与从库负载均衡配置(192.168.21.166/167)
注:配置手册:--https://mariadb.com/kb/en/mariadb-maxscale-2302-mariadb-maxscale-configuration-guide/#introduction
1、配置maxscale.cnf文件
(1)备份原来的配置文件
mv /etc/maxscale.cnf /etc/maxscale.cnf.bak
(2)配置maxscale.cnf
配置脚本(脚本)此脚本是老版本maxscale-2.2.11-1.rhel.7.x86_64.rpm的配置(对于当前版本maxscale-23.02.2-1.rhel.7.x86_64.rpm已经过时了。
vi /etc/maxscale.cnf
https://mariadb.com/docs/server/deploy/topologies/primary-replica/enterprise-server-10-3/config-mxs/ https://mariadb.com/docs/server/deploy/topologies/primary-replica/enterprise-server-10-4/config-mxs/ https://mariadb.com/docs/server/deploy/topologies/primary-replica/enterprise-server-10-5/config-mxs/ https://mariadb.com/docs/server/deploy/topologies/primary-replica/enterprise-server-10-6/config-mxs/
2、启动maxscale
systemctl start maxscale or: maxscale -f /etc/maxscale.cnf --user=maxscale
3、查看状态及端口
systemctl status maxscale netstat -ntlp
4、测试读写分离
(1)数据库创建表
mysql -uroot -prootroot create database testdb; use testdb create table test12(id int,name varchar(10)); insert into test12 values(1,'LLL01'); commit;
(2)测试读写分离
mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "select @@hostname"; mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "select @@hostname"; mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "delete from testdb.test12 limit 1;commit;show variables like 'server_id';"; mysql -umaxscale -pmaxscale123 -h 192.168.21.167 -P 3306 -e "delete from testdb.test12 limit 1;commit;show variables like 'server_id';";
(3)插入数据
在任意节点插入数据:注意:测试过程,show variables like 'server_id';显示其他节点的dml操作,但是实际是在主库执行的dml。
mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "insert into testdb.test12 values(12,'LLL12');commit;show variables like 'server_id';"; mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "insert into testdb.test12 values(12,'LLL12');commit;show variables like 'server_id';"; mysql -umaxscale -pmaxscale123 -h 192.168.21.166 -P 3306 -e "insert into testdb.test12 values(12,'LLL12');commit;show variables like 'server_id';";
查看所有节点数据,都是三条
查看所有节点同步信息
五、maxctrl查看配置(192.168.21.166/167)
1、查看服务器
maxctrl list servers
2、查看服务
maxctrl list services