测试使用mysqlbinlog进行恢复删除的数据

创建测试用的数据库binlog,在里面创建表test,插入并删除数据,然后通过mysqlbinlog解析对应的binlog日志。生成相应的sql语句,进行恢复(这个仅仅是测试,有些还没有搞清楚,还有一些疑问,后面搞清楚了再贴上来)

创建数据库和表,这个时候的binlog是mysql3306-bin.000030

create database binlog;     -- binlog 30 开始
create table test(id int auto_increment not null primary key,val int,data varchar(20))
	   
insert into test(val, data) values (10, 'wu');
insert into test(val, data) values (20, 'yang');
insert into test(val, data) values (20, 'ping');

使用flush log命令生成binlog,生成的新的binlog是mysql3306-bin.000031

flush logs;    -- log 31

 继续插入数据,删除数据,完毕后flush log,生成的binlog是mysql3306-bin.000032

insert into test(val, data) values (40, 'hao');
insert into test(val, data) values (50, 'iteblog');
delete from test where id in (4,5) ;-- between 40 and 50;
insert into test(val, data) values (60, 'iteblog1');
flush logs;  -- log 32  

继续插入数据,并删除表,删除库。

insert into test(val, data) values (70, 'ping123');
insert into test(val, data) values (80, 'ping163');
drop table test;
drop database binlog;   -- log 33 

使用mysqlbinlog解析各个binlog

[root@redhat762100 data]# mysqlbinlog --base64-output=decode-rows  -vv mysql3306-bin.000030 > binlog30.txt 
[root@redhat762100 data]# mysqlbinlog --base64-output=decode-rows  -vv mysql3306-bin.000031 > binlog31.txt 
[root@redhat762100 data]# mysqlbinlog --base64-output=decode-rows  -vv mysql3306-bin.000032 > binlog32.txt 
[root@redhat762100 data]# mysqlbinlog --base64-output=decode-rows  -vv mysql3306-bin.000033 > binlog33.txt

[root@redhat762100 data]# ll *.txt
-rw-r--r--. 1 root root 4625 Oct  8 15:40 binlog30.txt
-rw-r--r--. 1 root root 5357 Oct  8 15:40 binlog31.txt
-rw-r--r--. 1 root root 3986 Oct  8 15:41 binlog32.txt
-rw-r--r--. 1 root root  678 Oct  8 15:41 binlog33.txt
[root@redhat762100 data]# 

binlog 30里面的内容:

/*!\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 table test(id int auto_increment not null primary key,val int,data varchar(20))
/*!*/;

### INSERT INTO `binlog`.`test`
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2=10 /* INT meta=0 nullable=1 is_null=0 */
###   @3='wu' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### INSERT INTO `binlog`.`test`
### SET
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2=20 /* INT meta=0 nullable=1 is_null=0 */
###   @3='yang' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### INSERT INTO `binlog`.`test`
### SET
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2=20 /* INT meta=0 nullable=1 is_null=0 */
###   @3='ping' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

binlog 31里面的内容

### INSERT INTO `binlog`.`test`
### SET
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2=40 /* INT meta=0 nullable=1 is_null=0 */
###   @3='hao' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### INSERT INTO `binlog`.`test`
### SET
###   @1=5 /* INT meta=0 nullable=0 is_null=0 */
###   @2=50 /* INT meta=0 nullable=1 is_null=0 */
###   @3='iteblog' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### INSERT INTO `binlog`.`test`
### SET
###   @1=6 /* INT meta=0 nullable=0 is_null=0 */
###   @2=60 /* INT meta=0 nullable=1 is_null=0 */
###   @3='iteblog1' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### DELETE FROM `binlog`.`test`
### WHERE
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2=40 /* INT meta=0 nullable=1 is_null=0 */
###   @3='hao' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### DELETE FROM `binlog`.`test`
### WHERE
###   @1=5 /* INT meta=0 nullable=0 is_null=0 */
###   @2=50 /* INT meta=0 nullable=1 is_null=0 */
###   @3='iteblog' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

binlog 32里面的内容

### INSERT INTO `binlog`.`test`
### SET
###   @1=7 /* INT meta=0 nullable=0 is_null=0 */
###   @2=70 /* INT meta=0 nullable=1 is_null=0 */
###   @3='ping123' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

### INSERT INTO `binlog`.`test`
### SET
###   @1=8 /* INT meta=0 nullable=0 is_null=0 */
###   @2=80 /* INT meta=0 nullable=1 is_null=0 */
###   @3='ping163' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */

use `binlog`/*!*/;
SET TIMESTAMP=1570518795/*!*/;
DROP TABLE `test` /* generated by server */

#191008 15:13:20 server id 3306  end_log_pos 1084 CRC32 0x7856831e 	Query	thread_id=5	exec_time=0	error_code=0
SET TIMESTAMP=1570518800/*!*/;
drop database binlog
/*!*/;

通过以上的内容,可以执行语句进行还原(语句需要修改下,修改成标准的SQL语句)。

疑问,不知道哪里设置的问题,mysqlbinlog生成的语句,里面不是SQL语句,有些有###给屏蔽了。导致无法使用mysqlbinlog进行还原(需要手工修改下语句)。

扫描二维码关注公众号,回复: 8684790 查看本文章

mysqlbinlog -vv ,一个v是重新生成sql语句,两个v是会生成列的type的注释。

 -v, --verbose       Reconstruct pseudo-SQL statements out of row events. -v
                      -v adds comments on column data types.

base64-output,会显示每个事务的开始与结束(一堆很长的字母字符串)?

--base64-output=name 
                      Determine when the output statements should be
                      base64-encoded BINLOG statements: 'never' disables it and
                      works only for binlogs without row-based events;
                      'decode-rows' decodes row events into commented
                      pseudo-SQL statements if the --verbose option is also
                      given; 'auto' prints base64 only when necessary (i.e.,
                      for row-based events and format description events).  If
                      no --base64-output[=name] option is given at all, the
                      default is 'auto'.

END

发布了754 篇原创文章 · 获赞 31 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/102394456