读写分离的原理
收到SQL写请求时,交给master服务器处理
收到SQL读请求时,交给slave服务器处理
准备:
1.部署了一主一从结构的mysql服务
主服务器 192.168.4.1
从服务器 192.168.4.2
2.准备mysql代理服务器
代理服务器 192.168.4.3
3.准备客户端
客户端 192.168.4.4
1.https://blog.csdn.net/MANlidong/article/details/100565901
1.安装maxscale
可以用yum安装 或者 rpm -ivh
[root@mysql57 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm
2.修改配置
配置文件在/etc/maxscale.cnf
[root@mysql3 ~]# vim /etc/maxscale.cnf
//定义线程个数
[maxscale]
threads=auto //auto是自动
//定义数据库服务器
[server1]
type=server
address=192.168.4.1 //master主服务器ip地址
port=3306
protocol=MySQLBackend
//定义数据库服务器
[server2]
type=server
address=192.168.4.2 //slave从服务器ip地址
port=3306
protocol=MySQLBackend
//定义要监视的数据库节点
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1, server2 //主从服务器的主机名
user=maxscalemon //监控用户
passwd=123qqq...A //监控用户的密码
monitor_interval=10000
//定义读写分离的数据库节点
[Read-Write Service]
type=service
router=readwritesplit
servers=server1, server2 //主从数据库的住主机名
user=maxscalerouter //路由用户
passwd=123qqq...A //与偶用户密码
max_slave_connections=100%
//定义管理服务 默认不用修改
[MaxAdmin Service]
type=service
router=cli
//定义读写分离服务端口号 默认不用修改
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
//定义管理服务端口号
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4016 //添加一个没有服务使用的端口
3.在主服务器mysql1上授权监控用户(跟配置文件里一样),路由用户(跟配置文件里一样)
mysql> grant replication slave, replication client on *.* to maxscalemon@"%" identified by "123qqq...A";
Query OK, 0 rows affected, 1 warning (0.13 sec)
mysql> grant select on mysql.* to maxscalerouter@"%" identified by "123qqq...A";
Query OK, 0 rows affected, 1 warning (0.13 sec)
4.启动maxscale服务
命令格式:maxscale -f /etc/maxscale.cnf
[root@mysql3 ~]# maxscale -f /etc/maxscale.cnf
5.查看是否启动成功
(1)首先要有端口号
(2)日志中notice成功,error是失败
[root@mysql3 ~]# ss -utnlp | grep 4006
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=2186,fd=11))
[root@mysql3 ~]# ss -utnlp | grep 4016
tcp LISTEN 0 128 :::4016 :::* users:(("maxscale",pid=2186,fd=12))
[root@mysql3 ~]# cat /var/log/maxscale/maxscale.log
MariaDB MaxScale /var/log/maxscale/maxscale.log Fri Sep 6 10:55:55 2019
----------------------------------------------------------------------------
2019-09-06 10:55:55 notice : Working directory: /var/log/maxscale
mysql1跟mysql2数据不一样
mysql2从服务器写入数据mysql1主服务器不会有
mysql> create database db7;
Query OK, 1 row affected (0.13 sec)
mysql>
mysql>
mysql> create table db7.a(
-> id int);
Query OK, 0 rows affected (0.53 sec)
mysql> insert into db7.a values(1);
Query OK, 1 row affected (0.19 sec)
mysql> select * from db7.a;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
mysql> grant select,insert on db7.* to at@"%" identified by "123qqq...A";
mysql> select * from db7.a;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> insert into db7.a values(2);
Query OK, 1 row affected (0.13 sec)
mysql> select * from db7.a;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
现在1跟2两台服务器上面数据不一样
命令格式:mysql -h访问地址 -u用户名 -P端口 -p密码
[root@mysql4 ~]# mysql -h192.168.4.3 -uat -P4006 -p123qqq...A
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db7 |
+--------------------+
2 rows in set (0.01 sec)
mysql> select * from db7.a;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)