本博文演示環境:
centos7.2 x86_64 最小化安裝。3臺vm 時間同步,關閉iptables,selinux.,firewalld。3臺rpm包安裝MariaDB10.5.1 .172.16.0.130機器單獨安裝 maxscale-2.3.7
172.16.0.130 master maxscale
172.16.0.131 slave1
172.16.0.132 slave2
一.Maxscale簡單介紹:
如何配置MariaDB MaxScale,並介紹了一些可能的使用方案。 MariaDB MaxScale在設計時考慮了靈活性,它由事件處理核心和各種支持功能以及可定製程序行爲的插件模塊組成
MariaDB MaxScale是一種數據庫代理,可擴展MariaDB Server的高可用性,可伸縮性和安全性,同時通過將其與基礎數據庫基礎架構分離來簡化應用程序開發。
MariaDB MaxScale採用可擴展的體系結構進行工程設計,以支持插件,從而將其功能擴展到透明負載平衡之外,從而成爲例如數據庫防火牆。
藉助用於多個路由器,過濾器和協議的內置插件,可以將MariaDB MaxScale配置爲轉發數據庫請求並根據業務和技術要求修改數據庫響應,例如,以屏蔽敏感數據或擴展讀取
MariaDB MaxScale是一種數據庫代理,可將數據庫語句轉發到一個或多個數據庫服務器。
使用基於數據庫語句的語義理解以及數據庫後端集羣中服務器的角色的規則執行轉發。
MariaDB MaxScale旨在嚮應用程序透明地提供負載平衡和高可用功能。
MariaDB MaxScale具有可擴展且靈活的體系結構,其插件組件可支持不同的協議和路由方法
MariaDB MaxScale充分利用了Linux操作系統的異步I / O功能以及固定數量的工作線程。
epoll用於爲通過套接字的輸入和輸出提供事件驅動的框架。
MariaDB MaxScale提供的許多服務都實現爲在運行時加載的外部共享對象模塊。
這些模塊支持固定接口,並通過由一組功能指針組成的結構傳遞入口點。該結構稱爲“模塊對象”。可以創建其他模塊以與MariaDB MaxScale一起使用。
常用的模塊類型是協議,路由器和過濾器。協議模塊實現了客戶端與MariaDB MaxScale之間以及MariaDB MaxScale與後端服務器之間的通信。
路由器檢查來自客戶端的查詢並確定目標後端。這些決定通常基於路由規則和後端服務器狀態。
過濾器對通過MariaDB MaxScale傳遞的數據進行處理。
篩選器通常用於記錄查詢或修改服務器響應。
二.Maxscale 安裝:
支持源碼包編譯安裝,支持rpm包安裝,也支持二進制tar包安裝
官方安裝文檔:
提問地址:
https://github.com/mariadb-corporation/MaxScale/tree/2.3/Documentation
反饋bug地址:
https://jira.mariadb.org/browse/MXS
本演示環境安裝的是maxscale 是2.3.7 ,下面是官方的安裝文檔介紹
https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-installation-guide/
本博文采用的是rpm包安裝,最簡單
yum install libaio.x86_64 libaio-devel.x86_64 novacom-server.x86_64 libedit -y
wget https://downloads.mariadb.com/MaxScale/centos/7/x86_64/maxscale-2.3.7-1.centos.7.x86_64.rpm
rpm -ivh maxscale-2.3.7-1.centos.7.x86_64.rpm
關閉和啓動命令:
service maxscale status|start|stop|restart
systemctl enable maxscale
systemctl status|start|stop maxscale
maxadmin shutdown maxscale
maxscale 管理賬戶登錄: 默認賬戶:admin
[root@mgr01 ~]# maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'
三.配置文件介紹:
默認配置文件在/etc/maxscale.cnf
官方模塊參數介紹地址:
https://mariadb.com/kb/en/mariadb-maxscale-23-contents/#routers
配置文件參數介紹:
https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-configuration-usage-scenarios/
在名爲[MaxScale]的部分中的全局設置允許調整影響整個MariaDB MaxScale的各種參數。此部分必須在默認爲/etc/maxscale.cnf的根配置文件中定義
參數:threads 介紹:threads=[<number of threads> | auto ] 默認是1,最好是根據實際需要來調整
將工作線程數量增加到超過處理器內核數量並不會提高性能,反而可能降低性能,並且可能不必要地消耗資源。
演示成功的配置文件內容:
[root@mgr03 ~]# cat /etc/maxscale.cnf
#https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-configuration-usage-scenarios/
[maxscale]
#threads=auto
threads=1
log_info=1
logdir=/tmp/
auth_read_timeout=10
#提取用戶身份驗證數據時,MySQL與後端數據庫的連接的讀取超時(以秒爲單位)。增加此參數的值將導致在主動獲取用戶數據時,MariaDB MaxScale等待更長的時間來等待來自後端服務器的響應。如果身份驗證失敗,並且您有大量的數據庫用戶和授權,或者與後端服務器的連接速度很慢,則增加此值是個好主意。默認爲1秒.
auth_write_timeout=10
#提取用戶身份驗證數據時,MySQL與後端數據庫的連接的寫入超時(以秒爲單位),當前,MariaDB MaxScale不會在後端服務器中寫入或修改數據。默認值爲2秒。官方建議10s
query_retries=1
#被重試的內部查詢的次數。默認值爲重試一次.中斷查詢是指任何因網絡錯誤而中斷的查詢。連接超時包括在網絡錯誤中,因此建議確保將query_retry_timeout的值設置爲適當的值。2.3.0之後默認開啓
query_retry_timeout=5
##任何重試查詢的總超時時間(以秒爲單位)。默認值爲5秒,
ms_timestamp=1
# ms_timestamp=<0|1>。
# 在日誌文件中啓用或禁用高精度時間戳。啓用此選項可以爲所有日誌文件時間戳增加毫秒精度。
skip_permission_checks=false
#默認是false.
#當您知道權限確定並且想要加快啓動過程時,此功能很有用。此參數爲布爾值,默認情況下處於禁用狀態
#建議不要禁用權限檢查,以便在啓動maxscale時檢測到任何缺少的特權。如果由於在檢查權限時發生大量連接超時而導致MaxScale啓動緩慢,則禁用權限檢查可以加快啓動過程。
syslog=1
# syslog=<0|1>.
#啓用或禁用將消息記錄到syslog。默認情況下,啓用到syslog的日誌記錄
maxlog=1
# 啓用以禁用將消息記錄到MariaDB MaxScale的日誌文件中. 默認情況下,啓用對maxlog的日誌記錄。
# 要啓用到MariaDB MaxScale日誌文件的日誌記錄,請使用值1,而禁用則使用值0。
log_warning=1
# log_warning=<0|1>,
# 啓用或禁用系統日誌優先級爲警告的消息的日誌記錄。默認情況下,啓用此優先級的消息.
log_notice=1
#啓用或禁用系統日誌優先級爲notice的消息的日誌記錄。此優先級的消息提供有關MariaDB MaxScale的功能的信息,並且默認情況下處於啓用狀態。
##要禁用這些消息,請使用值0,而要啓用它們,請使用值1
log_debug=0
[server1]
type=server
address=172.16.0.131
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1
[server2]
type=server
address=172.16.0.132
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1
[server3]
type=server
address=172.16.0.130
port=3306
#protocol=MySQLBackend
protocol=MariaDBBackend
#server_weight=1
serversize=1
# https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-monitor/
[MySQL Monitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3
user=maxmonuser
password=maxmonpwd
monitor_interval=10000
##monitor_interval=10000 #探測間隔,單位毫秒,默認2000
auto_failover=true
#是否故障自動切換
auto_rejoin=true
#故障實例恢復後自動加入集羣
detect_standalone_master=true
#探測獨立的master,是否允許集羣中最後一個實例成爲主庫
#switchover_on_low_disk_space=true
##爲了使此參數生效,必須爲服務器或監視器指定disk_space_threshold。另外,必須爲監視器定義disk_space_check_interval
#allow_cluster_recovery=true
failcount=3
#在集羣中最後一個實例成爲主庫前檢查其它從庫是否存活的次數,默認爲5
detect_stale_master=true
#此參數從2.0版本開始默認就是開啓的,即使複製停止或配置錯誤,也允許先前的主服務器可用。所有從屬服務器均不可訪問或複製由於某種原因而中斷,master依然可以繼續提供服務。
detect_stale_slave=true
#此參數默認是開啓的,將沒有主服務器的正在運行的從服務器視爲有效的從服務器。slave丟失master時,slave仍可用於讀取。
failover_timeout=90
#此參數默認是90s,如果在配置的時間段內未成功進行故障轉移/切換,則會記錄一條消息,並禁用自動故障轉移。這樣可以防止對行爲異常的羣集進行進一步的自動修改.
verify_master_failure=true
master_failure_timeout=10
#啓用其他主服務器故障驗證以進行自動故障轉移.verify_master_failure默認開啓,是布爾值,master_failure_timeout默認是10.
script=/tmp/reset_slave.sh
events=master_down
#檢查到master掛掉的話,執行上面的腳本/tmp/reset_slave.sh
# https://mariadb.com/kb/en/mariadb-maxscale-23-readconnroute/
[Read-Only-Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=maxuser
password=maxpwd
router_options=slave
#router_options=master,slave
#weightby=server_weight
# ReadWriteSplit documentation:
# https://mariadb.com/kb/en/mariadb-maxscale-23-readwritesplit/
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxuser
password=maxpwd
#max_slave_connections=100%
max_slave_connections=2
#設置路由器會話隨時使用的最大從站數量。默認值是每個客戶端連接最多使用255個從屬連接。在舊版本中,默認設置是不加限制地使用所有可用的從站。
# 例如,如果您爲MaxScale配置了一個主機和三個從機,並設置max_slave_connections = 2,則對於每個客戶端連接,將打開到主機的連接和兩個從機連接。然後,在這兩個從屬服務器之間完成讀查詢負載平衡,並將寫操作發送到主服務器.
max_slave_replication_lag=4
#指定從站被允許落後於主站多少秒。如果滯後時間大於配置的值,則不能使用從站進行路由.
master_accept_reads=true
#默認讀是不被路由到master的。允許將主服務器用於讀取。如果您使用的服務器數量較少,並且希望也使用主服務器進行讀取,則啓用此選項非常有用
#weightby=server_weight
# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://mariadb.com/kb/en/mariadb-maxscale-23-maxadmin-admin-interface/
[MaxAdmin-Service]
type=service
router=cli
[Read-Only-Listener]
type=listener
service=Read-Only-Service
#protocol=MySQLClient
protocol=MariaDBClient
port=4008
[Read-Write-Listener]
type=listener
service=Read-Write-Service
#protocol=MySQLClient
protocol=MariaDBClient
port=4006
[MaxAdmin-Listener]
type=listener
service=MaxAdmin-Service
protocol=maxscaled
#socket=default
port=6603
四.提前配置好Mariadb 1主2從:
4.1配置基於gitd的主從複製
注意:在配置主從複製時,maxscale監控集羣不識別主機名,只識別ip地址所以在 CHANGE MASTER TO MASTER_HOST='172.16.0.130',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G 時,記得MASTER_HOST必須填寫ip地址,
否則會導致監控主從複製集羣失敗。
例如slave上:
CHANGE MASTER TO MASTER_HOST='mgr01',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G
[root@mgr01 ~]# maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 172.16.0.131 | 3306 | 0 | Master, Slave of External Server, Running
server2 | 172.16.0.132 | 3306 | 0 | Slave of External Server, Running
server3 | 172.16.0.130 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
Master, Slave of External Server, Running 像這樣的代表監控主從複製集羣關係失敗。
Slave of External Server, Running
例如slave上:
CHANGE MASTER TO MASTER_HOST='172.16.0.130',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='JuwoSdk21TbUser',master_use_gtid=slave_pos;start slave;show slave status\G
[root@mgr01 ~]# maxadmin -h127.0.0.1 -P6603 -uadmin -p'mariadb'
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 172.16.0.131 | 3306 | 0 | Slave, Running
server2 | 172.16.0.132 | 3306 | 0 | Slave, Running
server3 | 172.16.0.130 | 3306 | 0 | Master, Running
-------------------+-----------------+-------+-------------+--------------------
以上status代表正常
4.2 數據庫賬戶創建和授權:
1.監控後端mysql服務的狀態
2.複製集羣操作(failover,switchover,rejoin,reset-replication)
要求監視用戶具有以下特權:
create user 'maxmonuser'@'%' identified by 'maxmonpwd';
grant super, REPLICATION SLAVE,replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'%';
權限作用介紹:
SUPER, to modify slave connections and set globals such as read_only
REPLICATION CLIENT, to list slave connections
RELOAD, to flush binary logs
PROCESS, to check if the event_scheduler process is running
SHOW DATABASES and EVENT, to list and modify server events
授權注意事項:
grant super, replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'172.16.0.%';
MariaDB不支持上述方式的授權,只能採用下面方法授權。
grant super, replication client, reload, process, show databases,event on *.* to 'maxmonuser'@'%';
五.讀寫分離配置和測試:
創建讀寫分離操作後端mysql服務的賬戶:
create user 'maxuser'@'172.16.0.%' identified by 'maxpwd';
grant SELECT on mysql.* to 'maxuser'@'172.16.0.%';
GRANT SHOW DATABASES,super ON *.* TO 'maxuser'@'172.16.0.%';
select user,host from mysql.user;
創建測試用戶:
grant all on *.* to 'maxscale'@'172.16.0.%' identified by 'maxscalepass'; flush privileges;
或者
grant all on *.* to 'maxscale'@'%' identified by 'maxscalepass'; flush privileges;
通過maxscale機器ip 和maxscale.cnf配置文件中指定的4006端口來登錄進行讀寫庫
mysql -umaxscale -p'maxscalepass' -h 172.16.0.130 -P4006
讀寫分離配置:
https://mariadb.com/kb/en/mariadb-maxscale-23-readwritesplit/#readwritesplit
讀寫分割路由決策:
下面的動作會被路由到master上:
write statements,
all statements within an open transaction,
stored procedure calls
user-defined function calls
DDL statements (DROP|CREATE|ALTER TABLE … etc.)
EXECUTE (prepared) statements that modify the database
all statements using temporary tables
如果用max_slave_replication_lag參數配置了readwritesplit服務,並且如果所有從屬服務器都遭受過多的複製滯後,則語句將被路由到主服務器或者其他的slave上。
加入設定爲:max_slave_replication_lag=4
#指定從站被允許落後於主站多少秒。如果滯後時間大於配置的值,則不能使用從站進行路由.
日誌內容warning如下:
2020-02-28 21:52:59.040 info : (4) Route query to master: server3 [172.16.0.130]:3306 <
2020-02-28 21:52:59.057 info : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.059 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event
2020-02-28 21:52:59.059 info : (4) Route query to slave: server1 [172.16.0.131]:3306 <
2020-02-28 21:52:59.060 info : (4) Reply complete, last reply from server1
2020-02-28 21:52:59.495 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now())
2020-02-28 21:52:59.495 info : (4) Route query to master: server3 [172.16.0.130]:3306 <
2020-02-28 21:52:59.511 info : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.513 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event
2020-02-28 21:52:59.513 warning: (4) Replication lag of 'server1' is 48s, which is above the configured limit 4s. 'server1' is excluded from query routing.
2020-02-28 21:52:59.514 info : (4) Route query to slave: server2 [172.16.0.132]:3306 <
2020-02-28 21:52:59.535 info : (4) Reply complete, last reply from server2
2020-02-28 21:52:59.906 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now())
2020-02-28 21:52:59.908 info : (4) Route query to master: server3 [172.16.0.130]:3306 <
2020-02-28 21:52:59.929 info : (4) Reply complete, last reply from server3
2020-02-28 21:52:59.935 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 29, type: QUERY_TYPE_READ, stmt: select * from test_event
2020-02-28 21:52:59.935 info : (4) Route query to slave: server2 [172.16.0.132]:3306 <
2020-02-28 21:52:59.950 info : (4) Reply complete, last reply from server2
2020-02-28 21:53:00.304 info : (4) > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, plen: 90, type: QUERY_TYPE_WRITE, stmt: insert into test_event(username,password,create_time) values("李四","tomcat",now())
被路由到slave上的情況:
可以路由到從站的查詢必須是自動提交的,並且屬於以下組之一
read-only database queries,
read-only queries to system, or user-defined variables,
SHOW statements
system function calls.
六.故障自動切換:
A: Failover故障自動轉移將發生故障的主節點替換爲正在運行的從節點。它執行以下操作:
6.1選擇舊主機的最新從機作爲新主機。選擇標準按降序排列
1.gtid_IO_pos (latest event in relay log)
2.gtid_current_pos (most processed events)
3.log_slave_updates is on
4.disk space is not low
6.2如果新的主服務器具有未處理的中繼日誌項目,請取消並稍後重試
6.3 準備新的master
1.從舊的主服務器上刪除新主服務器用來複制的從服務器連接。
2.關閉新master上的read_only參數
3.啓用計劃的服務器事件(如果啓用了事件處理)。僅啓用在舊主機上啓用的事件
4運行promotion_sql_file中的命令
5.如果存在外部主服務器,則開始複製
6.4重定向所有其他從屬服務器以從新的主服務器複製
STOP SLAVE and RESET SLAVE
CHANGE MASTER TO
START SLAVE
6.5檢查所有從屬是否正在複製
如果步驟6.1到6.3成功,則故障轉移被視爲成功,因爲羣集至少具有一個有效的主服務器
B: Switchover手動切換將正在運行的master與正在運行的slave交換。它執行以下操作:
1.準備降級的舊的master:
Stop any external replication.
Kill connections from super-users since read_only does not affect them.
Enable the read_only-flag to stop writes.
Disable scheduled server events (if event handling is on).
Run the commands in demotion_sql_file.
Flush the binary log (FLUSH LOGS) so that all events are on disk.
2.等待新的master趕上舊的master
3.像故障轉移步驟3和4一樣,升級新的主服務器並重定向從服務器。還要重定向已降級的舊主服務器
4.檢查所有從屬是否正在複製
提示:Failover/switchover requires MariaDB 10.0.2 or later
七.自動重新加入到集羣:
Rejoin將獨立服務器加入羣集,或重定向從非主服務器複製的從服務器。獨立服務器通過以下方式加入:
Run the commands in demotion_sql_file.
Enable the read_only-flag.
Disable scheduled server events (if event handling is on).
Start replication: CHANGE MASTER TO and START SLAVE.
從錯誤的主服務器複製的服務器僅通過STOP SLAVE,RESET SLAVE,CHANGE MASTER TO和START SLAVE命令進行重定向
八.數據庫賬戶權限:
提示數據庫賬戶權限:
(root@'mgr03':mysql.sock)[(none)]>show grants for maxmonuser@'%';
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for maxmonuser@% |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT RELOAD, SUPER, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO `maxmonuser`@`%` IDENTIFIED BY PASSWORD '*DE290C4A038E4E06BB0AF2C0B8B01ABB51572796' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(root@'mgr03':mysql.sock)[(none)]>show grants for maxscale@'%';
+------------------------------------------------------------------------------------------------------------------+
| Grants for maxscale@% |
+------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `maxscale`@`%` IDENTIFIED BY PASSWORD '*E7ACDE28142BFC311A2CE191B3CB3E510A27EB60' |
+------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(root@'mgr03':mysql.sock)[(none)]>show grants for maxuser@'172.16.0.%';
+---------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].% |
+---------------------------------------------------------------------------------------------------------------------------------+
| GRANT SHOW DATABASES, SUPER ON *.* TO `maxuser`@`172.16.0.%` IDENTIFIED BY PASSWORD '*5EDBD32E469DAE0CE10E6999C3899DEFCB9F12E0' |
| GRANT SELECT ON `mysql`.* TO `maxuser`@`172.16.0.%` |
+---------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
(root@'mgr03':mysql.sock)[(none)]>show grants for repuser@'172.16.0.%';
+-----------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].% |
+-----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `repuser`@`172.16.0.%` IDENTIFIED BY PASSWORD '*15BB449A1D5BDA166BB45F5989B2417834A13C23' |
+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
參考文檔資料:
https://blog.51cto.com/linzhijian/1913794
http://udn.yyuap.com/thread-38241-1-1.html
https://downloads.mariadb.com/MaxScale/centos/7/x86_64/
https://blog.csdn.net/weixin_34194551/article/details/92492325
https://blog.csdn.net/thundermeng/article/details/83900503
https://www.jianshu.com/p/1ac435a6510e
http://www.ttlsa.com/mysql/maxscale-install-read-write-split/