MySQL的Auto-Failover功能

今天来体验一下MySQL的Auto-Failover功能,这里用到一个工具MySQL Utilities,它的功能很强大.此工具提供如下功能:
(1)管理工具 (克隆、复制、比较、差异、导出、导入)
(2)复制工具 (安装、配置)
(3)一般工具 (磁盘使用情况、冗余索引、搜索元数据)
而我们用它来实现Master-Slave的自动Failover,下面开始

Master:192.168.13.194
Slave:192.168.13.159
此Failover需要建立在GTID的基础上所以MySQL版本必须5.6即以上

先安装mysql-utilities
sudo yum install mysql-utilities

在my.cnf添加GTID参数(主从都要添加)

binlog-format = ROW
log-slave-updates = true
gtid-mode = on
enforce-gtid-consistency = true
report-host = 192.168.13.194
report-port = 3306
master-info-repository = TABLE
relay-log-info-repository = TABLE
sync-master-info = 1

在任意一个节点(此命令会自动完成主从配置)
[[email protected] ~]$  mysqlreplicate --master=root:[email protected]‘192.168.13.194‘:3306 --slave=root:[email protected]‘192.168.13.159‘:3306 --rpl-user=root:XXXX
# master on 192.168.13.194: ... connected.
# slave on 192.168.13.159: ... connected.
# Checking for binary logging on master...
# Setting up replication...
# ...done.

[[email protected] ~]$ mysql -uroot -pXXXX -h‘192.168.13.194‘ -e ‘show databases;‘
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ss_test            |
| mysql              |
| performance_schema |
| pre_test_market    |
| test               |
+--------------------+
[[email protected] ~]$ mysql -uroot -pXXXX -h‘192.168.13.159‘ -e ‘show databases;‘
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ss_test            |
| mysql              |
| performance_schema |
| pre_test_market    |
| test               |
+--------------------+

查看主从结构
[[email protected] ~]$ mysqlrplshow --master=root:[email protected]‘192.168.13.194‘:3306 --discover-slaves-login=root:XXXX;
# master on 192.168.13.194: ... connected.
# Finding slaves for master: 192.168.13.194:3306

# Replication Topology Graph
192.168.13.194:3306 (MASTER)
   |
   +--- 192.168.13.159:3306 - (SLAVE)

查看主从状态
[[email protected]host ~]$ mysqlrplcheck --master=root:[email protected]‘192.168.13.194‘ --slave=root:[email protected]‘192.168.13.159‘
# master on 192.168.13.194: ... connected.
# slave on 192.168.13.159: ... connected.
Test Description                                                     Status
---------------------------------------------------------------------------
Checking for binary logging on master                                [pass]
Are there binlog exceptions?                                         [WARN]

+---------+---------------------------------------+------------+
| server  | do_db                                 | ignore_db  |
+---------+---------------------------------------+------------+
| master  | pre_test_market,test,ss_test          |            |
| slave   | pre_test_market,test,ss_test          |            |
+---------+---------------------------------------+------------+

Replication user exists?                                             [pass]
Checking server_id values                                            [pass]
Checking server_uuid values                                          [pass]
Is slave connected to master?                                        [pass]
Check master information file                                        [pass]
Checking InnoDB compatibility                                        [pass]
Checking storage engines compatibility                               [pass]
Checking lower_case_table_names settings                             [pass]
Checking slave delay (seconds behind master)                         [pass]
# ...done.

准备Failover(注意:mysqlfailover的时候有时候Failover Mode = fail,正常应该是auto解决此错误加参数--force)
例子如下:
[[email protected] ~]$ mysqlfailover --master=root:[email protected]‘192.168.13.194‘:3306 --discover-slaves-login=root:XXXX --rediscover
# Discovering slaves for master at 192.168.13.194:3306
# Discovering slave at 192.168.13.159:3306
# Found slave: 192.168.13.159:3306
Multiple instances of failover console found for master 192.168.13.194:3306.
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.
# Checking privileges.
# Discovering slaves for master at 192.168.13.194:3306

MySQL Replication Failover Utility
Failover Mode = fail     Next Interval = Mon Jun  9 23:04:22 2014

正常模式:
[[email protected] ~]$ mysqlfailover --master=root:[email protected]‘192.168.13.194‘:3306 --discover-slaves-login=root:XXXX --rediscover 
# Discovering slaves for master at 192.168.13.194:3306
# Discovering slave at 192.168.13.159:3306
# Found slave: 192.168.13.159:3306
# Checking privileges.
# Discovering slaves for master at 192.168.13.194:3306

MySQL Replication Failover Utility
Failover Mode = auto     Next Interval = Mon Jun  9 23:56:32 2014

Master Information
------------------
Binary Log File   Position  Binlog_Do_DB                          Binlog_Ignore_DB  
mysql-bin.000041  191       pre_test_market,test,ss_test

GTID Executed Set
a7e4c60d-62ca-11e3-8710-080027e08a30:1-8

Replication Health Status
+-----------------+-------+---------+--------+------------+---------+
| host            | port  | role    | state  | gtid_mode  | health  |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.13.194  | 3306  | MASTER  | UP     | ON         | OK      |
| 192.168.13.159  | 3306  | SLAVE   | UP     | ON         | OK      |
+-----------------+-------+---------+--------+------------+---------+

关闭Master1
[[email protected] ~]$ mysqladmin -u root -proot shutdown
Warning: Using a password on the command line interface can be insecure.

开始自动Failover

Q-quit R-refresh H-health G-GTID Lists U-UUIDs
140609 23:30:22 mysqld_safe mysqld from pid file /mysql/mysqld.pid ended
Failed to reconnect to the master after 3 attemps.

Failover starting in ‘auto‘ mode...
# Candidate slave 192.168.13.159:3306 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 192.168.13.159:3306
Error connecting to a slave as [email protected]: Cannot connect to the slave server.
Error Can‘t connect to MySQL server on ‘192.168.13.194:3306‘ (111 Connection refused)

WARNING: There are slaves that had connection errors.

Failover console will restart in 5 seconds.

MySQL Replication Failover Utility
Failover Mode = auto     Next Interval = Mon Jun  9 23:31:40 2014

Master Information
------------------
Binary Log File   Position  Binlog_Do_DB                          Binlog_Ignore_DB  
mysql-bin.000051  1688      pre_test_market,test,ss_test

GTID Executed Set
a7e4c60d-62ca-11e3-8710-080027e08a30:1-8

Replication Health Status
+-----------------+-------+---------+--------+------------+---------+
| host            | port  | role    | state  | gtid_mode  | health  |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.13.159  | 3306  | MASTER  | UP     | ON         | OK      |
+-----------------+-------+---------+--------+------------+---------+
切换成功.Slave正式切换到Master

恢复Master1
[[email protected] ~]$ mysqld_safe &
140609 23:32:04 mysqld_safe Logging to ‘/mysql/mysqld.log‘.
140609 23:32:04 mysqld_safe Starting mysqld daemon with databases from /mysql/data

把原Master1从新添加
[[email protected] ~]$ mysqlreplicate --master=root:[email protected]‘192.168.13.159‘:3306 --slave=root:[email protected]‘192.168.13.194‘:3306 --rpl-user=root:XXXX
# master on 192.168.13.159: ... connected.
# slave on 192.168.13.194: ... connected.
# Checking for binary logging on master...
# Setting up replication...
# ...done.

MySQL Replication Failover Utility
Failover Mode = auto     Next Interval = Mon Jun  9 23:34:04 2014

Master Information
------------------
Binary Log File   Position  Binlog_Do_DB                          Binlog_Ignore_DB  
mysql-bin.000051  1688      pre_test_market,test,ss_test

GTID Executed Set
a7e4c60d-62ca-11e3-8710-080027e08a30:1-8

Replication Health Status
+-----------------+-------+---------+--------+------------+---------+
| host            | port  | role    | state  | gtid_mode  | health  |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.13.159  | 3306  | MASTER  | UP     | ON         | OK      |
| 192.168.13.194  | 3306  | SLAVE   | UP     | ON         | OK      |
+-----------------+-------+---------+--------+------------+---------+

主从切换恢复原状
[[email protected] ~]$ mysqlrpladmin --master=root:[email protected]‘192.168.13.159‘:3306 --new-master=root:[email protected]‘192.168.13.194‘:3306 --demote-master --discover-slaves-login=root:XXXX switchover
# Discovering slaves for master at 192.168.13.159:3306
# Discovering slave at 192.168.13.194:3306
# Found slave: 192.168.13.194:3306
# Checking privileges.
# Performing switchover from master at 192.168.13.159:3306 to slave at 192.168.13.194:3306.
# Checking candidate slave prerequisites.
# Checking slaves configuration to master.
# Waiting for slaves to catch up to old master.
# Stopping slaves.
# Performing STOP on all slaves.
# Demoting old master to be a slave to the new master.
# Switching slaves to new master.
# Starting all slaves.
# Performing START on all slaves.
# Checking slaves for errors.
# Switchover complete.
#
# Replication Topology Health:
+-----------------+-------+---------+--------+------------+---------+
| host            | port  | role    | state  | gtid_mode  | health  |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.13.194  | 3306  | MASTER  | UP     | ON         | OK      |
| 192.168.13.159  | 3306  | SLAVE   | UP     | ON         | OK      |
+-----------------+-------+---------+--------+------------+---------+
# ...done.

由于我们从新恢复了.所以Failover交互模式失效从新在进入恢复正常
MySQL Replication Failover Utility
Failover Mode = auto     Next Interval = Mon Jun  9 23:35:35 2014

Master Information
------------------
Binary Log File   Position  Binlog_Do_DB                          Binlog_Ignore_DB  
mysql-bin.000051  1688      pre_test_market,test,ss_test

GTID Executed Set
a7e4c60d-62ca-11e3-8710-080027e08a30:1-8

Replication Health Status
+-----------------+-------+---------+--------+------------+------------------------------------+
| host            | port  | role    | state  | gtid_mode  | health                             |
+-----------------+-------+---------+--------+------------+------------------------------------+
| 192.168.13.159  | 3306  | MASTER  | UP     | ON         | OK                                 |
| 192.168.13.194  | 3306  | SLAVE   | WARN   |            | Slave is not connected to master.  |
+-----------------+-------+---------+--------+------------+------------------------------------+

[[email protected] ~]$ mysqlfailover --master=root:[email protected]‘192.168.13.194‘:3306 --discover-slaves-login=root:XXXX --rediscover --force
# Discovering slaves for master at 192.168.13.194:3306
# Discovering slave at 192.168.13.159:3306
# Found slave: 192.168.13.159:3306
# Checking privileges.
# Discovering slaves for master at 192.168.13.194:3306

MySQL Replication Failover Utility
Failover Mode = auto     Next Interval = Mon Jun  9 23:36:10 2014

Master Information
------------------
Binary Log File   Position  Binlog_Do_DB                          Binlog_Ignore_DB  
mysql-bin.000041  191       pre_test_market,test,ss_test

GTID Executed Set
a7e4c60d-62ca-11e3-8710-080027e08a30:1-8

Replication Health Status
+-----------------+-------+---------+--------+------------+---------+
| host            | port  | role    | state  | gtid_mode  | health  |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.13.194  | 3306  | MASTER  | UP     | ON         | OK      |
| 192.168.13.159  | 3306  | SLAVE   | UP     | ON         | OK      |
+-----------------+-------+---------+--------+------------+---------+

最后简单的说几句:
Auto Failover的前提首先是MySQL必须是5.6并且必须启动GTID.

Master Crash之后可以自动把Slave提升为New Master,但是Old Master恢复之后需要手动添加进来.并且Failover交互模式也会失效需要从新进一次显示才能正常.今天先到此了..^_^

时间: 2024-10-09 10:58:22

MySQL的Auto-Failover功能的相关文章

mysql记录集中记录序号,MySQL中实现rownum功能类似的语句(转)

MySQL 如何实现 Oracle 的 ROWNUM mysql> SELECT * FROM frutas;+-----------+----------+| nombre    | color    |+-----------+----------+| fresa     | rojo     || platano   | amarillo || manzana   | verde    || uva       | verde    || pera      | verde    || m

初识 MySQL 5.6 新功能、参数

摘要: 继上一篇的文章 初识 MySQL 5.5 新功能.参数 之后,现在MySQL5.6 针对 MySQL5.5 各个方面又提升了很多,特别在性能和一些新参数上面,现在看看大致提升了哪些方面(后续不定时更新). 一:性能.功能上的提升. ① 在线DDL即 online DDL,日常的增删字段和索引都不会出现问题,但还是有很多操作不支持完全的在线DDL,包括增加一个全文索引,修改列的数据类型,删除一个主键,修改表的字符集等,其中主键可以通过自己指定的方式进行操作,操作方式有2种:algorith

MySQL的预编译功能

MySQL的预编译功能 预编译的好处 大家平时都使用过JDBC中的PreparedStatement接口,它有预编译功能.什么是预编译功能呢?它有什么好处呢? 当客户发送一条SQL语句给服务器后,服务器总是需要校验SQL语句的语法格式是否正确,然后把SQL语句编译成可执行的函数,最后才是执行SQL语句.其中校验语法,和编译所花的时间可能比执行SQL语句花的时间还要多. 如果我们需要执行多次insert语句,但只是每次插入的值不同,MySQL服务器也是需要每次都去校验SQL语句的语法格式,以及编译

mysql router 自动failover测试

[[email protected] bin]# cat /etc/mysqlrouter/mysqlrouter.ini [DEFAULT]#mkdir -p /var/log/mysql-router logging_folder = /var/log/mysql-router plugin_folder = /usr/local/mysql-router/lib/mysqlrouter runtime_folder = /usr/local/mysql-router [logger] #

MySQL 5.7.12新增MySQL Shell命令行功能

在最新发布的MySQL 5.7.12中有许多令人兴奋的新功能,对于MySQL开发者来说,最令人兴奋的莫不是新增的MySQL Shell了,其下载地址: http://dev.mysql.com/downloads/shell/ 有了MySQL Shell,我们可以将MySQL作为一个文档数据库来使用,也可以通过该工具调用Java.Python. 我们可以用mysqlsh --help来查看其帮助信息: 使用MySQL Shell的几个必要条件: 1.MySQL版本:5.7.12及以上 2.安装m

Oracle数据库中实现mysql数据库中auto-increment功能

在Mysql数据库中,想要实现一条数据的自增一功能(即插入此数据时填写null即可,系统自动+1),可直接在所在列使用语句auto-increment. id int primary key auto_increment 而在Oracle数据库中不可这样使用.下面介绍怎样在Oracle数据库中实现对id列的自增一的功能. 1 创建一张表cutomers.(无需多言) 1 create table customers( 2 id number(10), 3 name varchar(30) not

mysql事件调度器功能

一.前言 自MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作系统的计划任务来执行的工作.更值得 一提的是MySQL的事件调度器可以精确到每秒钟执行一个任务,而操作系统的计划任务(如:Linux下的CRON或Windows下的任务计划)只能精 确到每分钟执行一次.对于一些对数据实时性要求比较高的应用(例如:股票.赔率.比分等)就非常适合. 事件调度器有时也可称为

android+json+php+mysql实现用户反馈功能

相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流.首先看具体界面,三个字段.名字,邮箱为选填,可以为空,建议不能为空.如有需要可以给我留言. 下面贴出布局代码,这里用到一个<include layout="@layout/uphead">就是把另外一个布局文件引入到这个布局中. <?xml version="1.0" encoding="utf-8"?> <LinearLayo

MySQL Flashback 闪回功能详解

阅读目录 1. 简介 2. 闪回原理 3. flashback安装 4. 使用简介 5. Flashback工具使用注意点 回到顶部 1. 简介 mysqlbinlog flashback(闪回)用于快速恢复由于误操作丢失的数据.在DBA误操作时,可以把数据库恢复到以前某个时间点(或者说某个binlog的某个pos).比如忘了带where条件的update.delete操作,传统的恢复方式是利用全备+二进制日志前滚进行恢复,相比于传统的全备+增备,flashback显然更为快速.简单. 目前My

MySQL控制软件及GTID功能介绍

MySQL 5.6 版本引入了GTID功能,使得MySQL在主从复制数据一致性和数据安全方面有了巨大的提升,然而对于已经存在的基于binlog+position方式复制的MySQL集群,想要开启GTID,必须要停业务,重启MySQL,这一过程是很不友好的. 首先从重启MySQL来介绍,开启.关闭.重启MySQL的方式有很多,其中最方便的方法如下: 打开iis7服务器监控工具: 不管是重启.开启.关闭MySQL,都可以用它一键完成. 幸运地是,MySQL 5.7版本,提供了在线开启GTID的功能,