mysql router使用配置
參考資料:
https://www.jianshu.com/p/7fc8d77bea59
一、架構圖
介紹:
- MySQL Router是處於應用client和dbserver之間的輕量級代理程序,它能檢測,分析和轉發查詢到后端數據庫實例,並把結果返回給client。是mysql-proxy的一個替代品。
- Router實現讀寫分離,程序不是直接連接數據庫IP,而是固定連接到mysql router。MySQL Router對前端應用是透明的。應用程序把MySQL Router當作是普通的mysql實例,把查詢發給MySQL Router,而MySQL Router會把查詢結果返回給前端的應用程序。
- 從數據庫服務器故障,業務可以正常運行。由MySQL Router來進行自動下線不可用服務器。程序配置不需要任何修改。
- 主數據庫故障,由MySQL Router來決定主從自動切換,業務可以正常訪問。程序配置不需要做任何修改。
二、讀寫分離原理
MySQL Router接受前端應用程序請求后,根據不同的端口來區分讀寫,把連接讀寫端口的所有查詢發往主庫,把連接只讀端口的select查詢以輪詢方式發往多個從庫,從而實現讀寫分離的目的。讀寫返回的結果會交給MySQL Router,由MySQL Router返回給客戶端的應用程序。
三、實驗環境
編號 | 主機名 | IP | 角色 |
1 | db_shenji | 10.10.81.134 | mysql router |
2 | dba_test_001 | 10.10.50.60 | 主庫 |
3 | dba_test_002 | 10.10.117.231 | 從庫 |
四、MySQL-Router安裝配置
1、主從搭建,此處省略
2、MySQL-Router配置
2.1、下載
[root@db_shenji ~]# cd /data/ [root@db_shenji data]# wget https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-8.0.18-linux-glibc2.12-x86_64.tar.xz [root@db_shenji data]# tar -Jxvf mysql-router-8.0.18-linux-glibc2.12-x86_64.tar.xz [root@db_shenji data]# ln -s /data/mysql-router-8.0.18-linux-glibc2.12-x86_64 /usr/local/mysql-router
2.2、配置
[root@db_shenji data]# mkdir /etc/mysql-route/ [root@db_shenji data]# cp /usr/local/mysql-router/share/doc/mysqlrouter/sample_mysqlrouter.conf /etc/mysql-route/mysqlrouter.conf
修改后完整的內容
[DEFAULT] # 日志存放目錄 logging_folder = /data/log/mysql-route # 插件存放目錄 plugin_folder = /usr/local/mysql-router/lib/mysqlrouter # 配置文件存放目錄 config_folder = /etc/mysql-route # 運行目錄 runtime_folder = /var/run [logger] # 日志運行級別 level = debug # 主節點故障轉移配置 [routing:basic_failover] # 寫節點地址 bind_address=10.10.81.134 # 寫節點端口 bind_port = 7001 # 模式,讀寫 mode = read-write # 主節點地址:默認情況下第一台主數據庫為寫主庫,當第一台主數據庫DOWN機后,第二台數據庫被提升為主庫 destinations = 10.10.50.60:3306,10.10.117.231:3306 # 從節點負載均衡配置 [routing:balancing] # 綁定的IP地址 bind_address=10.10.81.134 # 監聽的端口 bind_port = 7002 # 連接超時時間 connect_timeout = 3 # 最大連接數 max_connections = 1024 # 后端服務器地址 destinations = 10.10.117.231:3306 # 模式:讀還是寫 mode = read-only [keepalive] interval = 60
創建目錄
mkdir -p /data/log/mysql-route chown root:root /data/log/mysql-route/
啟動方式
/usr/local/mysql-router/bin/mysqlrouter -c /etc/mysql-route/mysqlrouter.conf &
查看日志
[root@db_shenji ~]# tail -f /data/log/mysql-route/mysqlrouter.log 2019-12-06 14:05:40 main DEBUG [7f48cf782720] Starting all plugins. 2019-12-06 14:05:40 main DEBUG [7f48cc30e700] plugin 'keepalive:' starting 2019-12-06 14:05:40 main DEBUG [7f48cf782720] plugin 'logger:' doesn't implement start() 2019-12-06 14:05:40 keepalive INFO [7f48cc30e700] keepalive started with interval 60 2019-12-06 14:05:40 keepalive INFO [7f48cc30e700] keepalive 2019-12-06 14:05:40 main DEBUG [7f48cb90d700] plugin 'routing:balancing' starting 2019-12-06 14:05:40 main DEBUG [7f48caf0c700] plugin 'routing:basic_failover' starting 2019-12-06 14:05:40 main DEBUG [7f48cf782720] Running. 2019-12-06 14:05:40 routing INFO [7f48cb90d700] [routing:balancing] started: listening on 10.10.81.134:7002, routing strategy = round-robin 2019-12-06 14:05:40 routing INFO [7f48caf0c700] [routing:basic_failover] started: listening on 10.10.81.134:7001, routing strategy = first-available
登錄驗證
[root@db_shenji ~]# mysql -h 10.10.81.134 -uw -p'w' -P7001 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 130622 Server version: 5.6.41-84.1-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 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 slave status\G; Empty set (0.00 sec) ERROR: No query specified mysql>
[root@db_shenji ~]# mysql -h 10.10.81.134 -uw -p'w' -P7002
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.41-84.1-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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 slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.50.60
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 3512
Relay_Log_File: relay-bin.000053
Relay_Log_Pos: 354
Relay_Master_Log_File: mysql-bin.000021
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3512
Relay_Log_Space: 521
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1574144089
Master_UUID: 0501f340-0a94-11ea-ad2b-5254007dcbb3
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 0501f340-0a94-11ea-ad2b-5254007dcbb3:10-5440876
Executed_Gtid_Set: 0501f340-0a94-11ea-ad2b-5254007dcbb3:1-5440876,
137347eb-0a94-11ea-ad2b-525400dd43f8:1-531071
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>