1.前提工作
了解并已部署好 主从复制
2.下载MaxScale中间件
在线下载
wget https://dlm.mariadb.com/1065384/MaxScale/2.3.20/centos/7/x86_64/maxscale-2.3.20-1.centos.7.x86_64.rpm
安装
yum -y install maxscale-2.3.20-1.centos.7.x86_64.rpm
3.master主库创建用户
已实现主从复制:主库创建的用户,能同步到从库上
创建监控用户
create user 'maxmon'@'%' identified by 'maxmon';
ALTER USER 'maxmon'@'%' IDENTIFIED WITH mysql_native_password BY 'maxmon';
grant replication slave,replication client on *.* to 'maxmon'@'%';
创建路由用户
create user 'maxrou'@'%' identified by 'maxrou';
ALTER USER 'maxrou'@'%' IDENTIFIED WITH mysql_native_password BY 'maxrou';
grant all on *.* to 'maxrou'@'%';
flush privileges;
4.配置MaxScale
vim /etc/maxscale.cnf
找到 [server1] 部分,修改其中的 address 和 port,指向 master 的 IP 和端口
复制2次 [server1] 的整块儿内容,改为 [server2] 与 [server3],同样修改其中的 address 和 port,分别指向 slave1 和 slave2
[MariaDB-Monitor] 部分,修改 servers 为 server1,server2,server3,修改 user 和 password 为之前创建的监控用户的信息
注释掉[Read-Only-Service] 部分,读写由maxscale控制即可
[Read-Write Service] 部分,修改 servers 为 server1,server2,server3,修改 user 和 password 为之前创建的路由用户的信息
注释 [Read-Only-Listener] 部分,在 [MaxAdmin-Listener] 最后添加port=6603
5.启动MaxScale
创建maxscale相关目录,所属用户是mysql
mkdir /usr/local/mysql/maxscale
mkdir /usr/local/mysql/maxscale/data
mkdir /usr/local/mysql/maxscale/logs
mkdir /usr/local/mysql/maxscale/cache
再配置下
vim /etc/maxscale.cnf
logdir=/usr/local/mysql/maxscale/logs
datadir=/usr/local/mysql/maxscale/data
piddir=/usr/local/mysql/maxscale/logs
cachedir=/usr/local/mysql/maxscale/cache
启动,不能以root用户启动,这里指定mysql用户
maxscale --config=/etc/maxscale.cnf --user=mysql
netstat -ntelp
登录MaxScale管理器
maxadmin --user=admin --password=mariadb
查看服务
list servers
6.测试
创建测试用户
create user 'rtest'@'%' identified by 'rtest';
ALTER USER 'rtest'@'%' IDENTIFIED WITH mysql_native_password BY 'rtest';
grant select,insert on testdb.* to 'rtest'@'%';
flush privileges;
mysql 连接 MaxScale
mysql -urtest -prtest -h'192.168.192.128' -P4006
查看当前服务器名称
Select @@hostname;
开启事务,MaxScale切换到master
start transaction;
回滚
rollback;
写操作走主库,读操作走从库,能实现读写分离