【MYsql】Maxscale中介軟體使用
-
規劃
192.168.6.114 mysql master 192.168.6.115
mysql salve1 192.168.6.119 mysql slave2 192.168.6.121 maxscale
搭建114 115 119的一主兩叢結構並開啟半同步複製,和搭建mha一樣 -
在搭建完主從後我們還需要在資料庫建一個用於monitor的使用者,同時建好後面測試的使用者:
mysql> grant all privileges on *.* to 'max'@'%' identified by 'ESBecs00'; ---為了省事就用這一個使用者吧
1安裝maxscale
- 下載連結:
- 本次部落格安裝的是
-
maxscale-beta-1.3.0-1.centos5.x86_64.rpm 該版本
2 修改配置檔案
- 安裝完後會在/etc/目錄下生成模板配置檔案
-
[root@localhost etc]# ls maxscale.cnf.template
maxscale.cnf.template
-
寫一個配置檔案(每個版本的配置檔案不一樣,最好是cp模板配置檔案來修改)
-
[root@localhost etc]# more maxscale.cnf
-
# MaxScale documentation on GitHub:
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Documentation-Contents.md
-
-
# Global parameters
-
#
-
# Number of threads is autodetected, uncomment for manual configuration
-
# Complete list of configuration options:
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md
-
-
[maxscale]
-
threads=8
-
-
# Server definitions
-
#
-
# Set the address of the server to the network
-
# address of a MySQL server.
-
#
-
-
[server1]
-
type=server
-
address=192.168.6.114
-
port=3306
-
protocol=MySQLBackend
-
-
[server2]
-
type=server
-
address=192.168.6.115
-
port=3306
-
protocol=MySQLBackend
-
-
[server3]
-
type=server
-
address=192.168.6.119
-
port=3306
-
protocol=MySQLBackend
-
-
# Monitor for the servers
-
#
-
# This will keep MaxScale aware of the state of the servers.
-
# MySQL Monitor documentation:
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Monitors/MySQL-Monitor.md
-
-
[MySQL Monitor]
-
type=monitor
-
module=mysqlmon
-
servers=server1,server2,server3
-
user=max
-
passwd=ESBecs00
-
monitor_interval=10000
-
-
# Service definitions
-
#
-
# Service Definition for a read-only service and
-
# a read/write splitting service.
-
#
-
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadConnRoute.md
-
-
[Read-Only Service] ###只讀服務
-
type=service
-
router=readconnroute
-
servers=server1,server2,server3
-
user=max
-
passwd=ESBecs00
-
router_options=slave
-
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md
-
-
[Read-Write Service] ####寫服務
-
type=service
-
router=readwritesplit
-
servers=server1
-
user=max
-
passwd=ESBecs00
-
max_slave_connections=100%
-
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Reference/MaxAdmin.md
-
-
[MaxAdmin Service]
-
type=service
-
router=cli
-
-
-
[Read-Only Listener]
-
type=listener
-
service=Read-Only Service
-
protocol=MySQLClient
-
port=4008 ##讀服務啟動監聽 埠4008
-
-
[Read-Write Listener]
-
type=listener
-
service=Read-Write Service
-
protocol=MySQLClient
-
port=4006 ####寫服務啟動監聽 埠
-
-
[MaxAdmin Listener]
-
type=listener
-
service=MaxAdmin Service
-
protocol=maxscaled
- port=6603 ###管理埠
-
[root@localhost etc]# more maxscale.cnf
3 啟動maxscale服務
-
[root@localhost etc]# /etc/init.d/maxscale start
Starting MaxScale: found maxscale (pid
14126) 正在執行... [確定]
-
檢視message日誌
-
[root@localhost ~]# tail -f /var/log/messages
-
Jul 29 17:04:33 localhost maxscale[14035]: Configuration file: /etc/maxscale.cnf
-
Jul 29 17:04:33 localhost maxscale[14035]: Log directory: /var/log/maxscale ------日誌目錄
-
Jul 29 17:04:33 localhost maxscale[14035]: Data directory: /var/lib/maxscale/data
-
Jul 29 17:04:33 localhost maxscale[14035]: Module directory: /usr/lib64/maxscale
-
Jul 29 17:04:33 localhost maxscale[14035]: Service cache: /var/cache/maxscale
-
Jul 29 17:04:33 localhost maxscale[14035]: Initialise CLI router module V1.0.0.
-
Jul 29 17:04:33 localhost maxscale[14035]: Loaded module cli: V1.0.0 from /usr/lib64/maxscale/libcli.so
-
Jul 29 17:04:33 localhost maxscale[14035]: Initializing statemend-based read/write split router module.
-
Jul 29 17:04:33 localhost maxscale[14035]: Loaded module readwritesplit: V1.0.2 from /usr/lib64/maxscale/libreadwritesplit.so
-
Jul 29 17:04:33 localhost maxscale[14035]: Initialise readconnroute router module V1.1.0.
- Jul 29 17:04:33 localhost maxscale[14035]: Loaded module readconnroute: V1.1.0 from /usr/lib64/maxscale/libreadconnroute.so
-
[root@localhost ~]# tail -f /var/log/messages
-
[root@localhost maxscale]# tail -f maxscale1.log
-
MariaDB Corporation MaxScale /var/log/maxscale/maxscale1.log Fri Jul 29 17:09:46 2016
-----------------------------------------------------------------------
2016-07-29 17:09:46 notice : Configuration file: /etc/maxscale.cnf
2016-07-29 17:09:46 notice : Log directory: /var/log/maxscale
2016-07-29 17:09:46 notice : Data directory: /var/lib/maxscale/data
2016-07-29 17:09:46 notice : Module directory: /usr/lib64/maxscale
2016-07-29 17:09:46 notice : Service cache: /var/cache/maxscale
2016-07-29 17:09:46 notice : Initialise CLI router module V1.0.0.
2016-07-29 17:09:46 notice : Loaded module cli: V1.0.0 from /usr/lib64/maxscale/libcli.so
2016-07-29 17:09:46 notice : Initializing statemend-based read/write split router module.
2016-07-29 17:09:46 notice : Loaded module readwritesplit: V1.0.2 from /usr/lib64/maxscale/libreadwritesplit.so
2016-07-29 17:09:46 notice : Initialise readconnroute router module V1.1.0.
2016-07-29 17:09:46 notice : Loaded module readconnroute: V1.1.0 from /usr/lib64/maxscale/libreadconnroute.so
2016-07-29 17:09:46 notice : Initialise the MySQL Monitor module V1.4.0.
2016-07-29 17:09:46 notice : Loaded module mysqlmon: V1.4.0 from /usr/lib64/maxscale/libmysqlmon.so
2016-07-29 17:09:46 notice : MariaDB Corporation MaxScale beta-1.3.0 (C) MariaDB Corporation Ab 2013-2015
2016-07-29 17:09:46 notice : MaxScale is running in process 14140
2016-07-29 17:09:46 notice : Loaded 2 MySQL Users for service [Read-Only Service].
2016-07-29 17:09:46 notice : Loaded module MySQLClient: V1.0.0 from /usr/lib64/maxscale/libMySQLClient.so
2016-07-29 17:09:46 notice : Listening MySQL connections at 0.0.0.0:4008
2016-07-29 17:09:46 notice : Loaded 2 MySQL Users for service [Read-Write Service].
2016-07-29 17:09:46 notice : Listening MySQL connections at 0.0.0.0:4006
2016-07-29 17:09:46 notice : Loaded module maxscaled: V1.0.0 from /usr/lib64/maxscale/libmaxscaled.so
2016-07-29 17:09:46 notice : Listening maxscale connections at 0.0.0.0:6603
2016-07-29 17:09:46 notice : Started MaxScale log flusher.
2016-07-29 17:09:46 notice : MaxScale started with 8 server threads.
2016-07-29 17:09:47 notice : A Master Server is now available: 192.168.6.114:3306
-
MariaDB Corporation MaxScale /var/log/maxscale/maxscale1.log Fri Jul 29 17:09:46 2016
4測試
-
讀負載均衡測試
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
-
+---------------------+
-
寫測試
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4006 -e "create table tt as select * from mysql.user" chenliang; --建立一個表
-
Warning: Using a password on the command line interface can be insecure.
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang; ---同步到slave1
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
| tt |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang; ---同步到slave2了,很明顯寫在了主庫上,可以開generlog去驗證
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
-
| tt |
-
+---------------------+
-
讀負載均衡測試
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
- +---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
寫測試
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4006 -e "create table tt as select * from mysql.user" chenliang; --建立一個表
-
Warning: Using a password on the command line interface can be insecure.
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang; ---同步到slave1
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
| tt |
-
+---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang; ---同步到slave2了,很明顯寫在了主庫上,可以開generlog去驗證
-
Warning: Using a password on the command line interface can be insecure.
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test119 |
-
| tt |
- +---------------------+
-
[root@localhost etc]# mysql -umax -pESBecs00 -h192.168.6.121 -P4006 -e "create table tt as select * from mysql.user" chenliang; --建立一個表
5 後臺管理
-
[root@localhost ~]# maxadmin -pmariadb
-
MaxScale>
-
MaxScale>
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
-
server3 | 192.168.6.119 | 3306 | 0 | Slave, Running
-
-------------------+-----------------+-------+-------------+--------------------
-
MaxScale> list services
-
Services.
-
--------------------------+----------------------+--------+---------------
-
Service Name | Router Module | #Users | Total Sessions
-
--------------------------+----------------------+--------+---------------
-
Read-Only Service | readconnroute | 1 | 3
-
Read-Write Service | readwritesplit | 1 | 1
-
MaxAdmin Service | cli | 5 | 5
- --------------------------+----------------------+--------+---------------
故障測試之:一臺slave,stop
-
原始狀態
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
-
server3 | 192.168.6.119 | 3306 | 0 | Slave, Running
-
-------------------+-----------------+-------+-------------+--------------------
停掉119的slave,stop slave;檢視狀態
-
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
server3 | 192.168.6.119 | 3306 | 0 | Running
-------------------+-----------------+-------+-------------+--------------------
-
此時讀負載測試:全部落到正常的那臺slave
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
-
-
恢復119 在測試:負載均衡正常了
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
-
server3 | 192.168.6.119 | 3306 | 0 | Slave, Running
-
-------------------+-----------------+-------+-------------+--------------------
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test119 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
-
原始狀態
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
-
server3 | 192.168.6.119 | 3306 | 0 | Slave, Running
- -------------------+-----------------+-------+-------------+--------------------
停掉119的slave,stop slave;檢視狀態 -
MaxScale> list servers
-
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
server3 | 192.168.6.119 | 3306 | 0 | Running
-------------------+-----------------+-------+-------------+--------------------
-
此時讀負載測試:全部落到正常的那臺slave
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
-
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
恢復119 在測試:負載均衡正常了 -
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Slave, Running
-
server3 | 192.168.6.119 | 3306 | 0 | Slave, Running
- -------------------+-----------------+-------+-------------+--------------------
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test119 |
| tt |
| tt2 |
+---------------------+
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
故障測試之:兩臺slave全部stop
-
停掉1156,119的slave;stop slave
-
檢視狀態
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Running
-
server2 | 192.168.6.115 | 3306 | 0 | Running
-
server3 | 192.168.6.119 | 3306 | 0 | Running
-
-------------------+-----------------+-------+-------------+--------------------
-
負載測試:此時負載就出問題,會報錯,起碼得保持一臺slave,存活才行
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
ERROR 1045 (28000): failed to create new session
說明從伺服器全部失效後,會導致 master 也無法識別,使整個資料庫服務都失效了。
對於 slave 全部失效的情況,能否讓 master 還可用?這樣至少可以正常提供資料庫服務。
這需要修改 MaxScale 的配置,告訴 MaxScale 我們需要一個穩定的 master。
處理過程
先恢復兩個 slave,讓叢集回到正常狀態,登陸兩個 slave 的MySQL。
mysql> start slave;
修改 MaxScale 配置檔案,新增新的配置。
vi /etc/maxscale.cnf
找到 [MySQL Monitor] 部分,新增:
detect_stale_master=true
儲存退出,然後重啟 MaxScale。
驗證
停掉兩臺 slave ,檢視 MaxScale 伺服器狀態。
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Stale Status, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Running
-
server3 | 192.168.6.119 | 3306 | 0 | Running
-
-------------------+-----------------+-------+-------------+--------------------
負載測試:
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
-
+---------------------+
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
-
+---------------------+
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
-
+---------------------+
- 停掉1156,119的slave;stop slave
-
檢視狀態
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Running
-
server2 | 192.168.6.115 | 3306 | 0 | Running
-
server3 | 192.168.6.119 | 3306 | 0 | Running
- -------------------+-----------------+-------+-------------+--------------------
-
MaxScale> list servers
-
負載測試:此時負載就出問題,會報錯,起碼得保持一臺slave,存活才行
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
- ERROR 1045 (28000): failed to create new session
說明從伺服器全部失效後,會導致 master 也無法識別,使整個資料庫服務都失效了。
對於 slave 全部失效的情況,能否讓 master 還可用?這樣至少可以正常提供資料庫服務。
這需要修改 MaxScale 的配置,告訴 MaxScale 我們需要一個穩定的 master。
處理過程
先恢復兩個 slave,讓叢集回到正常狀態,登陸兩個 slave 的MySQL。
mysql> start slave;
修改 MaxScale 配置檔案,新增新的配置。
vi /etc/maxscale.cnf
找到 [MySQL Monitor] 部分,新增:
detect_stale_master=true
儲存退出,然後重啟 MaxScale。
驗證
停掉兩臺 slave ,檢視 MaxScale 伺服器狀態。
-
MaxScale> list servers
-
Servers.
-
-------------------+-----------------+-------+-------------+--------------------
-
Server | Address | Port | Connections | Status
-
-------------------+-----------------+-------+-------------+--------------------
-
server1 | 192.168.6.114 | 3306 | 0 | Master, Stale Status, Running
-
server2 | 192.168.6.115 | 3306 | 0 | Running
-
server3 | 192.168.6.119 | 3306 | 0 | Running
- -------------------+-----------------+-------+-------------+--------------------
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
-
+---------------------+
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
-
+---------------------+
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test114 |
-
| tt |
-
| tt2 |
- +---------------------+
-
[root@node4 ~]# mysql -umax -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
補充:可將讀寫分離配置在一個port上
-
配置檔案
-
[maxscale]
-
threads=80
-
-
[server1]
-
type=server
-
address=192.168.6.114
-
port=3306
-
protocol=MySQLBackend
-
-
[server2]
-
type=server
-
address=192.168.6.115
-
port=3306
-
protocol=MySQLBackend
-
-
[server3]
-
type=server
-
address=192.168.6.119
-
port=3306
-
protocol=MySQLBackend
-
-
-
[MySQL Monitor]
-
type=monitor
-
module=mysqlmon
-
servers=server1,server2,server3
-
user=max
-
passwd=ESBecs00
-
monitor_interval=10000
-
detect_stale_master=true
-
-
-
#[Read-Only Service]
-
#type=service
-
#router=readconnroute
-
#servers=server1,server2,server3
-
#user=max
-
#passwd=ESBecs00
-
#router_options=slave
-
-
-
[Read-Write Service]
-
type=service
-
router=readwritesplit
-
servers=server1,server3,server2 -----都配置在這即可
-
user=max
-
passwd=ESBecs00
-
max_slave_connections=100%
-
-
-
[MaxAdmin Service]
type=service
router=cli
#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
port=6603
- #####只需要將 read-only 讀負載均衡的部分註釋掉,只保留read-write部分即可
- 寫操作
-
[root@node4 ~]# mysql -uchenliang -pESBecs00 -h192.168.6.121 -P4006 -e "create table tt3 as select * from tt" chenliang;
- 檢查114,115,119都存在
-
mysql> show tables;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test119 |
| tt |
| tt2 |
| tt3 |
+---------------------+
4 rows in set (0.00 sec)
讀測試:落在了slave上
-
[root@node4 ~]# mysql -uchenliang -pESBecs00 -h192.168.6.121 -P4006 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
| tt |
-
| tt2 |
-
| tt3 |
-
+---------------------+
-
[root@node4 ~]# mysql -uchenliang -pESBecs00 -h192.168.6.121 -P4006 -e "show tables" chenliang;
-
+---------------------+
-
| Tables_in_chenliang |
-
+---------------------+
-
| test115 |
-
| tt |
-
| tt2 |
-
| tt3 |
- +---------------------+
-
[maxscale]
對比:
實驗一:實現了讀寫分離,和讀的負載均衡,但是需要連線兩個不同的埠,不方便
實驗二:實現了讀寫分離,但讀負載均衡不能實現,只需要連結一個埠即可,比較方便
思考:
-
1.假設程式使用者是chenliang 對 chengliang業務庫具有所有權,透過chenliang 這個角色能否做到讀寫分離呢?
-
- 直接使用
-
[root@localhost etc]# mysql -uchenliang -pESBecs00 -h192.168.6.121 -P4006 -e "create table tt2 as select * from tt" chenliang;
Warning: Using a password on the command line interface can be insecure.
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test119 |
| tt |
| tt2 |
+---------------------+
[root@localhost etc]# mysql -uchenliang -pESBecs00 -h192.168.6.121 -P4008 -e "show tables" chenliang;
Warning: Using a password on the command line interface can be insecure.
+---------------------+
| Tables_in_chenliang |
+---------------------+
| test115 |
| tt |
| tt2 |
+---------------------+
-
完美
2 若直接將程式使用者寫到配置檔案的讀寫分離部分可以嗎?
-
[Read-Only Service]
-
type=service
-
router=readconnroute
-
servers=server1,server2,server3
-
user=chenliang
-
passwd=ESBecs00
-
router_options=slave
-
-
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md
-
-
[Read-Write Service]
-
type=service
-
router=readwritesplit
-
servers=server1
-
user=chenliang
-
passwd=ESBecs00
- max_slave_connections=100%
重新啟動觀察日誌會報錯:
-
or table 'user'
-
2016-07-29 17:40:44 warning: Read-Only Service: User 'chenliang' is missing SELECT privileges on mysql.db table. Database name will be ignored in authentication. MySQL error message: SELECT command denied to user 'chenliang'@'192.168.6.121' for table 'db'
-
2016-07-29 17:40:44 error : Read-Only Service: Inadequate user permissions for service. Service not started.
-
2016-07-29 17:40:44 error : Failed to start service 'Read-Only Service'.
-
2016-07-29 17:40:44 error : Read-Write Service: User 'chenliang' is missing SELECT privileges on mysql.user table. MySQL error message: SELECT command denied to user 'chenliang'@'192.168.6.121' for table 'user'
-
2016-07-29 17:40:44 warning: Read-Write Service: User 'chenliang' is missing SELECT privileges on mysql.db table. Database name will be ignored in authentication. MySQL error message: SELECT command denied to user 'chenliang'@'192.168.6.121' for table 'db'
-
2016-07-29 17:40:44 error : Read-Write Service: Inadequate user permissions for service. Service not started.
-
2016-07-29 17:40:44 error : Failed to start service 'Read-Write Service'.
- 2016-07-29 17:40:44 notice : Loaded module maxscaled: V1.0.0 from /usr/lib64/maxscale/libmaxscaled.so
-
- 很明顯:讀寫分離的配置使用者需要對mysql.user表具有查詢許可權
透過二進位制包安裝,原始碼包安裝,密碼加密等參考文章
初探/
http://blog.sina.com.cn/s/blog_534360f50102ver7.html
優酷土豆資深工程師:MySQL高可用之MaxScale與MHA
MaxScale:實現MySQL讀寫分離與負載均衡的中介軟體利器
http://mp.weixin.qq.com/s?__biz=MzI4NTA1MDEwNg==&mid=2650756387&idx=1&sn=c0dce4a24c85307cd4c64c8183bffa36&scene=0#wechat_redirect
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2122737/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL中介軟體MySql
- MySQL中介軟體--ProxySQLMySql
- Mysql中介軟體 oneProxy的使用總結MySql
- 博學谷 - mysql資料庫效能優化筆記05 - 讀寫分離中介軟體MaxScaleMySql資料庫優化筆記
- MySQL中介軟體之ProxySQLMySql
- 【Mysql】KingShard中介軟體的使用與配置MySql
- gin使用中介軟體
- MySQL中介軟體方案盤點MySql
- Express 文件(使用中介軟體)Express
- Django 2.1.3 中介軟體使用Django
- 中介軟體redis的使用Redis
- MysqL讀寫分離的實現-Mysql proxy中介軟體的使用MySql
- Redis中介軟體與Web中介軟體RedisWeb
- 使用淘寶中介軟體cobar實現mysql分庫分表MySql
- MySQL cetus 中介軟體 讀寫分離MySql
- KingShard MySQL中介軟體快速入門MySql
- 中介軟體之訊息中介軟體-pulsar
- gin使用BasicAuth()(驗證)中介軟體
- 訊息中介軟體 — 使用場景
- 常用的MySQL中介軟體網址彙總MySql
- MySQL中介軟體之ProxySQL(14):ProxySQL+PXCMySql
- 阿里開源Mysql分散式中介軟體:Cobar阿里MySql分散式
- MySQL中介軟體總結MySql
- ThinkPHP 中介軟體PHP
- redux中介軟體Redux
- golang 中介軟體Golang
- 中介軟體整理
- django中介軟體Django
- Laravel 中介軟體Laravel
- Django——中介軟體Django
- 中介軟體-NginxNginx
- 中介軟體漏洞
- Laravel 中介軟體使用及原始碼分析Laravel原始碼
- Express的使用筆記3 中介軟體Express筆記
- 使用 defineNuxtRouteMiddleware 建立路由中介軟體UX路由
- 訊息中介軟體Notify和MetaQ-阿里中介軟體阿里
- Mycat中介軟體實現Mysql資料分片( 下篇)MySql
- Mycat中介軟體實現Mysql資料分片(上篇)MySql