1配置maxscale
1.先配置好主从,过程省略
实验主机:
maxscale 192.168.88.20
master 192.168.88.21
slave1 192.168.88.23
slave2 192.168.88.24
提前搭建好1主2从
2.下载maxscale源
[root@maxscale ~]# curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash
[root@maxscale ~]# yum makecache
3.安装maxscale
yum -y install mariadb-server mysql
yum -y install maxscale
4.配置maxscale配置文件(maxscale主机)
cat >/etc/maxscale.cnf<<EOF
[maxscale]
threads=auto
#配置服务器
[server1]
type=server
address=192.168.88.21
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.88.23
port=3306
protocol=MySQLBackend
[server3]
type=server
address=192.168.88.24
port=3306
protocol=MySQLBackend
#配置监视器
[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3
user=monitor
password=monitor
monitor_interval=2s
#配置服务和侦听器
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server1
#user=service_user
#password=service_pw
#router_options=slave
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale
#[Read-Only-Listener]
#type=listener
#service=Read-Only-Service
#protocol=mariadbprotocol
#port=4008
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=mariadbprotocol
port=4006
EOF
5.在主数据库为MaxScale创建用户帐户(主数据库中创建,从库自动复制)
CREATE USER 'maxscale'@'%' IDENTIFIED BY 'maxscale';
mysql> GRANT SELECT ON mysql.* TO 'maxscale'@'%';
mysql> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
mysql> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
mysql> GRANT ALL ON *.* TO 'maxscale'@'%';
6.创建监视需要的账号并授权(主数据库中创建,从库自动复制)
mysql> CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
mysql> GRANT REPLICATION CLIENT on *.* to 'monitor'@'%';
mysql> grant replication slave on *.* to 'monitor'@'%';
mysql> GRANT SUPER, RELOAD on *.* to 'monitor'@'%';
7.在maxscale机器创建maxscale
mysql进入后
CREATE USER 'maxscale'@'%' IDENTIFIED BY 'maxscale';
mysql> GRANT ALL ON *.* TO 'maxscale'@'%';
8.设置maxscale服务开机自启(maxscale主机)
systemctl enable --now maxscale
9.使用 MaxCtrl 检查 MaxScale 状态
maxctrl list services
maxctrl list servers
maxctrl list listeners;
maxctrl list sessions
maxctrl show server server1
2.验证实验是否成功
1.登录到maxscale的监听端口
mysql -h 192.168.88.20 -P 4006 -umaxscale -pmaxscale
2.创建数据库,建表,新增数据
create database game;
create table game.user(id int,name varchar(10),age int);
insert into game.user values(1,'wang',30);
insert into game.user values(1,'wang',30);
3.查看是哪个服务器执行的命令
select @@hostname
实验成功实现读写分离