MySQL router功能有讀寫分離,故障切換、負載均衡、透明代理等功能
1、搭建數據庫主從略
2、mysql_router安裝如下:
tar -xvf mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz
cp mysql-router-2.1.4-linux-glibc2.12-x86-64bit /usr/local/mysqlrouter -rf
cd /usr/local/mysqlrouter/
# 創建日誌目錄
mkdir -p /usr/local/mysqlrouter/logs
# 創建配置文件目錄
mkdir -p /usr/local/mysqlrouter/conf
touch /usr/local/mysqlrouter/conf/mysqlrouter.ini
#配置文件爲 /usr/local/mysqlrouter/conf/mysqlrouter.ini
[DEFAULT]
logging_folder = /usr/local/mysqlrouter/logs
plugin_folder = /usr/local/mysqlrouter/lib/mysqlrouter
config_folder = /usr/local/mysqlrouter/conf
runtime_folder = /usr/local/mysqlrouter/run
[logger]
#level = INFO
level = DEBUG
[routing:read_write]
bind_address = 172.16.32.102
bind_port = 7001
mode = read-write
destinations = 172.16.32.11:3307,172.16.32.10:3307
max_connections = 65535
max_connect_errors = 10000
client_connect_timeout = 9
[routing:read_only]
bind_address = 172.16.32.102
bind_port = 7002
mode = read-only
destinations = 172.16.32.10:3307,172.16.32.104:3307
max_connections = 65535
max_connect_errors = 10000
client_connect_timeout = 9
[keepalive]
interval = 60
###部分參數說明
DEFAULT]
# 定義日誌目錄
logging_folder = /usr/local/mysqlrouter/logs
[logger]
# 定義日誌等級
level = INFO
# 一個高可用的標籤
[routing:failover]
bind_address = 0.0.0.0
bind_port = 7001
max_connections = 1024
# 目前就支持兩種 : read-write 和 read-only # read-write:用於高可用,用於可讀可寫
# read-only:用於負載均衡,只讀
mode = read-write
# 實際轉發的地址
# 第一個socket如果可用,就一直會使用第一個
# 如果第一個socket無法連接了,纔會連接到第二個socket
destinations = 主庫地址:3307, 備選主庫地址:3307
# 一個用於複雜均衡的標籤
[routing:balancing]
bind_address = 0.0.0.0
bind_port = 7002
max_connections = 1024
# 用於負載均衡的只讀模式
mode = read-only
# 這裏的兩個socket是輪詢用的
destinations = 從庫地址:port,從庫地址2:port //讀負載均衡配置
######測試如下:
##服務端口檢查:netstat -tnl
看日誌:
2018-02-06 16:17:59 DEBUG [7f4fe95bb700] [routing:read_write] TCP connection from 6 accepted at 172.16.32.102:7001
2018-02-06 16:17:59 DEBUG [7f4fd37fe700] Trying server 172.16.32.11:3307 (index 0)
2018-02-06 16:17:59 DEBUG [7f4fd37fe700] [routing:read_write] source [172.16.32.102]:39043 - dest [172.16.32.11]:3307
2018-02-06 16:18:34 INFO [7f4feb5bf700] keepalive
2018-02-06 16:19:00 DEBUG [7f4fd37fe700] [routing:read_write] Routing stopped (up:481b;down:320b)
2018-02-06 16:19:01 DEBUG [7f4fe95bb700] [routing:read_write] TCP connection from 6 accepted at 172.16.32.102:7001
2018-02-06 16:19:01 DEBUG [7f4fd37fe700] Trying server 172.16.32.11:3307 (index 0)
2018-02-06 16:19:01 DEBUG [7f4fd37fe700] [routing:read_write] source [172.16.32.102]:39045 - dest [172.16.32.11]:3307
2018-02-06 16:19:34 INFO [7f4feb5bf700] keepalive
1、實現讀負載均衡
##在不同的SESSION中登陸7002訪問到不同的DB
2、從庫離線處理
##從庫自動上線離線處理
##連接切換(秒切換)
3、主庫離線與備選主庫
##實現備選主庫切換,主庫恢復後不在使用連接
##一組多從情況下,非線性級聯情況下,最後個從庫同步異常
##適合多主環境、GTID,如MGR,不適合異步複製
##重啓服務路由更新
4、併發測試
##出現服務異常,7002仍然可以訪問,重啓服務後恢復
##最大支持500併發