Mysql讀寫分離-Mysql router
原理: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 buck@‘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
Mysql讀寫分離-Mysql router