参考:http://wubx.net/?s=mysqlbinlog

mysql官方的mysqlbinlog没有回滚的功能,淘宝大牛对官方代码进行了修改使之能够将binlog中的DML操作变成互逆的语句,比如delete变为insert,update的set和where的互换。不过注意前提是binlog的格式必须是binlog_format=ROW

另外注意导入回滚数据较多时,需要调整超时时间和最大package的大小:

binlog_format=ROW

max_allowed_packet=1024M

#max_allow_packet大小不够时报错如下:
ERROR 1153 (08S01) at line 403133: Got a packet bigger than ‘max_allowed_packet’ bytes

重构代码

http://mysql.taobao.org/index.php/Patch_source_code#Add_flashback_feature_for_mysqlbinlog

有三个版本,测试percona5518版本可以使用5.5.18_flashback_all.diff,mysql官方版本只能使用5.5.18_flashback.diff。

http://mysql.taobao.org/images/5/53/5.5.18_flashback_all.diff

http://mysql.taobao.org/images/0/0f/5.5.18_flashback.diff

percona5.5.18版本的下载链接:

http://www.percona.com/downloads/Percona-Server-5.5/Percona-Server-5.5.18-23.0/source/Percona-Server-5.5.18-rel23.0.tar.gz

mysql5.5.18

http://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.18.tar.gz

cd mysql-5.5./
wget http://mysql.taobao.org/images/0/0f/5.5.18_flashback.diff
patch -p0 < 5.5.18_flashback.diff
## 编译成功无报错即可,无特殊编译参数要求

编译完成后,开始测试:

[root@server1 /]# /percona5518/bin/mysqlbinlog --help
在说明中多了
-B, --flashback Flashback data to start_postition or start_datetime.

然后可以用一个binlog文件做测试

mysql> select count(1) from t1;
+----------+
| count(1) |
+----------+
| 16 |
+----------+
1 row in set (0.00 sec) mysql> select * from t1 where id<5;
+----+------+------+
| id | a | b |
+----+------+------+
| 1 | aaaa | bbbb |
| 2 | aaaa | bbbb |
| 3 | aaaa | bbbb |
| 4 | aaaa | bbbb |
+----+------+------+
4 rows in set (0.00 sec) mysql> flush logs;
Query OK, 0 rows affected (0.11 sec) mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000010
Position: 108
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec) mysql> update t1 set a='XXXX',b='SSSS' where id<5;
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush logs;
Query OK, 0 rows affected (0.09 sec)

打印出binlog

[root@server1 bin]# ./mysqlbinlog -uroot -p ../data/mysql-bin. -v -v --base64-output=decode-rows
Enter password:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos Start: binlog v , server v 5.5.-log created ::
# at
# :: server id end_log_pos Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at
# at
# :: server id end_log_pos Table_map: `test`.`t1` mapped to number
# :: server id end_log_pos Update_rows: table id flags: STMT_END_F
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
# at
# :: server id end_log_pos Xid =
COMMIT/*!*/;
# at
# :: server id end_log_pos Rotate to mysql-bin. pos:
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

打印出回滚的binlog

[root@server1 bin]# ./mysqlbinlog -uroot -p ../data/mysql-bin. -v -v --base64-output=decode-rows -B
Enter password:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# :: server id end_log_pos Start: binlog v , server v 5.5.-log created ::
# :: server id end_log_pos Rotate to mysql-bin. pos:
# :: server id end_log_pos Xid =
COMMIT/*!*/;
# :: server id end_log_pos Table_map: `test`.`t1` mapped to number
# :: server id end_log_pos Update_rows: table id flags: STMT_END_F
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### UPDATE test.t1
### WHERE
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='XXXX' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='SSSS' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=0 is_null=0 */
### @='aaaa' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
### @='bbbb' /* VARSTRING(15) meta=15 nullable=1 is_null=0 */
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

delete语句的回滚是把delete换成了insert

在导入的时候遇到了问题,导入一直不成功。

[root@server1 bin]# ./mysqlbinlog ../data/mysql-bin.000010 -v -v --base64-output=decode-rows  -B |  ./mysql -uroot -p -S ../tmp/mysql.sock  test
没有效果!

后来发现是参数的问题,再试成功了,如果使用mysql官方版本好像除了-B 参数不用带其他参数,如果测试有问题可以增减参数试试。

[root@server1 bin]# ./mysqlbinlog ../data/mysql-bin. -v-v -B |  ./mysql -uroot -p -S ../tmp/mysql.sock  test

另外注意几个比较实用的参数

  --start-datetime=name
Start reading the binlog at first event having a datetime
equal or posterior to the argument; the argument must be
a date and time in the local time zone, in any format
accepted by the MySQL server for DATETIME and TIMESTAMP
types, for example: -- :: (you should
probably use quotes for your shell to set it properly).
-j, --start-position=#
Start reading the binlog at position N. Applies to the
first binlog passed on the command line.
--stop-datetime=name
Stop reading the binlog at first event having a datetime
equal or posterior to the argument; the argument must be
a date and time in the local time zone, in any format
accepted by the MySQL server for DATETIME and TIMESTAMP
types, for example: -- :: (you should
probably use quotes for your shell to set it properly).
--stop-position=# Stop reading the binlog at position N. Applies to the
last binlog passed on the command line. -d, --database=name List entries for just this database (local log only). -f, --force-read Force reading unknown binlog events.

恢复一个事务,找到开始和结束的pos位置

[root@server1 bin]# ./mysqlbinlog -v -v ../mysql-bin.  --start-position= --stop-position= -B |  ./mysql -uroot -p -S ../tmp/mysql.sock  test

如何能回滚一个binlog中一个表的数据而不影响其他表?如果是一个事务可以使用“--start-position=”和“--stop-position=”,如果不是一个连续的事务呢?

参考:http://hcymysql.blog.51cto.com/5223301/1553080

不过我在5.5的版本中测试不能给库级和表级授权super

【MySQL】使用mysqlbinlog回滚的更多相关文章

  1. 第二百八十六节,MySQL数据库-MySQL事务操作(回滚)

    MySQL数据库-MySQL事务操作(回滚) 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 举例:有这样一张表 从表里可以看出张 ...

  2. mysql基于binlog回滚工具_flashback(python版本)

        update.delete的条件写错甚至没有写,导致数据操作错误,需要恢复被误操作的行记录.这种情形,其实时有发生,可以选择用备份文件+binlog来恢复到测试环境,然后再做数据修复,但是这样 ...

  3. MySql事务无法回滚的原因

    使用MySQL时.假设发现事务无法回滚,但Hibernate.Spring.JDBC等配置又没有明显问题时.不要苦恼,先看看MySQL创建的表有没有问题.即表的类型. InnoDB和MyISAM是在使 ...

  4. 【MySQL】 DB 回滚崩溃案例一则

    一台mysql数据库在多次断电之后,长时间等待未果直接重启mysql 服务.之后错误日志中报错: 170119 :: InnoDB: Error: page log sequence number I ...

  5. Spring+hibernate+mysql事物不回滚的原因以及处理

    最近项目突然出了点问题,然后发现用Service层下面的一个类的一个方法里的事务居然没有回滚.然后自己写了一个测试方法经过了N次测试都是不回滚.以下是测试方法的一部分: @Transactional( ...

  6. MySQL数据库的回滚失败(JAVA)

    这几天在学习MySQL数据的知识,有一个小测试,用来测试数据库的提交和回滚. 刚开始的时候真的没把这个当回事,按照正常的步骤来讲的话,如下所示,加载驱动,获取数据库的连接,并且把数据库的自动提交给关闭 ...

  7. C# mysql 处理 事务 回滚 提交

    MySqlConnection myCon; void iniMysql() { //连接数据库 myCon = new MySqlConnection("server=127.0.0.1; ...

  8. php+mysql实现事务回滚

    模拟条件:第一个表插入成功,但是第二个表插入失败,回滚.第一个表插入成功,第二个表插入成功,执行.第一个表插入失败,第二个表插入成功,回滚.第一个表插入失败,第二个表插入失败,回滚.以上情况都需要回滚 ...

  9. php+mysql 原生事务回滚

    <?php $conn = mysql_connect('127.0.0.1', 'root', ''); mysql_select_db('msc_test'); mysql_query('S ...

随机推荐

  1. html元素的显示和隐藏

    div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: style="visibility: hidden;" document.getElementBy ...

  2. PLSQL_性能优化系列14_Oracle High Water Level高水位分析

    2014-10-04 Created By BaoXinjian 一.摘要 PLSQL_性能优化系列14_Oracle High Water Level高水位分析 高水位线好比水库中储水的水位线,用于 ...

  3. NeHe OpenGL教程 第十一课:飘动的旗帜

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. linux命令(11)下查看和添加环境变量

    &PATH:决定了shell将到哪些目录中去寻找命令或者程序,PATH值是一系列的目录,当你要运行一个程序时,Linux在这些目录下进行搜寻编译链接. 编辑PATH 声明,其格式为: PATH ...

  5. image和字节流之间的相互转换

    //将图片转化为长二进制 public Byte[] SetImgToByte(string imgPath) { FileStream file = new FileStream(imgPath, ...

  6. C++ operator 知识点 2

    http://blog.csdn.net/szlanny/article/details/4295854 operator它有两种用法,一种是operator overloading(操作符重载),一 ...

  7. ssh-keygen+ssh-copy-id无密码登录远程LINUX主机(转载)

    From:http://blog.163.com/lgh_2002/blog/static/44017526201011333227161/ 1.创建公钥和私钥 ligh@local-host$ ss ...

  8. SyntaxError: Non-UTF-8 code starting with '\xba' in file 错误的解决方法!!

    第一次在Eclipse建立python工程,添加了自己新建的文件,写了一点代码,随后执行时候出现了错误,和昨天我在Visual Studio 2015里面一样,错误: SyntaxError: Non ...

  9. FS4412系统移植

    一.SD启动盘制作 1. 下载SD启动盘的工具 sdfuse_q 2. 插入SD卡,并格式化 (1)查看SD卡:sudo fdisk -l (2)格式化SD卡: sudo mkfs -t vfat / ...

  10. windows timeGetTime() 函数 获取系统从开机到现在的毫秒时间值

    #include <windows.h> #include <iostream> #pragma comment( lib,"winmm.lib" ) in ...