MaxScale 是干什么的?
MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换,对多个从服务器能实现负载均衡。
MaxScale 实验环境
中间件 | 192.168.142.139 | MaxScale 22.08.4 |
---|---|---|
主服务器 | 192.168.142.146 | mysql 8.0.30 |
从服务器 | 192.168.142.147 | mysql 8.0.30 |
从服务器 | 192.168.142.148 | mysql 8.0.30 |
实现数据库主从复制
bash
vim /etc/hosts
192.168.142.139 proxy
192.168.142.146 master
192.168.142.147 slave1
192.168.142.148 slave2
主从配置
bash
# 主库配置
# tail -3 /etc/my.cnf.d/mysql-server.cnf
[root@master my.cnf.d]# vim mysql-server.cnf
server_id=1
gtid_mode=ON
enforce-gtid-consistency=ON
# systemctl restart mysqld
--创建用户
create user 'rep'@'192.168.142.%' identified with mysql_native_password by '123456';
--用户授权 "同步复制"、"同步复制状态"
grant replication slave on *.* to 'rep'@'192.168.142.%';
--从库配置
# tail -3 /etc/my.cnf.d/mysql-server.cnf
[root@master2 ~]# vim /etc/my.cnf.d/mysql-server.cnf
server_id=2/3
gtid_mode=ON
enforce-gtid-consistency=ON
read_only=ON
# systemctl restart mysqld
--配置主从同步
CHANGE MASTER TO
MASTER_HOST = '192.168.142.146',
MASTER_USER = 'rep',
MASTER_PASSWORD = '123456',
MASTER_AUTO_POSITION = 1;
--启动主从同步
start slave
--检查
# mysql -e 'show slave status \G' | grep -E -w "Slave_IO_Running|Slave_SQL_Running"
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
创建用户
在开始配置之前,需要在 master
中为 MaxScale 创建两个用户,用于监控模块和路由模块,已经实现主从复制的前提下,主库创建的用户,能同步到从库上
创建监控用户
bash
--创建用户
create user 'maxscale_monitor'@'192.168.142.%' identified with mysql_native_password by 'Admin@123456';
--用户授权 "同步复制"、"同步复制状态"
grant replication slave, replication client on *.* to maxscale_monitor@'192.168.142.%';
创建路由用户
bash
--创建用户
create user 'maxscale_route'@'192.168.142.%' identified with mysql_native_password by 'Admin@123456';
--用户授权
GRANT SHOW DATABASES ON *.* TO maxscale_route@'192.168.142.%';
GRANT SELECT ON mysql.user TO maxscale_route@'192.168.142.%';
GRANT SELECT ON mysql.db TO maxscale_route@'192.168.142.%';
GRANT SELECT ON mysql.tables_priv TO maxscale_route@'192.168.142.%';
GRANT SELECT ON mysql.columns_priv TO maxscale_route@'192.168.142.%';
GRANT SELECT ON mysql.proxies_priv TO maxscale_route@'192.168.142.%';
安装MaxScale
在 MariaDB Products & Tools Downloads | MariaDB 选择对应系统合适的版本下载安装,我这里使用docker 安装(安装dokcer请看最全面的Docker安装部署,配置镜像加速-CSDN博客)
bash
# yum install docker -y
# systemctl enable --now docker
# 管理后台端口8989,3306中间件连接端口
docker run -d --name mxs -p 8989:8989 -p 3306:3306 -v /data/maxscale/maxscale.cnf:/etc/maxscale.cnf.d mariadb/maxscale:latest
# docker save mariadb/maxscale:latest -o maxscale_latest.tar
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e2804021a7d mariadb/maxscale:latest "/usr/bin/tini -- do..." 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 0.0.0.0:8989->8989/tcp mxs
# 登录容器
docker exec -it mxs bash
# 查看版本信息
[root@4e2804021a7d /]# maxscale -version
MaxScale 23.08.3
# 查看版本日志
cat /var/log/maxscale/maxscale.log
配置maxscale
在,/etc/maxscale.cnf.d新建一个配置my.cnf或者直接修改/etc/maxscale.cnf文件
bash
[root@4e2804021a7d /]# cat /etc/maxscale.cnf.d/my.cnf
[maxscale]
threads=auto
[dbserv1]
type=server
address=192.168.142.146
port=3306
protocol=MariaDBBackend
[dbserv2]
type=server
address=192.168.142.147
port=3306
protocol=MariaDBBackend
[dbserv3]
type=server
address=192.168.142.148
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
配置完成后 重启MaxScale服务,进入容器中 可:
bash
[root@4e2804021a7d /]# maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale
使用 maxctrl list servers 命令查看运行状态
bash
[root@4e2804021a7d /]# maxctrl list servers
┌─────────┬────────────────┬──────┬─────────────┬─────────────────┬──────┬───────────────┐
│ Server │ Address │ Port │ Connections │ State │ GTID │ Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv1 │ 192.168.142.146 │ 3306 │ 0 │ Master, Running │ │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼──────────────┤
│ dbserv2 │ 192.168.142.147 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv3 │ 192.168.142.148 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
使用 maxctrl list listeners Read-Write-Service 命令查看服务监听状态信息
bash
[root@4e2804021a7d /]# maxctrl list listeners Read-Write-Service
┌─────────────────────┬──────┬─────────┬─────────┬────────────────────┐
│ Name │ Port │ Host │ State │ Service │
├─────────────────────┼──────┼─────────┼─────────┼────────────────────┤
│ Read-Write-Listener │ 3306 │ 0.0.0.0 │ Running │ Read-Write-Service │
└─────────────────────┴──────┴─────────┴─────────┴────────────────────┘
客户端连接测试
1)在 master
中创建一个访问用,已经实现主从复制的前提下,主库创建的用户,能同步到从库上
bash
-- 创建用户
create user 'admin_user'@'%' identified with mysql_native_password by 'Admin@123456'; --msql 8.0.x 用户认证的方式需要修改为 mysql_native_password
--用户授权
grant all privileges on *.* to 'admin_user'@'%' with grant option;
2)可以在从库中 slave(192.168.142.148) 新增一条数据,登录主库 master(192.168.142.146) 进行查询如果查询不到,在中间件连接库中 maxscale(192.168.142.139) 可以查询到则成功