mysqlfailover 工具对复制进行健康检测和实现故障自动转移。该工具在定期的时间内进行间隔或持续的健康检测。主要任务是监视主失败,并在发送故障时候,自动执行故障切换到当前最优秀的从服务器上。
mysqlfailover工具被设计成专门为了支持全局事务标识符(GTIDs)服务器, gtid_mode=ON。因此MySQL Server版本需要5.6.5或更高来支持GTIDs。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
使用 --interval选项来定义检测主状态和产生健康报告的时间间隔。在每个时间间隔,该工具通过ping来检查服务器是否存活,随后,检查连接器来判断服务器是否可达。ping操作可以通过--ping选项控制。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
如果主被发现是离线或无法访问,该工具将执行基于--failover-mode 选项值的动作。--failover-mode 选项值有:文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
auto:执行故障自动转移到第一候选人。如果没有从可行的,继续从从列表中找到一个可行的候选者。如果没有从被认为是一个可行的候选者,该程序将生成错误并退出。一旦候选者被发现,该程序将进行故障切换到最佳从上。该命令将测试每个候选从的先决条件。一旦候选从被选中,其他从作为它的从,收集其他从的任何事务交易。这种方式,确保候选者是最新的从。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
elect:这种模式与aoto一样的,除了如果在指定的候选从列表中没有可行的,不检测剩余的从和产生错误并退出。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
fail: 这种模式会产生一个错误,当主失败后不会进行故障转移。这种模式被用来只进行定期健康监测不进行故障切换。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
所有选项允许指定对个服务器,这些选项要求以逗号分隔,格式如下:<*user*>[:<*passwd*>]@<*host*>[:<*port*>][:<*socket*>] 或者<*login-path*>[:<*port*>][:<*socket*>]文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
该工具允许用户连接到主自动发现从。为了使用自动发现从功能,所有从必需使用--report-host 和 --report-port 启动参数。如果这些缺失或报告不正确的信息,从健康可能无法正确报告或从可能不会全部列出。对于无法连接到的从自动发现从功能将被忽略。在每个间隔内发现从功能自动运行。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
该工具允许用户指定外部脚本在切换和故障转移命令之前或之后执行。用户可以通过 --exec-before 和 --exec-after 选项指定。脚本的返回码来确定是否成功执行,0表示成功,非0表示失败并返回错误信息。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
该工具还允许用户指定特定的脚本来检测主当掉或应用程序级别的事件来触发故障转移。通过--exec-fail-check选项来指定。脚本的返回码用于调用故障转移。返回码0表示故障不发生。非0表示发生。如果该脚本指定,在每个间隔开始检测。在此情况下,timeout选项没有使用。该脚本在每个间隔时间执行一次。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
该工具还允许用户记录命令期间的所有动作。--log 选项需要指定有效的路径和文件名。只有该选项指定了,该功能才生效。--log-age 选项用于指定日志存放天数,默认是7天。旧的日志自动轮滚掉。日志的格式包括的项目有事件的日期和时间、事件的级别(informational - INFO, warning - WARN, error - ERROR, critical failure - CRITICAL)和消息。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
该接口为用户提供了许多选项用于显示附加信息。用户可以查看复制健康报告(默认)或者选择查看使用GTIDs列表,使用UUIDs列表,如果启用了日志查看日志文件内容。 每个的这些报告说明如下:文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
health 显示复制拓扑状况。这份报告是接口的默认视图。默认情况下,包括主机名、端口、角色(master或slave)、服务器状态(UP = is connected, WARN = not connected but can ping, DOWN = not connected and cannot ping)、GTID_MODE和健康状态。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
主的健康状态基于下面的:如果GTID_MODE=ON,服务器必需启用二进制日志且必需存在具有REPLICATE SLAVE权限的用户。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
当从落后于主,--seconds-behind 选项用于检测。它允许用户设置阀值仅作为报告用。不适用于在故障转移时的候选从或选举。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
从的健康状态基于下面的:IO_THREAD 和 SQL_THREADS 必需运行,必需连接到主,没有错误,没有启用GTID的落后从不超过--max-position选项指定的阀值,从正在读取正确的主日志文件,从延迟不高于--seconds-behind 选项阀值。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
在每个时间间隔,在启动时如果指定了发现从选项,新的从被发现,健康的报告将被刷新。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
gtid 显示执行 GTIDs主的列表。GTID变量的内容,@@GLOBAL.GTID_EXECUTED, @@GLOBAL.GTID_PURGED, @@GLOBAL.GTID_OWNED。因此,用于可以反复按G键进行屏幕切换,将循环显示这四个屏幕。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
UUID 显示所有服务器的全局唯一标识符(UUID)。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
Log 该选项只有指定--log选项可见。显示日志文件的内容。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-utilities-mysqlfailover/
用户界面被设计成匹配它所在运行的终端窗口大小。提供刷新选项,允许用户调整其终端窗口或刷新时间。然而,界面会在每个间隔自动调整终端窗口大小。该界面显示程序名称,主服务器状态包括二进制日志文件,位置,过滤器,下一个间隔事件的日期时间。也提供向上或向下滚动查看。当时长清单时,滚动选项会启用。用户可以向上箭头键和向下箭头键滚动列表。
使用--verbose选项来查看故障转移期间的健康报告和附加信息。
该工具支持两种操作模式。默认模式是运行在控制台,另一种模式是以守护进程方式运行。以守护进程运行,该工具不具有交互性。所有的事件被写入到日志文件中,可以使用 --report-values 选项来控制。以守护进程运行需要指定--daemon选项。--daemon选项值有:
- start 启动守护进程。需要--log选项。
- stop 停止守护进程。如果有指定--pidfile选项,该值需要与start指定的一致。
- restart 重新启动守护进程。如果有指定--pidfile选项,该值需要与start指定的一致。
- nodetach 启动守护进程,但是不会从控制台分离进程。需要--log选项。
选项
Usage: mysqlfailover --master=root@localhost --discover-slaves-login=root --candidates=root@host123:3306,root@host456:3306 mysqlfailover - automatic replication health monitoring and failover Options: --version show program's version number and exit --help display this help message and exit --license display program's license and exit --candidates=CANDIDATES connection information for candidate slave servers for failover in the form: <user>[:<password>]@<host>[:<port>][:<socket>] or <login-path>[:<port>][:<socket>] or <config- path>[<[group]>] Valid only with failover command. List multiple slaves in comma-separated list.连接候选从服务器 信息,仅对故障转移命令有效。以逗号分隔的多个从。 --discover-slaves-login=DISCOVER at startup, query master for all registered slaves and use the user name and password specified to connect. Supply the user and password in the form <user>[:<password>] or <login-path>. For example, --discover-slaves-login=joe:secret will use 'joe' as the user and 'secret' as the password for each discovered slave. 在启动时,查询主上所有注册的从。 --exec-after=EXEC_AFTER name of script to execute after failover or switchover 在故障转移或切换后执行的脚本。脚本名称可以包含路径。 --exec-before=EXEC_BEFORE name of script to execute before failover or switchover --log=LOG_FILE specify a log file to use for logging messages --log-age=LOG_AGE specify maximum age of log entries in days. Entries older than this will be purged on startup. Default = 7 days. --master=MASTER connection information for master server in the form: <user>[:<password>]@<host>[:<port>][:<socket>] or <login-path>[:<port>][:<socket>] or <config- path>[<[group]>]. --max-position=MAX_POSITION used to detect slave delay. The maximum difference between the master's log position and the slave's reported read position of the master. A value greater than this means the slave is too far behind the master. Default is 0. --ping=PING Number of ping attempts for detecting downed server.默认3秒。 --seconds-behind=MAX_DELAY used to detect slave delay. The maximum number of seconds behind the master permitted before slave is considered behind the master. Default is 0. --slaves=SLAVES connection information for slave servers in the form: <user>[:<password>]@<host>[:<port>][:<socket>] or <login-path>[:<port>][:<socket>] or <config- path>[<[group]>].List multiple slaves in comma- separated list. --timeout=TIMEOUT maximum timeout in seconds to wait for each replication command to complete. For example, timeout for slave waiting to catch up to master. Default = 300. --script-threshold=SCRIPT_THRESHOLD Value for external scripts to trigger aborting the operation if result is greater than or equal to the threshold. Default = None (no threshold checking). -i INTERVAL, --interval=INTERVAL interval in seconds for polling the master for failure and reporting health. Default = 15 seconds. Lowest value is 5 seconds. -f FAILOVER_MODE, --failover-mode=FAILOVER_MODE action to take when the master fails. 'auto' = automatically fail to best slave, 'elect' = fail to candidate list or if no candidate meets criteria fail, 'fail' = take no action and stop when master fails. Default = 'auto'. --exec-fail-check=EXEC_FAIL name of script to execute on each interval to invoke failover --force override the registration check on master for multiple instances of the console monitoring the same master. --exec-post-failover=EXEC_POST_FAIL name of script to execute after failover is complete and the utility has refreshed the health report. -p, --pedantic fail if some inconsistencies are found (e.g. errant transactions on slaves).在检查期间如果发现有不一致的情况 (从错误的事务或SQL线程错误)阻止故障转移。 --no-keyboard start with no keyboard input support. --daemon=DAEMON run on daemon mode. It can be start, stop, restart or nodetach. --pidfile=PIDFILE pidfile for running mysqlfailover as a daemon. --report-values=REPORT_VALUES report values used in mysqlfailover running as a daemon. It can be health, gtid or uuid. Multiple values can be used separated by commas. The default is health. -v, --verbose control how much information is displayed. e.g., -v = verbose, -vv = more verbose, -vvv = debug --rpl-user=RPL_USER the user and password for the replication user requirement, in the form: <user>[:<password>] or <login-path>. E.g. rpl:passwd --ssl-ca=SSL_CA The path to a file that contains a list of trusted SSL CAs. --ssl-cert=SSL_CERT The name of the SSL certificate file to use for establishing a secure connection. --ssl-key=SSL_KEY The name of the SSL key file to use for establishing a secure connection.
注意事项
工具检查服务器和监控状态必需具有相应的权限,如SHOW SLAVE STATUS, SHOW MASTER STATUS。同时,还需要执行故障切换的权限,如STOP SLAVE, START SLAVE, WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS, CHANGE MASTER TO...。还必需要有 REPLICATE SLAVE权限。为了执行成功,主和从需要同样的权限。用户连接到从、候选者和主需要SUPER, GRANT OPTION, REPLICATION SLAVE, RELOAD, DROP, CREATE, INSERT 和 SELECT权限。
故障转移初始化主或新主需要DROP, CREATE, INSERT 和 SELECT权限。任何从成为新主,从,候选者需要同样的权限。该工具在启动时检查主、从和候选者权限。
在启动时,控制台将尝试与主注册。如果另一控制台已经注册了,切换模式为自动或选举,该控制台运行故障转移将被阻止。当一个控制台退出,将从主撤销注册。
IP和主机名混合是不推荐的。复制工具将尝试比较主机名和IP地址作为别名来检查从连接到主。但是,如果不支持反向名称查询,比较可能会失败的。如果没有反向解析能力,复制工具将报告从连接不上主。
控制台将在mysql数据库下创建一个特殊的表来跟踪记录哪些实例与主联系。如果使用--force选项,控制台将删除该表中的行。表结构如下:
CREATE TABLE IF NOT EXISTS mysql.failover_console (host char(30), port char(10))
当控制台启动时,主的主机名和端口将被插入。在启动时,如果行匹配这些值,控制台无法启动。如果使用--force选项,改行将被删除。
当运行该工具是指定了 --daemon=nodetach 选项,--pidfile选项可以省略。如果使用的话也被忽略的。
当使用外部脚本时,参数传递的顺序如下。例如,假设你的脚本名为 'run_before.sh' ,想要在故障切换前执行。假设主是host1,端口3306,新主是can_host2,端口3308。调用方法如下:
% run_before.sh host1 3306 can_host2 3308
外部脚本参数:
MySQL Failover Option | Parameters Passed to External Script |
---|---|
--exec-before |
master host, master port, candidate host, candidate port |
--exec-after |
new master host, new master port |
--exec-fail-check |
master host, master port |
--exec-post-failover (no errors during failover) |
old master host, old master port, new master host, new master port |
--exec-post-failover (errors during failover) |
old master host, old master port |
实例
要启动该程序,必需至少指定--master选项和或者 --discover-slaves-login 或者--slaves选项。
默认的界面显示如下的复制健康报告,该例子中日志文件被启用。启动命令如下:
shell> mysqlfailover --master=root@localhost:3331 --discover-slaves-login=root --log=log.txt MySQL Replication Monitor and Failover Utility Failover Mode = auto Next Interval = Mon Mar 19 15:56:03 2012 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 571 GTID Executed Set 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 [...] Replication Health Status +------------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +------------+-------+---------+--------+------------+---------+ | localhost | 3331 | MASTER | UP | ON | OK | | localhost | 3332 | SLAVE | UP | ON | OK | | localhost | 3333 | SLAVE | UP | ON | OK | | localhost | 3334 | SLAVE | UP | ON | OK | +------------+-------+---------+--------+------------+---------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries
按“Q”键退出,按“R”键刷新,“H”键将返回到复制健康报告。如果用户按“G”键,GTID报告如下所示,第一页显示主执行的GTID设置:
MySQL Replication Monitor and Failover Utility Failover Mode = auto Next Interval = Mon Mar 19 15:59:33 2012 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 571 GTID Executed Set 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 [...] Master GTID Executed Set +-------------------------------------------+ | gtid | +-------------------------------------------+ | 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 | | 5503D37E-2DB2-11E2-A781-8077D4C14B33:1-3 | +-------------------------------------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries Up|Down-scroll
如果用户继续按下“G”键,将循环显示GTID列表。如果列表长于屏幕大小,将上下滚动显示。在这种情况下,用户可以按上下箭头进行上下滚动。
如果用户按“U”键,显示拓扑结构中使用的UUID列表,如下所示:
MySQL Replication Monitor and Failover Utility Failover Mode = auto Next Interval = Mon Mar 19 16:02:34 2012 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 571 GTID Executed Set 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 [...] UUIDs +------------+-------+---------+---------------------------------------+ | host | port | role | uuid | +------------+-------+---------+---------------------------------------+ | localhost | 3331 | MASTER | 55c65a00-71fd-11e1-9f80-ac64ef85c961 | | localhost | 3332 | SLAVE | 5dd30888-71fd-11e1-9f80-dc242138b7ec | | localhost | 3333 | SLAVE | 65ccbb38-71fd-11e1-9f80-bda8146bdb0a | | localhost | 3334 | SLAVE | 6dd6abf4-71fd-11e1-9f80-d406a0117519 | +------------+-------+---------+---------------------------------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries
如果一旦主被检测到当掉了,故障切换模式为自动或选举,并有可行的候选从,故障转移功能将会自动启动,用户会看到故障转移的信息。当故障切换完成后,5秒后界面返回复制健康监控信息。如下显示故障发生的实例:
Failover starting... # Candidate slave localhost:3332 will become the new master. # Preparing candidate for failover. # Creating replication user if it does not exist. # Stopping slaves. # Performing STOP on all slaves. # Switching slaves to new master. # Starting slaves. # Performing START on all slaves. # Checking slaves for errors. # Failover complete. # Discovering slaves for master at localhost:3332 Failover console will restart in 5 seconds.
在故障转移事件后,复制健康报告显示新的拓扑,如下所示:
MySQL Replication Monitor and Failover Utility Failover Mode = auto Next Interval = Mon Mar 19 16:05:12 2012 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 1117 GTID Executed Set 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 [...] UUIDs +------------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +------------+-------+---------+--------+------------+---------+ | localhost | 3332 | MASTER | UP | ON | OK | | localhost | 3333 | SLAVE | UP | ON | OK | | localhost | 3334 | SLAVE | UP | ON | OK | +------------+-------+---------+--------+------------+---------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries
如果用户按L键并指定了--log选项,将显示日志条目。如下所示:
MySQL Replication Monitor and Failover Utility Failover Mode = auto Next Interval = Mon Mar 19 16:06:13 2012 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 1117 GTID Executed Set 2A67DE00-2DA1-11E2-A711-00764F2BE90F:1-7 [...] Log File +-------------------------+----------------------------------------- ... --+ | Date | Entry ... | +-------------------------+----------------------------------------- ... --+ | 2012-03-19 15:55:33 PM | INFO Failover console started. ... | | 2012-03-19 15:55:33 PM | INFO Failover mode = auto. ... | | 2012-03-19 15:55:33 PM | INFO Getting health for master: localhos ... | | 2012-03-19 15:55:33 PM | INFO Master status: binlog: mysql-bin.00 ... | +-------------------------+----------------------------------------- ... --+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries Up|Down-scroll\
权限
用户必须有监控服务器和故障切换配置复制的权限。在主和从上运行mysqlfailover 注册和注销的权限。
具体来说需要的权限有: SUPER, GRANT OPTION, REPLICATION SLAVE, RELOAD, DROP, CREATE, INSERT, 和 SELECT。这些权限需要在所有服务器(主、从、候选者)上授予。

评论