一、MaxScale基本介绍
MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换,对多个从服务器能实现负载均衡。
二、MaxScale实验环境
中间件 | 192.168.121.51 | MaxScale 22.08.4 |
---|---|---|
主服务器 | 192.168.121.11 | mysql 8.0.30 |
从服务器 | 192.168.121.12 | mysql 8.0.30 |
从服务器 | 192.168.121.13 | mysql 8.0.30 |
三、实现数据库主从复制
(1)主库配置
[root@openEuler-1 ~]# tail -n4 /etc/my.cnf
server_id=11
gtid_mode=ON
enforce-gtid-consistency=ON
read_only=0
[root@openEuler-1 ~]# systemctl restart mysqld
# 创建用户
create user 'rep'@'192.168.121.%' identified with mysql_native_password by '123456';
# 用户授权
grant replication slave on *.* to 'rep'@'192.168.121.%';
(2)从库配置()
[root@openEuler-2 ~]# tail -n4 /etc/my.cnf
server_id=12
gtid_mode=ON
enforce-gtid-consistency=ON
read_only=1
[root@openEuler-2 ~]# systemctl restart mysqld
# 配置主从同步
CHANGE MASTER TO
MASTER_HOST = '192.168.121.11',
MASTER_USER = 'rep',
MASTER_PASSWORD = '123456',
MASTER_AUTO_POSITION = 1;
# 启动主从同步
start slave
四、创建用户
在开始配置之前,需要在master中为 MaxScale 创建两个用户,用于监控模块和路由模块,已经实现主从复制的前提下,主库创建的用户,能同步到从库上
(1)创建监控用户
# 创建用户
mysql> create user 'maxscale_monitor'@'192.168.121.%' identified with mysql_native_password by 'Admin@123456';
# 授权用户
mysql> grant replication slave, replication client on *.* to maxscale_monitor@'192.168.121.%';
(2)创建路由用户
# 创建用户
mysql> create user 'maxscale_route'@'192.168.121.%' identified with mysql_native_password by 'Admin@123456';
# 授权用户
mysql> GRANT SHOW DATABASES ON *.* TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.user TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.db TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.tables_priv TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.columns_priv TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.proxies_priv TO maxscale_route@'192.168.121.%';
五、安装MaxScale
在 Download MariaDB Products & Tools | MariaDB 选择对应系统合适的版本下载安装,可供参考。
# 配置存储库源
[root@localhost ~]# curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash
# 安装maxscale
[root@localhost ~]# dnf install maxscale -y
六、配置MaxScale
在/etc/maxscale.cnf.d新建一个配置my.cnf或者直接修改/etc/maxscale.cnf文件
[maxscale]
# 开启线程个数,默认为1.设置为auto会同cpu核数相同
threads=auto
# 定义三台主机
[dbserv1]
type=server
address=192.168.121.11
port=3306
protocol=MariaDBBackend
[dbserv2]
type=server
address=192.168.121.12
port=3306
protocol=MariaDBBackend
[dbserv3]
type=server
address=192.168.121.13
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服务
# 启动服务
[root@localhost ~]# maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale
# 查看运行状态
[root@localhost ~]# maxctrl list servers
┌─────────┬────────────────┬──────┬─────────────┬─────────────────┬──────┬───────────────┐
│ Server │ Address │ Port │ Connections │ State │ GTID │ Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv1 │ 192.168.121.11 │ 3306 │ 0 │ Master, Running │ │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv2 │ 192.168.121.12 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv3 │ 192.168.121.13 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
└─────────┴────────────────┴──────┴─────────────┴─────────────────┴──────┴───────────────┘
# 查看注册服务
[root@localhost ~]# maxctrl list services
┌────────────────────┬────────────────┬─────────────┬───────────────────┬───────────────────────────┐
│ Service │ Router │ Connections │ Total Connections │ Targets │
├────────────────────┼────────────────┼─────────────┼───────────────────┼───────────────────────────┤
│ Read-Write-Service │ readwritesplit │ 0 │ 0 │ dbserv1, dbserv2, dbserv3 │
└────────────────────┴────────────────┴─────────────┴───────────────────┴───────────────────────────┘
# 查看服务监听状态信息
[root@localhost ~]# 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中创建一个访问用户,已经实现主从复制的前提下,主库创建的用户,能同步到从库上
mysql> create user 'admin_user'@'%' identified with mysql_native_password by 'Admin@123456';
mysql> grant all privileges on *.* to 'admin_user'@'%' with grant option;
(2)测试读写分离
[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "select @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 13 |
+-------------+
[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "select @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 12 |
+-------------+
[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "begin;select @@server_id commit;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| commit |
+--------+
| 11 |
+--------+
(3)也可以在从库中 slave(192.168.121.12) 新增一条数据,登录主库 master(192.168.121.11) 进行查询如果查询不到,在中间件连接库中 maxscale(192.168.121.51) 可以查询到则成功 ,但是不建议这样测试,会破坏主从同步。
九、配置脚本启动maxscale
[root@localhost ~]# ps -ef | grep maxscale
maxscale 10918 1 0 11:59 ? 00:00:09 maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale
root 10964 1681 0 12:42 pts/0 00:00:00 grep --color=auto maxscale
# 关闭进程,停止服务
[root@localhost ~]# killall -9 maxscale
# 拷贝配置文件为主配置文件
[root@localhost ~]# cp /etc/maxscale.cnf.d/my.cnf /etc/maxscale.cnf
# 原来的重命名,防止出现报错
[root@localhost ~]# mv /etc/maxscale.cnf.d/my.cnf{,.bak}
# 启动服务
[root@localhost ~]# systemctl enable --now maxscale
十、开启maxscale GUI
(1)在maxscale配置文件中[maxscale]下添加两行配置
[root@localhost ~]# vim /etc/maxscale.cnf
[root@localhost ~]# head -n4 /etc/maxscale.cnf
[maxscale]
threads=auto
admin_host=0.0.0.0
admin_secure_gui=false
[root@localhost ~]# systemctl restart maxscale
(2)访问 http://192.168.121.51:8989(默认账户密码如下)