目录
使用 maxctrl list servers 命令查看运行状态
使用 maxctrl list listeners Read-Write-Service 命令查看服务监听状态信息
MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换,对多个从服务器能实现负载均衡。
中间件 | 192.168.20.150 | MaxScale 22.08.4 |
---|---|---|
主服务器 | 192.168.20.149 | mysql 8.0.30 |
从服务器 | 192.168.20.148 | mysql 8.0.30 |
从服务器 | 192.168.150.146 | mysql 8.0.30 |
分别在三台服务器的/etc/my.cnf.d/mysql-server.cnf添加:
- server_id=149 / 148 /146 (这个不要相同,下面两个可以相同)
- gtid_mode=ON
- enforce-gtid-consistency=ON
两个从服务器的/etc/my.cnf.d/mysql-server.cnf添加
read_only=1
在主服务器添加用户
- create user 'rep'@'192.168.20.%' identified with mysql_native_password by '123456';
- --用户授权 “同步复制”、“同步复制状态”
- grant replication slave on *.* to 'rep'@'192.168.150.%';
- --配置主从同步
- CHANGE MASTER TO
- MASTER_HOST = '192.168.20.149',
- MASTER_USER = 'rep',
- MASTER_PASSWORD = '123456',
- MASTER_AUTO_POSITION = 1;
-
- --启动主从同步
- start slave
双yes就是成功(两个从服务器都要求)
需要在
master
中为 MaxScale 创建两个用户,用于监控模块和路由模块,已经实现主从复制的前提下,主库创建的用户,能同步到从库上
- --创建用户
- create user 'maxscale_monitor'@'192.168.20.%' identified with mysql_native_password by 'Admin@123456';
- --用户授权 “同步复制”、“同步复制状态”
- grant replication slave, replication client on *.* to maxscale_monitor@'192.168.20.%';
- --创建用户
- create user 'maxscale_route'@'192.168.20.%' identified with mysql_native_password by 'Admin@123456';
- --用户授权
- GRANT SHOW DATABASES ON *.* TO maxscale_route@'192.168.20.%';
- GRANT SELECT ON mysql.user TO maxscale_route@'192.168.20.%';
- GRANT SELECT ON mysql.db TO maxscale_route@'192.168.20.%';
- GRANT SELECT ON mysql.tables_priv TO maxscale_route@'192.168.20.%';
- GRANT SELECT ON mysql.columns_priv TO maxscale_route@'192.168.20.%';
- GRANT SELECT ON mysql.proxies_priv TO maxscale_route@'192.168.20.%';
先安装docker,然后启动容器实例
- [root@openEuler-node3 ~]# yum install docker -y
- [root@openEuler-node3 ~]# systemctl enable --now docker
- [root@openEuler-node3 ~]# docker run -d --name mxs -p 8989:8989 -p 3306:3306 -v /data/maxscale/maxscale.cnf:/etc/maxscale.cnf.d mariadb/maxscale:latest
- Unable to find image 'mariadb/maxscale:latest' locally
- latest: Pulling from mariadb/maxscale
- 7ecefaa6bd84: Pull complete
- ca0e00deb174: Pull complete
- a477bc0ba89b: Pull complete
- 1197a5109a4f: Pull complete
- 07a9436e0cbc: Pull complete
- ea7a1629d291: Pull complete
- 22d7399fd0e3: Pull complete
- 9de24e307d1e: Pull complete
- 4b69deb551d5: Pull complete
- 099ea1430764: Pull complete
- ebc519f0dc79: Pull complete
- Digest: sha256:5c658141992f1b83580759137bbe1b881ebe73e07c85819e46a8a14f332ff690
- Status: Downloaded newer image for mariadb/maxscale:latest
- 832d4689e6c5f9333d089ef2fa39a6c498b10d8da7a1c414e45cb4e0db8bf9ba
- [root@openEuler-node3 ~]#
- [root@openEuler-node3 ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 832d4689e6c5 mariadb/maxscale:latest "/usr/bin/tini -- do…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 0.0.0.0:8989->8989/tcp mxs
- [root@openEuler-node3 ~]# docker exec -it mxs bash
- [root@832d4689e6c5 /]# maxscale -version
- MaxScale 23.08.4
我们看到版本信息
在,/etc/maxscale.cnf.d新建一个配置my.cnf或者直接修改/etc/maxscale.cnf文件
- [root@832d4689e6c5 /]# cat /etc/maxscale.cnf.d/my.cnf
- [maxscale]
- threads=auto
-
- [dbserv1]
- type=server
- address=192.168.20.149
- port=3306
- protocol=MariaDBBackend
-
- [dbserv2]
- type=server
- address=192.168.20.148
- port=3306
- protocol=MariaDBBackend
-
- [dbserv3]
- type=server
- address=192.168.20.146
- port=3306
- protocol=MariaDBBackend
-
- [MySQL-Monitor]
- type=monitor
- module=mariadbmon
- servers=dbserv1, dbserv2, dbserv3
- user=maxscale_monitor
- password=Admin@123456
- monitor_interval=2s
-
- [Read-Write-Service]
- type=service
- router=readwritesplit
- servers=dbserv1,dbserv2,dbserv3
- user=maxscale_route
- password=Admin@123456
- enable_root_user=true
-
- [Read-Write-Listener]
- type=listener
- service=Read-Write-Service
- protocol=MariaDBClient
- address=0.0.0.0
- port=3306
配置完成后重启docker、MaxScale服务:
[root@4e2804021a7d /]# maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale
到这里服务已经完成了,测试内容我有时间分享,思路可以是:
在master主机授权测试用户
客户端client访问读写分离服务器
master主机验证查看数据
slave主机验证查看数据
slave主机插入数据,主服务器不会同步;
使用client客户端访问验证,能看到slave插入的数据,代表查询操作是slave主机提供服务;