本博文演示環境:
centos7.2 x86_64 最小化安裝。3臺vm 時間同步,關閉iptables,selinux.,firewalld.
3臺rpm包安裝MariaDB10.5.1 。172.16.0.130機器單獨安裝 maxscale-2.3.7html
172.16.0.130 master maxscale 172.16.0.131 slave1 172.16.0.132 slave2
如何配置MariaDB MaxScale,並介紹了一些可能的使用方案。 MariaDB MaxScale在設計時考慮了靈活性,它由事件處理核心和各類支持功能以及可定製程序行爲的插件模塊組成
MariaDB MaxScale是一種數據庫代理,可擴展MariaDB Server的高可用性,可伸縮性和安全性,同時經過將其與基礎數據庫基礎架構分離來簡化應用程序開發。
MariaDB MaxScale採用可擴展的體系結構進行工程設計,以支持插件,從而將其功能擴展到透明負載平衡以外,從而成爲例如數據庫防火牆。
藉助用於多個路由器,過濾器和協議的內置插件,能夠將MariaDB MaxScale配置爲轉發數據庫請求並根據業務和技術要求修改數據庫響應,例如,以屏蔽敏感數據或擴展讀取mysql
MariaDB MaxScale是一種數據庫代理,可將數據庫語句轉發到一個或多個數據庫服務器。
使用基於數據庫語句的語義理解以及數據庫後端集羣中服務器的角色的規則執行轉發。
MariaDB MaxScale旨在嚮應用程序透明地提供負載平衡和高可用功能。
MariaDB MaxScale具備可擴展且靈活的體系結構,其插件組件可支持不一樣的協議和路由方法linux
MariaDB MaxScale充分利用了Linux操做系統的異步I / O功能以及固定數量的工做線程。
epoll用於爲經過套接字的輸入和輸出提供事件驅動的框架。
MariaDB MaxScale提供的許多服務都實現爲在運行時加載的外部共享對象模塊。
這些模塊支持固定接口,並經過由一組功能指針組成的結構傳遞入口點。該結構稱爲「模塊對象」。能夠建立其餘模塊以與MariaDB MaxScale一塊兒使用。
經常使用的模塊類型是協議,路由器和過濾器。協議模塊實現了客戶端與MariaDB MaxScale之間以及MariaDB MaxScale與後端服務器之間的通訊。
路由器檢查來自客戶端的查詢並肯定目標後端。這些決定一般基於路由規則和後端服務器狀態。
過濾器對經過MariaDB MaxScale傳遞的數據進行處理。
篩選器一般用於記錄查詢或修改服務器響應。ios
支持源碼包編譯安裝,支持rpm包安裝,也支持二進制tar包安裝
官方安裝文檔:
提問地址:
https://github.com/mariadb-corporation/MaxScale/tree/2.3/Documentationgit
反饋bug地址:
https://jira.mariadb.org/browse/MXSgithub
本演示環境安裝的是maxscale 是2.3.7 ,下面是官方的安裝文檔介紹
https://mariadb.com/kb/en/mariadb-maxscale-23-mariadb-maxscale-installation-guide/sql
本博文采用的是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/centos
在名爲[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
注意:在配置主從複製時,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表明正常
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.
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成功,則故障轉移被視爲成功,由於羣集至少具備一個有效的主服務器
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 maxuser@172.16.0.% | +---------------------------------------------------------------------------------------------------------------------------------+ | 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 repuser@172.16.0.% | +-----------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO `repuser`@`172.16.0.%` IDENTIFIED BY PASSWORD '*15BB449A1D5BDA166BB45F5989B2417834A13C23' | +-----------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
參考文檔資料:
http://www.javashuo.com/article/p-ztsutawd-ba.html
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/