原理:MySQL router根据端口来区分读写,把连接读写端口的所有请求发往master,连接只读端口的所有请求以轮询方式发往多个slave,从而实现读写分离
主: SQL-Master 192.168.200.101/24
从: SQL-Slave 192.168.200.102/24,192.168.200.103/24
mysql-router 192.168.200.104/24
使用rpm安装mysql-router
rpm -ivh https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-2.1.4-1.el7.x86_64.rpm
vim /etc/mysqlrouter/mysqlrouter.conf
[DEFAULT]
logging_folder = /var/log/mysqlrouter/
plugin_folder = /usr/lib64/mysqlrouter
runtime_folder = /var/run/mysqlrouter
config_folder = /etc/mysqlrouter
[logger]
level = info
[keepalive]
interval = 60
[routing:read_write]
bind_address=192.168.200.104 #监听地址
bind_port=7001 #监听端口
destinations=192.168.200.101:3306 #mysql列表
mode=read-write #指定读写
max_connections=1024
max_connect_errors=100
client_connect_timeout=9
[routing:read_only]
bind_address=192.168.200.104
bind_port=7002
destinations=192.168.200.102:3306,192.168.200.103:3306
mode=read-only #指定只读
max_connections=65535
max_connect_errors=100
client_connect_timeout=9
systemctl start mysqlrouter
systemctl enable mysqlrouter
ss -ntlu
tcp LISTEN 0 128 192.168.200.106:7001 *:*
tcp LISTEN 0 128 192.168.200.106:7002 *:*
主数据库配置:
mysql -u root -p123456
创建用户test设置密码123456
MariaDB [(none)]> grant all on *.* to [email protected]‘127.0.0.1‘ identified by "hello";
MariaDB [(none)]> use mysql;
修改host权限
MariaDB [(none)]> update user set host = ‘%‘ where user = ‘test‘;
MariaDB [mysql]> select user, host from user;
+------+---------------+
| user | host |
+------+---------------+
| test | % |
刷新数据库
MariaDB [mysql]> flush privileges;
mysql -utest -p123456 -h192.168.200.104 -P7001
原文地址:https://www.cnblogs.com/liang-yao/p/8455040.html