In theprevious postof this series we saw how you could usemysqlrpladmin
to perform manual failover/switchover when GTID replication is enabled in MySQL 5.6. Now we will reviewmysqlfailover
(version 1.4.3), another tool from the MySQL Utilities that can be used for automatic failover.
Summary
-
mysqlfailover
can perform automatic failover if MySQL 5.6′s GTID-replication is enabled. - All slaves must use
--master-info-repository=TABLE
. - The monitoring node is a single point of failure: don’t forget to monitor it!
- Detection of errant transactions works well, but you have to use the
--pedantic
option to make sure failover will never happen if there is an errant transaction. - There are a few limitations such as the inability to only fail over once, or excessive CPU utilization, but they are probably not showstoppers for most setups.
Setup
We will use the same setup as last time: one master and two slaves, all using GTID replication. We can see the topology usingmysqlfailover
with thehealth
command:
$ mysqlfailover --master=root@localhost:13001 --discover-slaves-login=root health[...]MySQL Replication Failover UtilityFailover Mode = auto Next Interval = Tue Jul1 10:01:22 2014Master Information------------------Binary Log File PositionBinlog_Do_DBBinlog_Ignore_DBmysql-bin.000003700GTID Executed Seta9a396c6-00f3-11e4-8e66-9cebe8067a3f:1-3Replication Health Status+------------+--------+---------+--------+------------+---------+| host | port | role| state| gtid_mode| health|+------------+--------+---------+--------+------------+---------+| localhost| 13001| MASTER| UP | ON | OK|| localhost| 13002| SLAVE | UP | ON | OK|| localhost| 13003| SLAVE | UP | ON | OK|+------------+--------+---------+--------+------------+---------+
$mysqlfailover--master=root@localhost:13001--discover-slaves-login=roothealth [...] MySQLReplicationFailoverUtility FailoverMode=auto NextInterval=TueJul 110:01:222014 MasterInformation ------------------ BinaryLogFile Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000003 700 GTIDExecutedSet a9a396c6-00f3-11e4-8e66-9cebe8067a3f:1-3 ReplicationHealthStatus +------------+--------+---------+--------+------------+---------+ |host |port |role |state |gtid_mode |health | +------------+--------+---------+--------+------------+---------+ |localhost |13001 |MASTER |UP |ON |OK | |localhost |13002 |SLAVE |UP |ON |OK | |localhost |13003 |SLAVE |UP |ON |OK | +------------+--------+---------+--------+------------+---------+ |
Note that--master-info-repository=TABLE
needs to be configured on all slaves or the tool will exit with an error message:
2014-07-01 10:18:55 AM CRITICAL Failover requires --master-info-repository=TABLE for all slaves.ERROR: Failover requires --master-info-repository=TABLE for all slaves.
2014-07-0110:18:55AMCRITICALFailoverrequires--master-info-repository=TABLEforallslaves. ERROR:Failoverrequires--master-info-repository=TABLEforallslaves. |
Failover
You can use 2 commands to trigger automatic failover:
-
auto
: the tool tries to find a candidate in the list of servers specified with--candidates
, and if no good server is found in this list, it will look at the other slaves to see if one can be a good candidate. This is the default command -
elect
: same asauto
, but if no good candidate is found in the list of candidates, other slaves will not be checked and the tool will exit with an error.
Let’s start the tool withauto
:
$ mysqlfailover --master=root@localhost:13001 --discover-slaves-login=root auto
$mysqlfailover--master=root@localhost:13001--discover-slaves-login=rootauto |
The monitoring console is visible and is refreshed every--interval
seconds (default: 15). Its output is similar to what you get when using thehealth
command.
Then let’s kill -9 the master to see what happens once the master is detected as down:
Failed to reconnect to the master after 3 attemps.Failover starting in 'auto' mode...# Candidate slave localhost:13002 will become the new master.# Checking slaves status (before failover).# Preparing candidate for failover.# Creating replication user if it does not exist.# Stopping slaves.# Performing STOP on all slaves.# Switching slaves to new master.# Disconnecting new master as slave.# Starting slaves.# Performing START on all slaves.# Checking slaves for errors.# Failover complete.# Discovering slaves for master at localhost:13002Failover console will restart in 5 seconds.MySQL Replication Failover UtilityFailover Mode = auto Next Interval = Tue Jul1 10:59:47 2014Master Information------------------Binary Log File PositionBinlog_Do_DBBinlog_Ignore_DBmysql-bin.000005191GTID Executed Seta9a396c6-00f3-11e4-8e66-9cebe8067a3f:1-3Replication Health Status+------------+--------+---------+--------+------------+---------+| host | port | role| state| gtid_mode| health|+------------+--------+---------+--------+------------+---------+| localhost| 13002| MASTER| UP | ON | OK|| localhost| 13003| SLAVE | UP | ON | OK|+------------+--------+---------+--------+------------+---------+
Failedtoreconnecttothemasterafter3attemps. Failoverstartingin'auto'mode... # Candidate slave localhost:13002 will become the new master. # Checking slaves status (before failover). # Preparing candidate for failover. # Creating replication user if it does not exist. # Stopping slaves. # Performing STOP on all slaves. # Switching slaves to new master. # Disconnecting new master as slave. # Starting slaves. # Performing START on all slaves. # Checking slaves for errors. # Failover complete. # Discovering slaves for master at localhost:13002 Failoverconsolewillrestartin5seconds. MySQLReplicationFailoverUtility FailoverMode=auto NextInterval=TueJul 110:59:472014 MasterInformation ------------------ BinaryLogFile Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000005 191 GTIDExecutedSet a9a396c6-00f3-11e4-8e66-9cebe8067a3f:1-3 ReplicationHealthStatus +------------+--------+---------+--------+------------+---------+ |host |port |role |state |gtid_mode |health | +------------+--------+---------+--------+------------+---------+ |localhost |13002 |MASTER |UP |ON |OK | |localhost |13003 |SLAVE |UP |ON |OK | +------------+--------+---------+--------+------------+---------+ |
Looks good! The tool is then ready to fail over to another slave if the new master becomes unavailable.
You can also run custom scripts at several points of execution with the--exec-before
,--exec-after
,--exec-fail-check
,--exec-post-failover
options.
However it would be great to have a--failover-and-exit
option to avoid flapping: the tool would detect master failure, promote one of the slaves, reconfigure replication and then exit (this is what MHA does for instance).
Tool registration
When the tool is started, it registers itself on the master by writing a few things in the specific table:
mysql> SELECT * FROM mysql.failover_console;+-----------+-------+| host| port|+-----------+-------+| localhost | 13001 |+-----------+-------+
mysql>SELECT*FROMmysql.failover_console; +-----------+-------+ |host |port | +-----------+-------+ |localhost|13001| +-----------+-------+ |
This is nice as it avoids that you start several instances ofmysqlfailover
to monitor the same master. If we try, this is what we get:
$ mysqlfailover --master=root@localhost:13001 --discover-slaves-login=root auto[...]Multiple instances of failover console found for master localhost:13001.If this is an error, restart the console with --force.Failover mode changed to 'FAIL' for this instance.Console will start in 10 seconds..........starting Console.
$mysqlfailover--master=root@localhost:13001--discover-slaves-login=rootauto [...] Multipleinstancesoffailoverconsolefoundformasterlocalhost:13001. Ifthisisanerror,restarttheconsolewith--force. Failovermodechangedto'FAIL'forthisinstance. Consolewillstartin10seconds..........startingConsole. |
With thefail
command,mysqlfailover
will monitor replication health and exit in the case of a master failure, without actually performing failover.
Running in the background
In all previous examples,mysqlfailover
was running in the foreground. This is very good for demo, but in a production environment you are likely to prefer running it in the background. This can be done with the--daemon
option:
$ mysqlfailover --master=root@localhost:13001 --discover-slaves-login=root auto --daemon=start --log=/var/log/mysqlfailover.log
$mysqlfailover--master=root@localhost:13001--discover-slaves-login=rootauto--daemon=start--log=/var/log/mysqlfailover.log |
and it can be stopped with:
$ mysqlfailover --daemon=stop
$mysqlfailover--daemon=stop |
Errant transactions
If we create an errant transaction on one of the slaves, it will be detected:
MySQL Replication Failover UtilityFailover Mode = auto Next Interval = Tue Jul1 16:29:44 2014[...]WARNING: Errant transaction(s) found on slave(s).Replication Health Status[...]
MySQLReplicationFailoverUtility FailoverMode=auto NextInterval=TueJul 116:29:442014 [...] WARNING:Erranttransaction(s)foundonslave(s). ReplicationHealthStatus [...] |
However this does not prevent failover from occurring! You have to use--pedantic
:
$ mysqlfailover --master=root@localhost:13001 --discover-slaves-login=root --pedantic auto[...]# WARNING: Errant transaction(s) found on slave(s).#- For slave 'localhost@13003': db906eee-012d-11e4-8fe1-9cebe8067a3f:12014-07-01 16:44:49 PM CRITICAL Errant transaction(s) found on slave(s). Note: If you want to ignore this issue, please do not use the --pedantic option.ERROR: Errant transaction(s) found on slave(s). Note: If you want to ignore this issue, please do not use the --pedantic option.
$mysqlfailover--master=root@localhost:13001--discover-slaves-login=root--pedanticauto [...] # WARNING: Errant transaction(s) found on slave(s). # - For slave 'localhost@13003': db906eee-012d-11e4-8fe1-9cebe8067a3f:1 2014-07-0116:44:49PMCRITICALErranttransaction(s)foundonslave(s).Note:Ifyouwanttoignorethisissue,pleasedonotusethe--pedanticoption. ERROR:Erranttransaction(s)foundonslave(s).Note:Ifyouwanttoignorethisissue,pleasedonotusethe--pedanticoption. |
Limitations
- Like for
mysqlrpladmin
, the slave election process is not very sophisticated and it cannot be tuned. - The server on which
mysqlfailover
is running is a single point of failure. - Excessive CPU utilization: once it is running,
mysqlfailover
hogs one core. This is quite surprising.
Conclusion
mysqlfailover
is a good tool to automate failover in clusters using GTID replication. It is flexible and looks reliable. Its main drawback is that there is no easy way to make it highly available itself: ifmysqlfailover
crashes, you will have to manually restart it.

要優化MySQL慢查詢,需使用slowquerylog和performance_schema:1.啟用slowquerylog並設置閾值,記錄慢查詢;2.利用performance_schema分析查詢執行細節,找出性能瓶頸並優化。

MySQL和SQL是開發者必備技能。 1.MySQL是開源的關係型數據庫管理系統,SQL是用於管理和操作數據庫的標準語言。 2.MySQL通過高效的數據存儲和檢索功能支持多種存儲引擎,SQL通過簡單語句完成複雜數據操作。 3.使用示例包括基本查詢和高級查詢,如按條件過濾和排序。 4.常見錯誤包括語法錯誤和性能問題,可通過檢查SQL語句和使用EXPLAIN命令優化。 5.性能優化技巧包括使用索引、避免全表掃描、優化JOIN操作和提升代碼可讀性。

MySQL異步主從復制通過binlog實現數據同步,提升讀性能和高可用性。 1)主服務器記錄變更到binlog;2)從服務器通過I/O線程讀取binlog;3)從服務器的SQL線程應用binlog同步數據。

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL的安裝和基本操作包括:1.下載並安裝MySQL,設置根用戶密碼;2.使用SQL命令創建數據庫和表,如CREATEDATABASE和CREATETABLE;3.執行CRUD操作,使用INSERT,SELECT,UPDATE,DELETE命令;4.創建索引和存儲過程以優化性能和實現複雜邏輯。通過這些步驟,你可以從零開始構建和管理MySQL數據庫。

InnoDBBufferPool通過將數據和索引頁加載到內存中來提升MySQL數據庫的性能。 1)數據頁加載到BufferPool中,減少磁盤I/O。 2)臟頁被標記並定期刷新到磁盤。 3)LRU算法管理數據頁淘汰。 4)預讀機制提前加載可能需要的數據頁。

MySQL適合初學者使用,因為它安裝簡單、功能強大且易於管理數據。 1.安裝和配置簡單,適用於多種操作系統。 2.支持基本操作如創建數據庫和表、插入、查詢、更新和刪除數據。 3.提供高級功能如JOIN操作和子查詢。 4.可以通過索引、查詢優化和分錶分區來提升性能。 5.支持備份、恢復和安全措施,確保數據的安全和一致性。

全表掃描在MySQL中可能比使用索引更快,具體情況包括:1)數據量較小時;2)查詢返回大量數據時;3)索引列不具備高選擇性時;4)複雜查詢時。通過分析查詢計劃、優化索引、避免過度索引和定期維護表,可以在實際應用中做出最優選擇。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用