maxscale实现读写分离
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
master | 192.168.159.141 | rocky linux9.3 mysql-8.0.35 | 无数据 |
slave | 192.168.159.147 | rocky linux9.3 mysql-8.0.35 | 无数据 |
maxscale | 192.168.159.135 | rocky linux9.3 maxscale23.08.4-GA | |
slave2 | 192.168.159.137 | rocky linux9.3 mysql-8.0.35 |
mysql安装
分别在主从三台服务器上安装mysql-8.0.35
版本,此处略过安装步骤。
maxscale安装
去官网先下载yum源
[root@maxscale ~]# curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash
# [info] Checking for script prerequisites.
# [info] MariaDB Server version 11.2 is valid
# [info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo
# [info] Adding trusted package signing keys...
/etc/pki/rpm-gpg ~
~
# [info] Successfully added trusted package signing keys
# [info] Cleaning package cache...
25 files removed
[root@maxscale ~]# ls /etc/yum.repos.d/
mariadb.repo rocky-addons.repo rocky-devel.repo rocky-extras.repo rocky.repo
安装maxscale
[root@maxscale ~]# yum -y install maxscale
登录到主库
[root@master ~]# mysql -uroot -pcjy123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
创建maxscale用户密码是maxscale
mysql> CREATE USER 'maxscale'@'%' IDENTIFIED BY 'maxscale';
Query OK, 0 rows affected (0.00 sec)
授权maxscale可以查询所有数据库
mysql> GRANT SELECT ON mysql.* TO 'maxscale'@'%';
Query OK, 0 rows affected (0.01 sec)
授权可以看所有数据库
mysql> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
Query OK, 0 rows affected (0.00 sec)
创建admin用户可以在maxscale上登录
mysql> CREATE USER 'admin'@'192.168.159.135' IDENTIFIED BY 'admin';
Query OK, 0 rows affected (0.01 sec)
在maxscale上安装mysql
[root@maxscale ~]# yum -y install mysql
[root@maxscale ~]# which mysql
/usr/bin/mysql
登录到master
[root@maxscale ~]# mysql -uadmin -padmin -h192.168.159.141
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
因为没有权限所以只能看到两个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)
mysql> exit
Bye
回到master主库设置增删改查权限
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'admin'@'192.168.159.135';
Query OK, 0 rows affected (0.00 sec)
在master主库上授权真机有增删改查权限
mysql> CREATE USER 'admin'@'192.168.137.1' IDENTIFIED BY 'admin';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'admin'@'192.168.137.1';
Query OK, 0 rows affected (0.01 sec)
在maxscale上修改配置文件
[root@maxscale ~]# vim /etc/maxscale.cnf
先看有无这个
[maxscale]
threads=auto
修改后端服务器地址
[server1]
type=server
address=192.168.159.141
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.159.137
port=3306
protocol=MySQLBackend
[server3]
type=server
address=192.168.159.147
port=3306
protocol=MySQLBackend
配置监控
[MySQL-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
配置listener
注释掉只读
#[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=3306 (伪装成数据库端口3306)
切换到主库创建monitor用户
mysql> CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected (0.01 sec)
再添加授权
mysql> GRANT REPLICATION CLIENT on *.* to 'monitor'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE on *.* to 'monitor'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT SUPER,RELOAD on *.* to 'monitor'@'%';
Query OK, 0 rows affected, 1 warning (0.01 sec)
启动服务
[root@maxscale ~]# systemctl start maxscale
查看端口
[root@maxscale ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.1:8989 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@maxscale ~]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.1:8989 0.0.0.0:* users:(("maxscale",pid=15450,fd=19))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=792,fd=3))
LISTEN 0 4096 *:3306 *:* users:(("maxscale",pid=15450,fd=27))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=792,fd=4))
查看有哪些服务
[root@maxscale ~]# maxctrl list services
┌────────────────────┬────────────────┬─────────────┬───────────────────┬───────────────────────────┐
│ Service │ Router │ Connections │ Total Connections │ Targets │
├────────────────────┼────────────────┼─────────────┼───────────────────┼───────────────────────────┤
│ Read-Write-Service │ readwritesplit │ 0 │ 0 │ server1, server2, server3 │
└────────────────────┴────────────────┴─────────────┴───────────────────┴───────────────────────────┘
查看后台服务器有哪些
[root@maxscale ~]# maxctrl list servers
┌─────────┬─────────────────┬──────┬─────────────┬─────────────────┬──────┬───────────────┐
│ Server │ Address │ Port │ Connections │ State │ GTID │ Monitor │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server1 │ 192.168.159.141 │ 3306 │ 0 │ Master, Running │ │ MySQL-Monitor │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server2 │ 192.168.159.137 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server3 │ 192.168.159.147 │ 3306 │ 0 │ Slave, Running │ │ MySQL-Monitor │
└─────────┴─────────────────┴──────┴─────────────┴─────────────────┴──────┴───────────────┘
测试
用客户机连接maxscale
user:admin
pw:admin
会发现进行读操作时,是在slave的从数据库上执行
在进行写操作时,是在master主数据库上执行