在生产环境中,我们时常要考虑到高可用性的问题,MySQL复制是脆弱的,诸多原因会导致复制停止。有多种方案来解决MySQL高可用性问题,如DRBD、MMM等等。监视复制拓扑,在故障发生时,自动进行切换,最大限度的减少停机时间保持复制运行。
mysqlfailover工具来监视复制拓扑并在需要时自动执行故障转移。当当前的主出现故障时,人工切换主是非常繁琐和容易出现问题的,需要将所有的从指向新的主,确保不丢失任何事务。mysqlfailover 工具能够自动的执行这一全过程。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
假设有5台服务器,主server1:3311,从分别是server2:3312, server3:3313, server4:3314, server:3315。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
实例
启动mysqlfailover工具文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
shell> mysqlfailover --master=root@server1:3311 \ --slaves=root@server2:3312,root@server3:3313,root@server4:3314,root@server5:3315 \ --log=log.txt --rpl-user=rpl:rpl NOTE: Log file 'log.txt' does not exist. Will be created. # Checking privileges. MySQL Replication Failover Utility Failover Mode = auto Next Interval = Fri Jul 26 10:17:52 2013 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB master-bin.000001 151 GTID Executed Set None Replication Health Status +----------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +----------+-------+---------+--------+------------+---------+ | server1 | 3311 | MASTER | UP | ON | OK | | server2 | 3312 | SLAVE | UP | ON | OK | | server3 | 3313 | SLAVE | UP | ON | OK | | server4 | 3314 | SLAVE | UP | ON | OK | | server5 | 3315 | SLAVE | UP | ON | OK | +----------+-------+---------+--------+------------+---------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries
主崩溃无法访问,然后在预定的时间间隔后(默认15秒),故障转移将会自动启动文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
Failover starting in 'auto' mode... # Candidate slave server2:3312 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. Failover console will restart in 5 seconds. [...] MySQL Replication Failover Utility Failover Mode = auto Next Interval = Fri Jul 26 10:25:17 2013 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB master-bin.000001 151 GTID Executed Set None Replication Health Status +----------+-------+---------+--------+------------+---------+ | host | port | role | state | gtid_mode | health | +----------+-------+---------+--------+------------+---------+ | server2 | 3312 | MASTER | UP | ON | OK | | server3 | 3313 | SLAVE | UP | ON | OK | | server4 | 3314 | SLAVE | UP | ON | OK | | server5 | 3315 | SLAVE | UP | ON | OK | +----------+-------+---------+--------+------------+---------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs L-log entries
上面的例子说明如何启动mysqlfailover ,检查复制拓扑的健康和故障转移发生时的输出信息。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
权限
用户必须要有配置复制的权限。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
小技巧
上面的例子是在控制台模式下运行的,可以放在后台,在启动时加上--daemon=start选项。还可以指定--log选项,将日志输出到日志文件中。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/
还可以指定外部脚本。--exec-fail-check 指定外部检查脚本用于替换默认的检查(主可达并且存活)。--exec-before 指定在故障切换前执行的脚本。--exec-after 在故障切换后执行的脚本。--exec-post-failover在故障转移过程完成后(显示健康报告前)执行的脚本。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/ 文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-automatic-failover/

评论