今天来体验一下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

在任意一个节点(此命令会自动完成主从配置)
[mysql@localhost ~]$  mysqlreplicate --master=root:XXXX@'192.168.13.194':3306 --slave=root:XXXX@'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.

[mysql@localhost ~]$ 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               |
+--------------------+
[mysql@localhost ~]$ 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               |
+--------------------+

查看主从结构
[mysql@localhost ~]$ mysqlrplshow --master=root:XXXX@'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)

查看主从状态
[mysql@localhost ~]$ mysqlrplcheck --master=root:XXXX@'192.168.13.194' --slave=root:XXXX@'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)
例子如下:
[mysql@localhost ~]$ mysqlfailover --master=root:XXXX@'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

正常模式:
[mysql@localhost ~]$ mysqlfailover --master=root:XXXX@'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
[mysql@master1 ~]$ 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 root@192.168.13.194: 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
[mysql@master1 ~]$ 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从新添加
[mysql@master1 ~]$ mysqlreplicate --master=root:XXXX@'192.168.13.159':3306 --slave=root:XXXX@'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      |
+-----------------+-------+---------+--------+------------+---------+

主从切换恢复原状
[mysql@master1 ~]$ mysqlrpladmin --master=root:XXXX@'192.168.13.159':3306 --new-master=root:XXXX@'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.  |
+-----------------+-------+---------+--------+------------+------------------------------------+

[mysql@localhost ~]$ mysqlfailover --master=root:XXXX@'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交互模式也会失效需要从新进一次显示才能正常.今天先到此了..^_^

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

  1. 初识 MySQL 5.6 新功能、参数

    摘要: 继上一篇的文章 初识 MySQL 5.5 新功能.参数 之后,现在MySQL5.6 针对 MySQL5.5 各个方面又提升了很多,特别在性能和一些新参数上面,现在看看大致提升了哪些方面(后续不 ...

  2. 有关mysql实现oracle分析函数功能的方法

    目前公司erp开发有一个脚本需求:对于收款合同审批单和收款合同(n:1),需要获取收款审批单中最新的一条审批记录来更新其对应的收款合同的相关信息. 难点主要在对相同类别的属性进行分组然后组内排序(分组 ...

  3. python操作三大主流数据库(6)python操作mysql⑥新闻管理后台功能的完善(增、ajax异步删除新闻、改、查)

    python操作mysql⑥新闻管理后台功能的完善(增.删.改.查)安装表单验证D:\python\python_mysql_redis_mongodb\version02>pip instal ...

  4. mysql慢查询日志功能的使用

    作用:mysql慢查询日志可监控有效率问题的SQL .. 一.开启mysql慢查询日志功能 1.查看是否开启 未使用索引的SQL记录日志查询 mysql> show variables like ...

  5. MySQL常用存储引擎功能与用法详解

    本文实例讲述了MySQL常用存储引擎功能与用法. MySQL存储引擎主要有两大类: 1. 事务安全表:InnoDB.BDB. 2. 非事务安全表:MyISAM.MEMORY.MERGE.EXAMPLE ...

  6. PHP+mysql数据库开发搜索功能:中英文分词+全文检索(MySQL全文检索+中文分词(SCWS))

    PHP+mysql数据库开发类似百度的搜索功能:中英文分词+全文检索 中文分词: a)   robbe PHP中文分词扩展: http://www.boyunjian.com/v/softd/robb ...

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

    在Mysql数据库中,想要实现一条数据的自增一功能(即插入此数据时填写null即可,系统自动+1),可直接在所在列使用语句auto-increment. id int primary key auto ...

  8. MySQL 5.7.12新增MySQL Shell命令行功能

      在最新发布的MySQL 5.7.12中有许多令人兴奋的新功能,对于MySQL开发者来说,最令人兴奋的莫不是新增的MySQL Shell了,其下载地址: http://dev.mysql.com/d ...

  9. mysql router 自动failover测试

    mysql router 启动服务文件内容: [root@monitor mysqlrouter]# cat /etc/init.d/mysqlrouter#! /bin/bash## mysqlro ...

  10. MySQL触发器之审计功能(转)

    [导读] 最近ITPUB技术论坛特意组织网络性讨论活动,关于数据库审计的话题,分享各自公司如何实现数据库审计.个人经验和构想,以及数据库审计的技巧,刚好有网友发了一个典型的审计需求,要帮他分析,以及教 ...

随机推荐

  1. IIS错误提示:另一个程序正在使用此文件 进程无法访问

    在IIS管理中,启动一个配置好的网站时,提示:另一个程序正在使用此文件 进程无法访问 原因:网站绑定端口被占用 解决办法:更换绑定端口或者将占用此端口的程序关掉即可

  2. CopyOnWriteArrayList、CopyOnWriteArraySet、ConcurrentHashMap的实现原理和适用场景

    ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储的,同步Map在同步的 ...

  3. Java Servlet开发的轻量级MVC框架最佳实践

    在Servlet开发的工程实践中,为了减少过多的业务Servlet编写,会采用构建公共Servlet的方式,通过反射来搭建轻量级的MVC框架,从而加快应用开发. 关于Servlet开发的基础知识,请看 ...

  4. Entity Framework Plus

    ZZZ Project 这家外国公司,有很多关于.NET和数据访问的项目,有收费的,有开源的,我之前介绍过 Z.ExtensionMethods 一个强大的开源扩展库 就出自该名下,其他有 如下 1. ...

  5. Nginx多进程高并发、低时延、高可靠机制缓存代理中的应用

    1. 开发背景 现有开源缓存代理中间件有twemproxy.codis等,其中twemproxy为单进程单线程模型,只支持memcache单机版和redis单机版,都不支持集群版功能. 由于twemp ...

  6. sql知识收集

    在SQL Server里面有top关键字可以很方便的取出前N条记录,但是Oracle里面却没有top的使用,类似实现取出前N条记录的简单方法如下: 方法1:利用ROW_NUMBER函数 取出前5条记录 ...

  7. NOI Linux的安装说明以及使用指南

    安装 本人的安装环境为Win10. 1. 首先从官网上下载一个CCF官方提供的Noi linux虚拟机以及安装文档 传送门 2. 然后,安装一个VMware Workstation 14 Pro,这里 ...

  8. Css设置img属性让图片水平居中/居左/居右的写法

    图片的居中显示css有很多方法,但在很多情况下有的方法无效,无意发现这个系统的官方处理图片居中,居左,居右的css写法,喜欢的朋友可以收藏下哦 图片的居中显示css有很多方法,但在很多情况下有的方法无 ...

  9. Lucene6.6.0 案例与学习路线

    之前在学习Lucene这个全文检索工具,为项目搜索引擎的开发打下基础.在这里先分享一下关于Lucene的学习心得. 核心的学习流程是:索引文件格式--索引创建过程--检索流程. 1.首先建议参看这篇精 ...

  10. POJ - 2240 Arbitrage(Bellman-Ford)

    https://vjudge.net/problem/POJ-2240 题意 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 分析 Bellman-Ford判断正环,注意初始 ...