您好,登錄后才能下訂單哦!
本文主要給大家介紹使用masterha_master_switch工具實現MySQL高可用MHA的自動切換,文章內容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下使用masterha_master_switch工具實現MySQL高可用MHA的自動切換吧。
簡介:masterha_master_switch作為一個切換工具被集成在MHA程序包中,
安裝:編譯安裝MHA manager后會在/usr/local/bin/中生成二進制可執行程序masterha_master_switch。
使用:
$masterha_master_switch --help
Usage:
# For master failover
masterha_master_switch --master_state=dead
--global_conf=/etc/masterha_default.cnf
--conf=/usr/local/masterha/conf/app1.cnf --dead_master_host=host1
# For online master switch
masterha_master_switch --master_state=alive
--global_conf=/etc/masterha_default.cnf
--conf=/usr/local/masterha/conf/app1.cnf
See online reference
(http://code.google.com/p/mysql-master-ha/wiki/masterha_master_switch)
for details.
在這里,我習慣將一套主從的配置都放在app1.cnf中,并且更改為業務相關的名稱,如mainBusiness.cnf
分析:
目標:獲取masterha_master_switch的在線切換邏輯
環境:MHA manager 192.168.1.8
MHA node1+MySQL5.7+GTID 192.168.1.109+PORT3109 主
MHA node1+MySQL5.7+GTID 192.168.1.110+PORT3110 從
配置文件內容:
manager_workdir=/data/mha/mainBusiness #設置MHA的工作目錄
manager_log=/data/mha/mainBusiness/manager.log #MHA manager的日志輸出
remote_workdir=/data/mha/ #預設MHA node端的工作目錄
master_binlog_dir= /data/mysql/3109/log/,/data/mysql/3110/log/ #預設MHA node端的binlog目錄
#secondary_check_script= masterha_secondary_check -s 192.168.1.109 -s 192.168.1.110
secondary_check_script= masterha_secondary_check -s 192.168.1.109 -s 192.168.1.110 --user=root --master_host=192.168.1.109 --master_port=3109
ping_interval=1 #設置MHA manager的檢測間隔(1秒)
[server1]
hostname=MySQL-Cent7-IP001109
ip=192.168.1.109
port=3109
ssh_user=root
ssh_port=22
candidate_master=1 #設置該節點是否可以提升為主,1為是,0否
check_repl_delay=0 #發生故障后是否檢查本實例主從落后程度,0否,1是
[server2]
hostname=MySQL-Cent7-IP001110
ip=192.168.1.110
port=3110
ssh_user=root
ssh_port=22
candidate_master=1 #設置該節點是否可以提升為主,1為是,0否
check_repl_delay=0 #發生故障后是否檢查本實例主從落后程度,0否,1是
在MHA manager端上執行:
$masterha_master_switch --master_state=alive --conf=/etc/mha/mainBusiness.cnf --orig_master_is_new_slave
#--master_state 指明在線切換
#--orig_master_is_new_slave 指定原先的主作為從庫掛到新的主上
MHA manager端輸出如下
#####################輸出段1###########################
[info] MHA::MasterRotate version 0.57.
[info] Starting online master switch.. #開始在線切換
[info]
[info] * Phase 1: Configuration Check Phase.. #階段1,檢查配置
[info]
[warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.. #我這里沒有使用全局參數文件,會有報錯跳過,沒關系
[info] Reading application default configuration from /etc/mha/mainBusiness.cnf.. #程序從mainBusiness文件中讀取配置
[info] Reading server configuration from /etc/mha/mainBusiness.cnf..
[info] GTID failover mode = 1 #啟用GTID故障轉移模式
[info] Current Alive Master: MySQL-Cent7-IP001109(192.168.1.109:3109) #列出當前存活的主實例
[info] Alive Slaves: #列出當前存活的從實例
[info] MySQL-Cent7-IP001110(192.168.1.110:3110) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
[info] GTID ON #從采用了GTID模式
[info] Replicating from 192.168.1.109(192.168.1.109:3109)
[info] Primary candidate for the new Master (candidate_master is set)
It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on MySQL-Cent7-IP001109(192.168.1.109:3109)? (YES/no):
#處于事務一致性考慮,程序詢問是否臨時關閉非事務型的表
原master,3109端顯示如下
#####################輸出段1###########################
SET wait_timeout=86400; #設置連接超時時間,防止切換時翻車
SELECT @@global.server_id As Value;
SELECT VERSION() AS Value; #獲取用于復制的server—id
SELECT @@global.gtid_mode As Value; #獲取自身是否使用了GTID復制
SHOW GLOBAL VARIABLES LIKE 'log_bin'; #檢查自身是否啟用了binlog
SHOW MASTER STATUS; #獲取自身的事務執行情況
SELECT @@global.datadir AS Value; #獲取自身數據文件的存儲位置
SELECT @@global.slave_parallel_workers AS Value; #檢查是否采用了多線程復制
SHOW SLAVE STATUS; #獲取自身作為從庫時的事務執行情況
SELECT @@global.read_only As Value; #獲取自身是否開啟了只讀
SELECT @@global.relay_log_purge As Value; #檢查自身是否開啟了relay log自動清除
原slave,3110端顯示如下
SELECT @@global.server_id As Value;
SELECT VERSION() AS Value;
SELECT @@global.gtid_mode As Value;
SHOW GLOBAL VARIABLES LIKE 'log_bin';
SHOW MASTER STATUS;
SELECT @@global.datadir AS Value;
SELECT @@global.slave_parallel_workers AS Value;
SHOW SLAVE STATUS;
SELECT @@global.read_only As Value;
SELECT @@global.relay_log_purge As Value;
SELECT @@global.relay_log_info_repository AS Value; #差異處,獲取自身relay信息保存形式(table)
SELECT Relay_log_name FROM mysql.slave_relay_log_info; #差異處,獲取正在使用的relay文件名稱
SELECT @@global.datadir AS Value;
SHOW SLAVE STATUS;
SELECT Repl_slave_priv AS Value FROM mysql.user WHERE user = 'repl'; #差異處,檢查復制用戶是否具有復制權限
第一部分小結:
讀取配置文件,確認主從關系與復制方式;
根據主從關系復制方式,連接主庫:設置必要參數,獲取的復制詳細信息/
連接從庫:獲取復制的詳細信息,獲取relay信息,獲取repl賬號并確認權限
接MHA manager端輸出確認輸入yes后
MHA manager端輸出如下:
#####################輸出段2###########################
[info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
[info] ok. #注意,這里雖然顯示關閉了非事務表,但是上面的抓包語句里面并沒有出現相關語句
[info] Checking MHA is not monitoring or doing failover.. #檢查MHA是否工作,切換時要求MHA manager停止運行
[info] Checking replication health on MySQL-Cent7-IP001110.. #檢查主從健康程度
[info] ok.
[info] Searching new master from slaves.. #開始在從庫中選取一個新主
[info] Candidate masters from the configuration file: #列出候選從庫
[info] MySQL-Cent7-IP001109(192.168.1.109:3109) Version=5.7.19-log log-bin:enabled
[info] GTID ON #檢查GTID開啟情況
[info] MySQL-Cent7-IP001110(192.168.1.110:3110) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
[info] GTID ON #檢查GTID開啟情況
[info] Replicating from 192.168.1.109(192.168.1.109:3109)
[info] Primary candidate for the new Master (candidate_master is set)
[info] Non-candidate masters:
[info] Searching from candidate_master slaves which have received the latest relay log events..
[info] #在所有從庫中選取relay log最新的一個作為新的主庫
From:
MySQL-Cent7-IP001109(192.168.1.109:3109) (current master)
+--MySQL-Cent7-IP001110(192.168.1.110:3110)
To:
MySQL-Cent7-IP001110(192.168.1.110:3110) (new master)
+--MySQL-Cent7-IP001109(192.168.1.109:3109)
Starting master switch from MySQL-Cent7-IP001109(192.168.1.109:3109) to MySQL-Cent7-IP001110(192.168.1.110:3110)? (yes/NO):
#程序詢問是否可以進行切換了
master,3109端顯示如下
#####################輸出段2###########################
USE `unknown_database`;
FLUSH NO_WRITE_TO_BINLOG TABLES; -- 刷新表緩存到硬盤,同時不記錄這條到binlog
SELECT GET_LOCK('MHA_Master_High_Availability_Monitor', '0') AS Value; -- 加一個模擬鎖,防止出現切換程序多開的問題,這樣多開的程序以為獲取不到同名鎖就會失敗退出,
#GET_LOCK(str,time) -- str為鎖的名稱,0表示持續鎖,5.7.5之后可以存在多個名稱不同的GET_LOCK
SHOW PROCESSLIST;
原slave,3110端顯示如下
#####################輸出段2###########################
USE `unknown_database`;
SELECT GET_LOCK('MHA_Master_High_Availability_Failover', '0') AS Value;
SHOW SLAVE STATUS;
SHOW SLAVE STATUS;
第二部分小結:
1.確認可以保存關閉表后,刷新表緩存到磁盤, 避免丟數據,加模擬鎖防止切換程序多開造成切換異常
2.從候選從庫中選出擁有最新數據的從庫,并將其設為要切換到的主庫
接MHA manager端輸出確認可以切換后
MHA manager端輸出如下:
#####################輸出段3###########################
[info] Checking whether MySQL-Cent7-IP001110(192.168.1.110:3110) is ok for the new master..
[info] ok. #檢查上一步中被選定的新主庫的是否真正可以成為主
[info] MySQL-Cent7-IP001109(192.168.1.109:3109): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host. #沒有檢查到原來主(現在被作為從)作為從庫的殘留信息,不管3721先將其掛到一個空主上
[info] MySQL-Cent7-IP001109(192.168.1.109:3109): Resetting slave pointing to the dummy host.
[info] ** Phase 1: Configuration Check Phase completed.
[info] # 配置檢查階段結束
[info] * Phase 2: Rejecting updates Phase..
[info] #
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO):
#切換程序檢測到主機虛擬IP地址切換的地址沒有定義,如果只切換主從身份的話,應用還會寫到原來的主上,需要設置只讀。詢問是否真的要切換,這里我們只是做切換實驗,先觀察下兩個實例的輸出,然后直接切換即可。
原master,3109端顯示如下
USE `unknown_database`;
FLUSH NO_WRITE_TO_BINLOG TABLES; -- 刷新表緩存到磁盤, 避免同步丟數據的問題
SELECT GET_LOCK('MHA_Master_High_Availability_Monitor', '0') AS Value;
SHOW PROCESSLIST;
SHOW SLAVE STATUS;
CHANGE MASTER TO MASTER_HOST='dummy_host'; #將原主作為從切換到一個莫須有的主機上
SHOW SLAVE STATUS;
RESET SLAVE /*!50516 ALL */; #嘗試清除自己之前殘存的slave屬性的信息,若本機不為主時,需要!
SELECT RELEASE_LOCK('MHA_Master_High_Availability_Monitor') As Value;
原slave,3110端顯示如下
USE `unknown_database`;
SELECT GET_LOCK('MHA_Master_High_Availability_Failover', '0') AS Value;
SHOW SLAVE STATUS;
SHOW SLAVE STATUS;
SHOW PROCESSLIST;
第三部分總結:
1.檢查選定的從庫的詳細信息,確認真的是否可以作為新的主庫
2.處理從庫之間的數據比對,若只有一個主和從,則將原主庫先掛到一個莫須有的空主上
若有兩個以上的從庫,則需清理從庫原來的slave記錄
接MHA manager端輸出確認,強制切換后:
MHA manager端輸出如下:
#####################輸出段4###########################
[info] Locking all tables on the orig master to reject updates from everybody (including root):
[info] Executing FLUSH TABLES WITH READ LOCK..
[info] ok. #所有的表都禁止寫操作
[info] Orig master binlog:pos is 3109binlog.000070:536.
[info] Waiting to execute all relay logs on MySQL-Cent7-IP001110(192.168.1.110:3110)..
[info] master_pos_wait(3109binlog.000070:536) completed on MySQL-Cent7-IP001110(192.168.1.110:3110). Executed 0 events.
[info] done.#將從庫已經獲得,但還未來得及執行的事務應用到自身,和其他沒有跟上的從庫
[info] Getting new master`s binlog name and position..
[info] 3110binlog.000049:536
[info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='MySQL-Cent7-IP001110 or 192.168.1.110', MASTER_PORT=3110, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
[info] Setting read_only=0 on MySQL-Cent7-IP001110(192.168.1.110:3110)..
[info] ok. #即將所有其他從庫都用change master的語句切換到新的主,即原來的從110上
[info]
[info] * Switching slaves in parallel..
[info] #即將并行切換所有從庫
[info] Unlocking all tables on the orig master:
[info] Executing UNLOCK TABLES..
[info] ok. #釋放原主庫的表鎖
[info] Starting orig master as a new slave..
[info] Resetting slave MySQL-Cent7-IP001109(192.168.1.109:3109) and starting replication from the new master MySQL-Cent7-IP001110(192.168.1.110:3110)..
[info] Executed CHANGE MASTER.
[info] Slave started.
[info] All new slave servers switched successfully.
[info]
[info] * Phase 5: New master cleanup phase..
[info] #即將清理新從庫的主從信息,切換到主庫
[info] MySQL-Cent7-IP001110: Resetting slave info succeeded.
[info] Switching master to MySQL-Cent7-IP001110(192.168.1.110:3110) completed successfully.
#切換完成
新slave,3109端顯示如下:
#####################輸出段4###########################
USE ``;
SELECT CONNECTION_ID() AS Value;
SET wait_timeout=86400;
SET GLOBAL read_only=1; #將自己設為只讀
SHOW MASTER STATUS;
UNLOCK TABLES; #釋放表鎖
CHANGE MASTER TO MASTER_HOST='192.168.1.110', MASTER_PORT=3110, MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_AUTO_POSITION=1; #把自己指向MHA選定的新主
START SLAVE;
SHOW SLAVE STATUS; #差異處,啟動slave身份
SELECT RELEASE_LOCK('MHA_Master_High_Availability_Failover') As Value; #釋放鎖止同應用的并發鎖
新master,3110端顯示如下:
#####################輸出段4###########################
SHOW SLAVE STATUS;
SELECT MASTER_POS_WAIT('3109binlog.000070','536',0) AS Result; #檢查自己是否執行完差異事務
STOP SLAVE SQL_THREAD; #停止從庫SQL線程
SHOW SLAVE STATUS;
SHOW MASTER STATUS;
SELECT @@global.read_only As Value;
SELECT @@global.read_only As Value;
SET GLOBAL read_only=0; #取消只讀狀態
USE ``;
SELECT UNIX_TIMESTAMP();
SELECT @@GLOBAL.SERVER_ID;
SET @master_heartbeat_period= 30000001024;
SET @master_binlog_checksum= @@global.binlog_checksum;
SELECT @master_binlog_checksum;
SELECT @@GLOBAL.GTID_MODE;
SELECT @@GLOBAL.SERVER_UUID;
SET @slave_uuid= '28ea40ab-9bbd-11e7-8cd1-000c29c31069'; #寫入從庫的UUID
STOP SLAVE; #停止自身作為從庫的身份
SHOW SLAVE STATUS;
RESET SLAVE /*!50516 ALL */; #清除自身作為從庫的所有記錄
SHOW SLAVE STATUS;
SELECT RELEASE_LOCK('MHA_Master_High_Availability_Failover') As Value; #釋放鎖止同應用的并發鎖
看完以上關于使用masterha_master_switch工具實現MySQL高可用MHA的自動切換,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業知識信息 ,可以持續關注我們的行業資訊欄目的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。