https://www.percona.com/blog/2015/12/02/gtid-failover-with-mysqlslavetrx-fix-errant-transactions/

使用GTID复制时,Errant transactions是一个主要问题。 虽然这不是什么新鲜事,但GTID的缺点比常规复制更为臭名昭着。
错误的事务让您感到困难的是一个常见的DBA任务:故障转移。 现在像MHA这样的工具已经支持GTID复制(从0.56版本开始),这个协议变得越来越流行了,错误事务也是如此。 幸运的是,修复就像向空缺事务的数据库注入一个空的事务一样简单。 您可以轻松地通过master做到这一点,它会传播给所有的slaves。
我们来考虑以下情况:

当master吹到空中并且不在画面中时会发生什么?
     当不只有一个而是几十个错误的事务时会发生什么?
     当你有大量的slaves时会发生什么?

事情开始变得更复杂一点。

第一种情况的附注:当你的master不在时,你怎么找到错误的事务? 那么,你不能。 在这种情况下,你应该检查你的slave和你以前的slave/即将成为master之间的错误事务。

让我们考虑替代品。 什么是每个单一的slave注入每个错误的事务空交易的解决方法是什么? MySQL实用程序mysqlslavetrx。 基本上,这个工具允许我们在一个步骤中跳过多个slave上的多个事务。

安装MySQL实用程序的一种方法是执行以下步骤:

  • wget http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.6.2.tar.gz
  • tar -xvzf mysql-utilities-1.6.2.tar.gz
  • cd mysql-utilities-1.6.2
  • python ./setup.py build
  • sudo python ./setup.py install

你准备好了

那么一些例子呢? 假设我们有一个GTID复制的主/从服务器,当前状态如下:

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 | 530      |              |                  | 66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> show slave statusG
...
Executed_Gtid_Set: 66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2
Auto_Position: 1
1 row in set (0.00 sec)

向新的模式添加混乱的slave:

mysql> create database percona;
Query OK, 1 row affected (0.00 sec)
 
Now we have an errant transaction!!!!!
 
The slave status looks different:
 
mysql> show slave statusG
...
Executed_Gtid_Set: 66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2,
674a625e-976e-11e5-a8fb-125cab082fc3:1
Auto_Position: 1
1 row in set (0.00 sec)
 
通过使用GTID_SUBSET函数,我们可以确认事情从“好”到“不好”:
 
 
Before:
mysql> select gtid_subset('66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2','66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2') as is_subset;
+-----------+
| is_subset |
+-----------+
| 1         |
+-----------+
1 row in set (0.00 sec)
 
After:
mysql> select gtid_subset('66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2,674a625e-976e-11e5-a8fb-125cab082fc3:1','66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2') as is_subset;
+-----------+
| is_subset |
+-----------+
| 0         |
+-----------+
1 row in set (0.00 sec)
 
好吧,这是一团糟,明白了。 什么是错误的事务? GTID_SUBTRACT函数会告诉我们:
mysql> select gtid_subtract('66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2,674a625e-976e-11e5-a8fb-125cab082fc3:1','66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2') as errand;
+----------------------------------------+
| errand                                 |
+----------------------------------------+
| 674a625e-976e-11e5-a8fb-125cab082fc3:1 |
+----------------------------------------+
1 row in set (0.00 sec)
 
这个函数有两个参数,第一个写 show slave status 查出的所有Executed_Gtid_Set, 第二个参数写Executed_Gtid_Set 中正常的事务,可参考Retrieved_Gtid_Set
 
解决这个问题的经典方法是注入一个空的事务:
mysql> SET GTID_NEXT='674a625e-976e-11e5-a8fb-125cab082fc3:1';
Query OK, 0 rows affected (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GTID_NEXT='AUTOMATIC';
Query OK, 0 rows affected (0.00 sec)
 
在此之后,错误的事务将不再是错误的。
 
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                                                                |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| mysql-bin.000002 | 715      |              |                  | 66fbd3be-976e-11e5-a8fb-1256731a26b7:1-2,
674a625e-976e-11e5-a8fb-125cab082fc3:1                                                                                                             |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
好吧,让我们再添加一个slave。 现在是mysqlslavetrx工具变得非常方便的时刻。
 

What you need to know is:

  • The slave’s IP address
  • The GTID set

It will be simple to execute:

mysqlslavetrx --­­gtid­-set=6aa9a742­8284­11e5­a09b­12aac3869fc9:1­­ --verbose ­­--slaves=user:password@172.16.1.143:3306,user:password@172.16.1.144

gtid-set 写 所有slave上select gtid_subtract 查出来的事务

详细的输出将看起来像这样:

# GTID set to be skipped for each server:
# ­- 172.16.1.143@3306: 6aa9a742­8284­11e5­a09b­12aac3869fc9:1
# ­- 172.16.1.144@3306: 6aa9a742­8284­11e5­a09b­12aac3869fc9:1 #
# Injecting empty transactions for '172.16.1.143:3306'...
# ­- 6aa9a742­8284­11e5­a09b­12aac3869fc9:1
# Injecting empty transactions for '172.16.1.144:3306'...
# ­- 6aa9a742­8284­11e5­a09b­12aac3869fc9:1 #
#...done.
#

你可以从任何地方(master或slave)运行mysqlslavetrx。 您只需确保用户名和密码是有效的,并具有设置gtid_next变量所需的SUPER权限。

总结:利用MySQL实用程序。 在这种特殊情况下,使用GTID复制时,mysqlslavetrx非常有用,并且您希望执行干净的故障转移。 它可以作为MHA故障切换的前置脚本(自0.56版本以来支持GTID)添加,也可以简单地用于维护主站和从站之间的一致性。

errant-transactions的更多相关文章

  1. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  2. The Myths about Transactions (ACID) and NoSQL

    There has been widespread characterization of one of the major distinctions between NoSQL and tradit ...

  3. 事务使用中如何避免误用分布式事务(System.Transactions.TransactionScope)

    1:本地事务DbTransaction和分布式事务TransactionScope的区别: 1.1:System.Data.Common.DbTransaction: 本地事务:这个没什么好说了,就是 ...

  4. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

  5. Netsuite > Hierarchy of transactions in Inventory cost calculation

    First in day worksheets + Purchase Transactions (Receipts, Bills, Adjustments, Assembly Builds) + Tr ...

  6. PCI Express(六) - Simple transactions

    原文地址:http://www.fpga4fun.com/PCI-Express6.html Let's try to control LEDs from the PCI Express bus. X ...

  7. [转]ORACLE DBA TRANSACTIONS

    本文转自:http://blog.sina.com.cn/s/blog_66f845010100qelf.html 一, Transaction control 默认Transaction 由修改数据 ...

  8. Why you shouldn't use Entity Framework with Transactions

    Links EntityFramework This is a .net ORM Mapper Framework from Microsoft to help you talking with yo ...

  9. Working with Transactions (EF6 Onwards)

    Data Developer Center > Learn > Entity Framework > Get Started > Working with Transactio ...

随机推荐

  1. 压缩JS,提高代码执行速度

    压缩JS java -jar yuicompressor-2.4.jar --type js xxx.js -o xxx.js --charset utf-8 压缩CSS java -jar yuic ...

  2. Tinker爬坑之路

    目的 热修复去年年底出的时候,变成了今年最火的技术之一.依旧记得去年面试的时候统一的MVP,然而今年却变成了RN,热修复.这不得不导致我们需要随时掌握最新的技术.不然可能随时会被淘汰.记得刚进公司,技 ...

  3. Golang使用MongoDB通用操作

    MongoDB是Nosql中常用的一种数据库,今天笔者就简单总结一下Golang如何使用这些通用的供能的,不喜勿喷... 研究的事例结构如下: type LikeBest struct { Autho ...

  4. ubuntu 设置plank开机自启之后关机键失效变为注销键

    之前因为猎奇心,给我的ubuntu系统换了一个macUbuntu的桌面,但是之前用的dock是docky,昨日闲来无聊换成了plank,设置成然后就发现我的系统关不了机了,只能通过指令关机. 百度之后 ...

  5. CentOS 6.5添加网易163源

    换国内的yum源. 准备工作,首先备份/etc/yum.repos.d/CentOS-Base.repo              cd /etc/yum.repos.d/ wget http://m ...

  6. PS基础教程[7]如何为照片瘦身

    有没有对自己的身材有所抱怨,有没有想过让自己的照片便得苗条一些,其实只有你想不到的,没有我们做不到的,PS中可以很简单的将我们的身体进行美化瘦身,本次经验我们就来学习一下简单的操作. 方法 1.打卡P ...

  7. bzoj 1500 维修序列

    Written with StackEdit. Description 请写一个程序,要求维护一个数列,支持以下 \(6\) 种操作: 请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格 I ...

  8. Robot Framework接口测试(1)

    RF是做接口测试的一个非常方便的工具,我们只需要写好发送报文的脚本,就可以灵活的对接口进行测试. 做接口测试我们需要做如下工作: 1.拼接发送的报文 2.发送请求的方法 3.对结果进行判断 我们先按步 ...

  9. SQL Server: Top 10 Secrets of a SQL Server Expert

    转载自:http://technet.microsoft.com/en-us/magazine/gg299551.aspx Many companies have downsized their IT ...

  10. CODEVS1049 棋盘染色

    题目大意:01矩阵,1表示黑色,0表示白色,求将白色染成黑色最少的次数 使黑色成为一整个联通块. 题解: 搜索bfs 90... dfs判断连通 #include<iostream> #i ...