1. 创建表,插入数据.

```

mysql> create database binlog;

mysql> create table bt(id int);

mysql> insert into bi values (1);

mysql> insert into bi values (2);

mysql> insert into bi values (3);

mysql> insert into bi values (4);

mysql> delete from bt where id=1; 误删除

mysql> update bt set id=22 where id=2;

mysql> drop table bt;

mysql> drop database binlog;

```

  1. 我想恢复数据到 delete之前

查看当前所有的是哪个binlog

-> ;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000006 | 2750 | | | |

+------------------+----------+--------------+------------------+-------------------+

  1. 查看位置点

mysql> show binlog events in "mysql-bin.000006";

+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+

| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |

+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+

| mysql-bin.000006 | 4 | Format_desc | 3307 | 120 | Server ver: 5.6.40-log, Binlog ver: 4 |

| mysql-bin.000006 | 120 | Query | 3307 | 214 | create database tttt |

| mysql-bin.000006 | 214 | Query | 3307 | 314 | create database binlog |

| mysql-bin.000006 | 314 | Query | 3307 | 415 | use `binlog`; create table bt(id int) |

| mysql-bin.000006 | 415 | Query | 3307 | 489 | BEGIN |

| mysql-bin.000006 | 489 | Table_map | 3307 | 536 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 536 | Write_rows | 3307 | 576 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 576 | Xid | 3307 | 607 | COMMIT /* xid=2101 */ |

| mysql-bin.000006 | 607 | Query | 3307 | 681 | BEGIN |

| mysql-bin.000006 | 681 | Table_map | 3307 | 728 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 728 | Write_rows | 3307 | 768 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 768 | Xid | 3307 | 799 | COMMIT /* xid=2102 */ |

| mysql-bin.000006 | 799 | Query | 3307 | 873 | BEGIN |

| mysql-bin.000006 | 873 | Table_map | 3307 | 920 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 920 | Write_rows | 3307 | 960 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 960 | Xid | 3307 | 991 | COMMIT /* xid=2107 */ |

| mysql-bin.000006 | 991 | Query | 3307 | 1065 | BEGIN |

| mysql-bin.000006 | 1065 | Table_map | 3307 | 1112 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 1112 | Write_rows | 3307 | 1152 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 1152 | Xid | 3307 | 1183 | COMMIT /* xid=2108 */ |

| mysql-bin.000006 | 1183 | Query | 3307 | 1257 | BEGIN |

| mysql-bin.000006 | 1257 | Table_map | 3307 | 1304 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 1304 | Delete_rows | 3307 | 1344 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 1344 | Xid | 3307 | 1375 | COMMIT /* xid=2110 */ |

| mysql-bin.000006 | 1375 | Query | 3307 | 1449 | BEGIN |

| mysql-bin.000006 | 1449 | Table_map | 3307 | 1496 | table_id: 211 (binlog.bt) |

| mysql-bin.000006 | 1496 | Update_rows | 3307 | 1542 | table_id: 211 flags: STMT_END_F |

| mysql-bin.000006 | 1542 | Xid | 3307 | 1573 | COMMIT /* xid=2112 */ |

| mysql-bin.000006 | 1573 | Query | 3307 | 1692 | use `binlog`; DROP TABLE `bt` /* generated by server */ |

| mysql-bin.000006 | 1692 | Query | 3307 | 1781 | drop database binlog |

+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+

  1. mysqlbinlog查看详细位置

[root@db01 data]#mysqlbinlog --base64-output=decode-rows -vvv /data/3307/mysql-bin.000006;

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#190312 15:55:43 server id 3307 end_log_pos 120 CRC32 0x2ec2b6d5     Start: binlog v 4, server v 5.6.40-log created 190312 15:55:43 at startup

# Warning: this binlog is either in use or was not closed properly.

ROLLBACK/*!*/;

# at 120

#190312 16:49:12 server id 3307 end_log_pos 214 CRC32 0xba00b2bf     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552380552/*!*/;

SET @@session.pseudo_thread_id=23/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=1073741824/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

/*!\C utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

create database tttt

/*!*/;

#190312 17:18:59 server id 3307 end_log_pos 314 CRC32 0xd2cf5a58     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382339/*!*/;

create database binlog

/*!*/;

# at 314

#190312 17:19:51 server id 3307 end_log_pos 415 CRC32 0x96788f47     Query    thread_id=23    exec_time=0    error_code=0

use `binlog`/*!*/;

SET TIMESTAMP=1552382391/*!*/;

create table bt(id int)

/*!*/;

# at 415

#190312 17:20:40 server id 3307 end_log_pos 489 CRC32 0x16fe37e6     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382440/*!*/;

BEGIN

/*!*/;

# at 489

#190312 17:20:40 server id 3307 end_log_pos 536 CRC32 0xcb39e953     Table_map: `binlog`.`bt` mapped to number 211

# at 536

#190312 17:20:40 server id 3307 end_log_pos 576 CRC32 0xc8238a32     Write_rows: table id 211 flags: STMT_END_F

### INSERT INTO `binlog`.`bt`

### SET

### @1=1 /* INT meta=0 nullable=1 is_null=0 */

# at 576

#190312 17:20:40 server id 3307 end_log_pos 607 CRC32 0x0688157a     Xid = 2101

COMMIT/*!*/;

# at 607

#190312 17:20:46 server id 3307 end_log_pos 681 CRC32 0xa06512e1     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382446/*!*/;

BEGIN

/*!*/;

# at 681

#190312 17:20:46 server id 3307 end_log_pos 728 CRC32 0xab62c3d3     Table_map: `binlog`.`bt` mapped to number 211

# at 728

#190312 17:20:46 server id 3307 end_log_pos 768 CRC32 0x01b4afd0     Write_rows: table id 211 flags: STMT_END_F

### INSERT INTO `binlog`.`bt`

### SET

### @1=2 /* INT meta=0 nullable=1 is_null=0 */

# at 768

#190312 17:20:46 server id 3307 end_log_pos 799 CRC32 0xa871612a     Xid = 2102

COMMIT/*!*/;

# at 799

#190312 17:21:52 server id 3307 end_log_pos 873 CRC32 0x058d258b     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382512/*!*/;

BEGIN

/*!*/;

# at 873

#190312 17:21:52 server id 3307 end_log_pos 920 CRC32 0xcbf9b0d7     Table_map: `binlog`.`bt` mapped to number 211

# at 920

#190312 17:21:52 server id 3307 end_log_pos 960 CRC32 0x308c7764     Write_rows: table id 211 flags: STMT_END_F

### INSERT INTO `binlog`.`bt`

### SET

### @1=3 /* INT meta=0 nullable=1 is_null=0 */

# at 960

#190312 17:21:52 server id 3307 end_log_pos 991 CRC32 0x1fd35a76     Xid = 2107

COMMIT/*!*/;

# at 991

#190312 17:21:57 server id 3307 end_log_pos 1065 CRC32 0x2ee8076d     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382517/*!*/;

BEGIN

/*!*/;

# at 1065

#190312 17:21:57 server id 3307 end_log_pos 1112 CRC32 0x90f088e7     Table_map: `binlog`.`bt` mapped to number 211

# at 1112

#190312 17:21:57 server id 3307 end_log_pos 1152 CRC32 0x6068e4af     Write_rows: table id 211 flags: STMT_END_F

### INSERT INTO `binlog`.`bt`

### SET

### @1=4 /* INT meta=0 nullable=1 is_null=0 */

# at 1152

#190312 17:21:57 server id 3307 end_log_pos 1183 CRC32 0x2611d2c2     Xid = 2108

COMMIT/*!*/;

# at 1183

#190312 17:22:35 server id 3307 end_log_pos 1257 CRC32 0x3945a6f3     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382555/*!*/;

BEGIN

/*!*/;

# at 1257

#190312 17:22:35 server id 3307 end_log_pos 1304 CRC32 0xbd963afc     Table_map: `binlog`.`bt` mapped to number 211

# at 1304 1304这个删除事件

#190312 17:22:35 server id 3307 end_log_pos 1344 CRC32 0x5004ca84     Delete_rows: table id 211 flags: STMT_END_F

### DELETE FROM `binlog`.`bt`

### WHERE

### @1=1 /* INT meta=0 nullable=1 is_null=0 */

# at 1344

#190312 17:22:35 server id 3307 end_log_pos 1375 CRC32 0x1bf962db     Xid = 2110

COMMIT/*!*/;

# at 1375

#190312 17:23:04 server id 3307 end_log_pos 1449 CRC32 0xa4635773     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382584/*!*/;

BEGIN

/*!*/;

# at 1449

#190312 17:23:04 server id 3307 end_log_pos 1496 CRC32 0xd2a3c907     Table_map: `binlog`.`bt` mapped to number 211

# at 1496

#190312 17:23:04 server id 3307 end_log_pos 1542 CRC32 0x1138be7d     Update_rows: table id 211 flags: STMT_END_F

### UPDATE `binlog`.`bt`

### WHERE

### @1=2 /* INT meta=0 nullable=1 is_null=0 */

### SET

### @1=22 /* INT meta=0 nullable=1 is_null=0 */

# at 1542

#190312 17:23:04 server id 3307 end_log_pos 1573 CRC32 0x28cc9f00     Xid = 2112

COMMIT/*!*/;

# at 1573

#190312 17:23:35 server id 3307 end_log_pos 1692 CRC32 0x31a3bbb9     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382615/*!*/;

DROP TABLE `bt` /* generated by server */

/*!*/;

# at 1692

#190312 17:23:49 server id 3307 end_log_pos 1781 CRC32 0xe889af93     Query    thread_id=23    exec_time=0    error_code=0

SET TIMESTAMP=1552382629/*!*/;

drop database binlog

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

  1. mysqlbinlog 命令把截取的pos点导出sql语句

[root@db01 data]#mysqlbinlog --start-position=214 --stop-position=1257 /data/3307/mysql-bin.000006 > /tmp/binlog.sql

进库导入binlog.sql

> mysql source /tmp/binlog.sql

  1. 查看找回数据

mysql> show tables

+------------------+

| Tables_in_binlog |

+------------------+

| bt |

+------------------+

1 row in set (0.00 sec)

mysql> select * from bt;

+------+

| id |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

+------+

4 rows in set (0.00 sec)

mysqlbinlog 恢复数据到任意时间点的更多相关文章

  1. mysqlbinlog恢复数据-update20140820

    mysqlbinlog恢复数据 BINLOG就是一个记录SQL语句的过程,和普通的LOG一样.只是它是二进制存储,普通的是十进制存储. ================================ ...

  2. mysqlbinlog恢复数据注意事项【转】

    mysqlbinlog 恢复数据注意事项 前言: 上次有个有个朋友恢复 MySQL 数据,一直恢复不成功,也没有报错信息,使用的环境是 MySQL 5.7 使用了 GTID 以及 binlog 格式为 ...

  3. mysql数据安全之利用二进制日志mysqlbinlog恢复数据

    mysql数据安全之利用二进制日志mysqlbinlog恢复数据 简介:如何利用二进制日志来恢复数据 查看二进制日志文件的内容报错: [root@xdclass-public log_bin]# my ...

  4. 通过mysqlbinlog 恢复数据

    前提数据库开启了bin_log记录日志. 查看日志 刷新日志 flush logs; 再次查看 show binary logs; 向表中插入一条数据 现在执行delete误操作,删除所有的数据. d ...

  5. 使用mysqlbinlog恢复数据

    前提:mysql数据库开启了binlog日志,并且有对应的日志文件 起因:今天由于同事对数据库的误操作不小心删除了一条数据 方法一:通过binlog日志文件恢复数据 通过mysqlbinlog恢复My ...

  6. mysqlbinlog恢复数据

    操作命令: 复制代码代码如下: show binlog events in 'mysql-bin.000016' limit 10; reset master 删除所有的二进制日志flush logs ...

  7. oracle 恢复数据到某个时间点

    delete from tablename;insert into tablename select * from tablename as of timestamp to_timestamp('20 ...

  8. 不小心删除数据--利用MySQL的binlog恢复数据

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  9. 2020-08-01:MySQL 的数据如何恢复到任意时间点?

    福哥答案2020-08-01: 恢复到任意时间点以定时的做全量备份,以及备份增量的 binlog 日志为前提.恢复到任意时间点首先将全量备份恢复之后,再此基础上回放增加的 binlog 直至指定的时间 ...

随机推荐

  1. ABAP基础篇2 数据类型

    基本数据类型列表: 1.长度可变的内置类型(String.XString)1)string类型 在ABAP程序中,string类型是长度无限的字符型字段,可以和CHAR ,D,T ,I,N (F和P未 ...

  2. VS2015 调试中断点失效或表达式错误的解决办法

    题描述:在调试前加了断点,但debug时红色的断点变成透明的圆圈加一个感叹号,执行到该处时也不会停止. 这个问题遇到过几次了,前几次都没怎么注意,有时候是因为复制粘贴了某段代码后就这样了,然后点击撤销 ...

  3. golang中defer的正确使用方式(源自深入解析go)

    3.4 defer关键字 defer和go一样都是Go语言提供的关键字.defer用于资源的释放,会在函数返回之前进行调用.一般采用如下模式: f,err := os.Open(filename) i ...

  4. 【并行计算-CUDA开发】【视频开发】ffmpeg Nvidia硬件加速总结

    2017年5月25日 0. 概述 FFmpeg可通过Nvidia的GPU进行加速,其中高层接口是通过Video Codec SDK来实现GPU资源的调用.Video Codec SDK包含完整的的高性 ...

  5. SpringBoot系列教程web篇之重定向

    原文地址: SpringBoot系列教程web篇之重定向 前面介绍了spring web篇数据返回的几种常用姿势,当我们在相应一个http请求时,除了直接返回数据之外,还有另一种常见的case -&g ...

  6. 接口和抽象类的区别,注意JDK8的接口可以有实现。

    Java中,抽象类和接口有相似的地方.下面我们就来细说说接口和抽象类的异同. 首先是相同的地方: 1. 接口和抽象类都能定义方法和属性. 2. 接口和抽象类都是看作是一种特殊的类.大部分的时候,定义的 ...

  7. 【剑指offer】链表中的倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点. 分析: 定义两个结点p1和p2都指向头节点,p1先走k-1步,然后p1和p2一起走,当p1走到链表尾部时,p2指向的结点就是倒数第k个结点 遍历一遍链表即可 ...

  8. Mysql 语句 insert into 与 replace into 区别

    []insert into 与 replace into 区别 replace into 的运行与insert into 很相似.不同点: 若表中的一个旧记录与一个用于PRIMARY KEY 或 一个 ...

  9. Go语言【数据结构】字符串

    字符串 简介 一个字符串是一个不可改变的字节序列,字符串通常是用来包含人类可读的文本数据.和数组不同的是,字符串的元素不可修改,是一个只读的字节数组.每个字符串的长度虽然也是固定的,但是字符串的长度并 ...

  10. [loj 6253] Yazid的新生舞会

    (很久之前刷的题现在看起来十分陌生a) 题意: 给你一个长度为n的序列A,定义一个区间$[l,r]$是“新生舞会的”当且仅当该区间的众数次数严格大于$\frac{r-l+1}{2}$,求有多少子区间是 ...