目錄
Summary
Failover vs switchover
Setup for this test
Simple failover scenario
Simple switchover scenario
Extension points
What about errant transactions?
Some limitations
Conclusion
首頁 資料庫 mysql教程 Failover with the MySQL Utilities – Part 1: mysqlrpladmin_MySQL

Failover with the MySQL Utilities – Part 1: mysqlrpladmin_MySQL

Jun 01, 2016 pm 01:07 PM

MySQL Utilitiesare a set of tools provided by Oracle to perform many kinds of administrative tasks. When GTID-replication is enabled, 2 tools can be used for slave promotion:mysqlrpladminandmysqlfailover. We will reviewmysqlrpladmin(version 1.4.3) in this post.

Summary

  • mysqlrpladmincan perform manual failover/switchover when GTID-replication is enabled.
  • You need to have your servers configured with--master-info-repository = TABLEor to add the--rpl-useroption for the tool to work properly.
  • The check for errant transactions is failing in the current GA version (1.4.3) so be extra careful when using it or watchbug #73110to see when a fix is committed.
  • There are some limitations, for instance the inability to pre-configure the list of slaves in a configuration file or the inability to check that the tool will work well without actually doing a failover or switchover.

Failover vs switchover

mysqlrpladmin can help you promote a slave to be the new master when the master goes down and then automate replication reconfiguration after this slave promotion. There are 2 separate scenarios: unplanned promotion (failover) and planned promotion (switchover). Beyond the words, it has implications on the way you have to execute the tool.

Setup for this test

To test the tool, our setup will be a master with 2 slaves, all using GTID replication.mysqlrpladmincan show us the current replication topology with thehealthcommand:

$ mysqlrpladmin --master=root@localhost:13001 --discover-slaves-login=root health# Discovering slaves for master at localhost:13001# Discovering slave at localhost:13002# Found slave: localhost:13002# Discovering slave at localhost:13003# Found slave: localhost:13003# Checking privileges.## Replication Topology Health:+------------+--------+---------+--------+------------+---------+| 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|+------------+--------+---------+--------+------------+---------+# ...done.
登入後複製

$mysqlrpladmin--master=root@localhost:13001--discover-slaves-login=roothealth

# Discovering slaves for master at localhost:13001

# Discovering slave at localhost:13002

# Found slave: localhost:13002

# Discovering slave at localhost:13003

# Found slave: localhost:13003

# Checking privileges.

#

# Replication Topology Health:

+------------+--------+---------+--------+------------+---------+

|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      |

+------------+--------+---------+--------+------------+---------+

# ...done.

As you can see, we have to specify how to connect to the master (no surprise) but instead of listing all the slaves, we can let the tool discover them.

Simple failover scenario

What will the tool do when performing failover? Essentially we will give it the list of slaves and the list of candidates and it will:

  • Run a few sanity checks
  • Elect a candidate to be the new master
  • Make the candidate as up-to-date as possible by making it a slave of all the other slaves
  • Configure replication on all the other slaves to make them replicate from the new master

After killing -9 the master, let’s try failover:

$ mysqlrpladmin --slaves=root:@localhost:13002,root:@localhost:13003 --candidates=root@localhost:13002 failover
登入後複製

$mysqlrpladmin--slaves=root:@localhost:13002,root:@localhost:13003--candidates=root@localhost:13002failover

This time, the master is down so the tool has no way to automatically discover the slaves. Thus we have to specify them with the--slavesoption.

However we get an error:

# Checking privileges.# Checking privileges on candidates.ERROR: You must specify either the --rpl-user or set all slaves to use --master-info-repository=TABLE.
登入後複製

# Checking privileges.

# Checking privileges on candidates.

ERROR:Youmustspecifyeitherthe--rpl-userorsetallslavestouse--master-info-repository=TABLE.

The error message is clear, but it would have been nice to have such details when running thehealthcommand (maybe a warning instead of an error). That would allow you to check beforehand that the tool can run smoothly rather than to discover in the middle of an emergency that you have to look at the documentation to find which option is missing.

Let’s choose to specify the replication user:

$ mysqlrpladmin --slaves=root:@localhost:13002,root:@localhost:13003 --candidates=root@localhost:13002 --rpl-user=repl:repl failover# Checking privileges.# Checking privileges on candidates.# Performing failover.# 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.## Replication Topology Health:+------------+--------+---------+--------+------------+---------+| host | port | role| state| gtid_mode| health|+------------+--------+---------+--------+------------+---------+| localhost| 13002| MASTER| UP | ON | OK|| localhost| 13003| SLAVE | UP | ON | OK|+------------+--------+---------+--------+------------+---------+# ...done.
登入後複製

$mysqlrpladmin--slaves=root:@localhost:13002,root:@localhost:13003--candidates=root@localhost:13002--rpl-user=repl:replfailover

# Checking privileges.

# Checking privileges on candidates.

# Performing failover.

# 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.

#

# Replication Topology Health:

+------------+--------+---------+--------+------------+---------+

|host      |port  |role    |state  |gtid_mode  |health  |

+------------+--------+---------+--------+------------+---------+

|localhost  |13002  |MASTER  |UP    |ON        |OK      |

|localhost  |13003  |SLAVE  |UP    |ON        |OK      |

+------------+--------+---------+--------+------------+---------+

# ...done.

Simple switchover scenario

Let’s now restart the old master and configure it as a slave of the new master (by the way, this can be done withmysqlreplicate, another tool from the MySQL Utilities). If we want to promote the old master, we can run:

$ mysqlrpladmin --master=root@localhost:13002 --new-master=root@localhost:13001 --discover-slaves-login=root --demote-master --rpl-user=repl:repl --quiet switchover# Discovering slave at localhost:13001# Found slave: localhost:13001# Discovering slave at localhost:13003# Found slave: localhost:13003+------------+--------+---------+--------+------------+---------+| 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|+------------+--------+---------+--------+------------+---------+
登入後複製

$mysqlrpladmin--master=root@localhost:13002--new-master=root@localhost:13001--discover-slaves-login=root--demote-master--rpl-user=repl:repl--quietswitchover

# Discovering slave at localhost:13001

# Found slave: localhost:13001

# Discovering slave at localhost:13003

# Found slave: localhost:13003

+------------+--------+---------+--------+------------+---------+

|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      |

+------------+--------+---------+--------+------------+---------+

Notice that the master is available in this case so we can use thediscover-slaves-loginoption. Also notice that we can tune the verbosity of the tool by using--quietor--verboseor even log the output in a file with--log.

We also used--demote-masterto make the old master a slave of the new master. Without this option, the old master will be isolated from the other nodes.

Extension points

In general doing switchover/failover at the database level is one thing but informing the other components of the application that something has changed is most often necessary for the application to keep on working correctly.

This is where the extension points are handy: you can execute a script before switchover/failover with--exec-beforeand after switchover/failover with--exec-after.

For instance with these simple scripts:

# cat /usr/local/bin/check_before#!/bin/bash/usr/local/mysql5619/bin/mysql -uroot -S /tmp/node1.sock -Ee 'SHOW SLAVE STATUS' > /tmp/before# cat /usr/local/bin/check_after#!/bin/bash/usr/local/mysql5619/bin/mysql -uroot -S /tmp/node1.sock -Ee 'SHOW SLAVE STATUS' > /tmp/after
登入後複製

# cat /usr/local/bin/check_before

#!/bin/bash

/usr/local/mysql5619/bin/mysql-uroot-S/tmp/node1.sock-Ee'SHOW SLAVE STATUS'>/tmp/before

# cat /usr/local/bin/check_after

#!/bin/bash

/usr/local/mysql5619/bin/mysql-uroot-S/tmp/node1.sock-Ee'SHOW SLAVE STATUS'>/tmp/after

We can execute:

$ mysqlrpladmin --master=root@localhost:13001 --new-master=root@localhost:13002 --discover-slaves-login=root --demote-master --rpl-user=repl:repl --quiet --exec-before=/usr/local/bin/check_before --exec-after=/usr/local/bin/check_after switchover
登入後複製

$mysqlrpladmin--master=root@localhost:13001--new-master=root@localhost:13002--discover-slaves-login=root--demote-master--rpl-user=repl:repl--quiet--exec-before=/usr/local/bin/check_before--exec-after=/usr/local/bin/check_afterswitchover

And looking the /tmp/before and /tmp/after, we can see that our scripts have been executed:

# cat /tmp/before# cat /tmp/after*************************** 1. row *************************** Slave_IO_State: Queueing master event to the relay logMaster_Host: localhostMaster_User: replMaster_Port: 13002[...]
登入後複製

# cat /tmp/before

# cat /tmp/after

***************************1.row***************************

              Slave_IO_State:Queueingmastereventtotherelaylog

                  Master_Host:localhost

                  Master_User:repl

                  Master_Port:13002

[...]

If the external script does not seem to work, using –verbose can be useful to diagnose the issue.

What about errant transactions?

We already mentioned that errant transactions can createlots of issueswhen a new master is promoted in a cluster running GTIDs. So the question is: howmysqlrpladminbehaves when there is an errant transaction?

Let’s create an errant transaction:

# On localhost:13003mysql> CREATE DATABASE test2;mysql> FLUSH LOGS;mysql> SHOW BINARY LOGS;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 69309 || mysql-bin.000002 | 1237667 || mysql-bin.000003 | 617 || mysql-bin.000004 | 231 |+------------------+-----------+mysql> PURGE BINARY LOGS TO 'mysql-bin.000004';
登入後複製

# On localhost:13003

mysql>CREATEDATABASEtest2;

mysql>FLUSHLOGS;

mysql>SHOWBINARYLOGS;

+------------------+-----------+

|Log_name        |File_size|

+------------------+-----------+

|mysql-bin.000001|    69309|

|mysql-bin.000002|  1237667|

|mysql-bin.000003|      617|

|mysql-bin.000004|      231|

+------------------+-----------+

mysql>PURGEBINARYLOGSTO'mysql-bin.000004';

and let’s try to promote localhost:13003 as the new master:

$ mysqlrpladmin --master=root@localhost:13001 --new-master=root@localhost:13003 --discover-slaves-login=root --demote-master --rpl-user=repl:repl --quiet switchover[...]+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| host | port | role| state| gtid_mode| health|+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| localhost| 13003| MASTER| UP | ON | OK|| localhost| 13001| SLAVE | UP | ON | IO thread is not running., Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.', Slave has 1 transactions behind master.|| localhost| 13002| SLAVE | UP | ON | IO thread is not running., Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.', Slave has 1 transactions behind master.|+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
登入後複製

$mysqlrpladmin--master=root@localhost:13001--new-master=root@localhost:13003--discover-slaves-login=root--demote-master--rpl-user=repl:repl--quietswitchover

[...]

+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

|host      |port  |role    |state  |gtid_mode  |health                                                                                                                                                                                                                                                                                              |

+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

|localhost  |13003  |MASTER  |UP    |ON        |OK                                                                                                                                                                                                                                                                                                  |

|localhost  |13001  |SLAVE  |UP    |ON        |IOthreadisnotrunning.,Gotfatalerror1236frommasterwhenreadingdatafrombinarylog:'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.',Slavehas1transactionsbehindmaster.  |

|localhost  |13002  |SLAVE  |UP    |ON        |IOthreadisnotrunning.,Gotfatalerror1236frommasterwhenreadingdatafrombinarylog:'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.',Slavehas1transactionsbehindmaster.  |

+------------+--------+---------+--------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Oops! Although it is suggested by the documentation, the tool does not check errant transactions. This is a major issue as you cannot run failover/switchover reliably with GTID replication if errant transactions are not correctly detected.

The documentation suggests errant transactions should be checked and a quick look at the code confirms that, but it does not work! So it has beenreported.

Some limitations

Apart from the missing errant transaction check, I also noticed a few limitations:

  • You cannot use a configuration file listing all the slaves. This becomes boring once you have a large amount of slaves. In such a case, you should write a wrapper script aroundmysqlrpladminto generate the right command for you
  • The slave election process is either automatic or it relies on the order of the servers given in the--candidatesoption. This is not very sophisticated.
  • It would be useful to have a –dry-run mode which would validate that everything is configured correctly but without actually failing/switching over. This is something MHA does for instance.

Conclusion

mysqlrpladminis a very good tool to help you perform manual failover/switchover in a cluster using GTID replication. The main caveat at this point is the failing check for errant transactions, which requires a lot of care before executing the tool.

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何使用Alter Table語句在MySQL中更改表? 如何使用Alter Table語句在MySQL中更改表? Mar 19, 2025 pm 03:51 PM

本文討論了使用MySQL的Alter Table語句修改表,包括添加/刪除列,重命名表/列以及更改列數據類型。

說明InnoDB全文搜索功能。 說明InnoDB全文搜索功能。 Apr 02, 2025 pm 06:09 PM

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

如何為MySQL連接配置SSL/TLS加密? 如何為MySQL連接配置SSL/TLS加密? Mar 18, 2025 pm 12:01 PM

文章討論了為MySQL配置SSL/TLS加密,包括證書生成和驗證。主要問題是使用自簽名證書的安全含義。[角色計數:159]

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什麼? 哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什麼? Mar 21, 2025 pm 06:28 PM

文章討論了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比較了它們對初學者和高級用戶的功能和適合性。[159個字符]

您如何處理MySQL中的大型數據集? 您如何處理MySQL中的大型數據集? Mar 21, 2025 pm 12:15 PM

文章討論了處理MySQL中大型數據集的策略,包括分區,碎片,索引和查詢優化。

如何使用Drop Table語句將表放入MySQL中? 如何使用Drop Table語句將表放入MySQL中? Mar 19, 2025 pm 03:52 PM

本文討論了使用Drop Table語句在MySQL中放下表,並強調了預防措施和風險。它強調,沒有備份,該動作是不可逆轉的,詳細介紹了恢復方法和潛在的生產環境危害。

InnoDB中的聚類索引和非簇索引(次級索引)之間的差異。 InnoDB中的聚類索引和非簇索引(次級索引)之間的差異。 Apr 02, 2025 pm 06:25 PM

聚集索引和非聚集索引的區別在於:1.聚集索引將數據行存儲在索引結構中,適合按主鍵查詢和範圍查詢。 2.非聚集索引存儲索引鍵值和數據行的指針,適用於非主鍵列查詢。

說明不同類型的MySQL索引(B樹,哈希,全文,空間)。 說明不同類型的MySQL索引(B樹,哈希,全文,空間)。 Apr 02, 2025 pm 07:05 PM

MySQL支持四種索引類型:B-Tree、Hash、Full-text和Spatial。 1.B-Tree索引適用於等值查找、範圍查詢和排序。 2.Hash索引適用於等值查找,但不支持範圍查詢和排序。 3.Full-text索引用於全文搜索,適合處理大量文本數據。 4.Spatial索引用於地理空間數據查詢,適用於GIS應用。

See all articles