mysqlbinlog 工具的使用 , 大家可以看 MySQL 的帮助手册。里面有详细的用,在这个例子中,重点是 --start-position 参数和 --stop-position

 

· --start-position=N

从二进制日志中第 1 个位置等于 N

· --stop-position=N

从二进制日志中第 1 个位置等于和大于 N

 

OK ,现在开始,要启动二进制日志记录,要先在 my.cnf / my.ini 文件的 mysqld

log-bin=

在这里,偶是的设置是 log-bin=liangck

然后再启动 mysql 服务,因为偶是用 windows 系统,所以执行 net start mysql

 

然后在一测试数据库里,创建一个表,并添加记录。

mysql>  create  table  test( id int  auto_increment not  null  primary  key , val int , data varchar ( 20));
 
mysql>  insert  into  test( val, data)  values ( 10, 'liang' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  insert  into  test( val, data)  values ( 20, 'jia' );
Query OK,  1 row affected  ( 0.08 sec)
 
mysql>  insert  into  test( val, data)  values ( 30, 'hui' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  flush logs;     --
Query OK,  0 rows  affected  ( 0.09 sec)
 
mysql>  insert  into  test( val, data)  values ( 40, 'aaa' );
Query OK,  1 row affected  ( 0.05 sec)
 
mysql>  insert  into  test( val, data)  values ( 50, 'bbb' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  insert  into  test( val, data)  values ( 60, 'ccc' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  delete  from  test where  id between  4 and  5;    --
Query OK,  2 rows  affected  ( 0.05 sec)
 
mysql>  insert  into  test( val, data)  values ( 70, 'ddd' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  flush logs;            --
Query OK,  0 rows  affected  ( 0.11 sec)
 
mysql>  insert  into  test( val, data)  values ( 80, 'dddd' );
Query OK,  1 row affected  ( 0.05 sec)
 
mysql>  insert  into  test( val, data)  values ( 90, 'eeee' );
Query OK,  1 row affected  ( 0.03 sec)
 
mysql>  drop  table  test;         --
Query OK,  0 row affected  ( 0.05 sec)

 

――――――――――――――――――――――――――――――――――

OK

就是将 test

 

先用 mysqlbinlog 工具将日志文件生成 txt

 

F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000001 > G:\001.txt
 
F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000002 > G:\002.txt
 
F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000003 > G:\003.txt

通过这三个命令,可以在 G 盘下生成 3

因为我们需要重做第一个日志文件的所有操作,所以这里只需要将第一个日志文件全恢复就行了。

F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000001 | mysql -uroot –p

 

Ok, 接着,我们需要分析的是第二个日志文件。为什么要分析它呢,因为它中途执行了一个操作是 DELETE

我们先打开 002.txt

 

/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#090427 15:27:56 server id 1   end_log_pos 106 Start: binlog v 4, server v 5.1.32-community-log created 090427 15:27:56
BINLOG '
fF71SQ8BAAAAZgAAAGoAAAAAAAQANS4xLjMyLWNvbW11bml0eS1sb2cAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
# at 106
#090427 15:28:37 server id 1   end_log_pos 176 Query   thread_id=1    exec_time=0    error_code=0
use mytest/*!*/;
SET TIMESTAMP=1240817317/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1344274432/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=28/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 176
#090427 15:28:37 server id 1   end_log_pos 204 Intvar
SET INSERT_ID=4/*!*/;
# at 204
#090427 15:28:37 server id 1   end_log_pos 312 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817317/*!*/;
insert into test(val,data) values(40,'aaa')
/*!*/;
# at 312
#090427 15:28:37 server id 1   end_log_pos 339 Xid = 12
COMMIT/*!*/;
# at 339
#090427 15:28:46 server id 1   end_log_pos 409 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817326/*!*/;
BEGIN
/*!*/;
# at 409
#090427 15:28:46 server id 1   end_log_pos 437 Intvar
SET INSERT_ID=5/*!*/;
# at 437
#090427 15:28:46 server id 1   end_log_pos 545 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817326/*!*/;
insert into test(val,data) values(50,'bbb')
/*!*/;
# at 545
#090427 15:28:46 server id 1   end_log_pos 572 Xid = 13
COMMIT/*!*/;
# at 572
#090427 15:29:35 server id 1   end_log_pos 642 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817375/*!*/;
BEGIN
/*!*/;
# at 642
#090427 15:29:35 server id 1   end_log_pos 670 Intvar
SET INSERT_ID=6/*!*/;
# at 670
#090427 15:29:35 server id 1   end_log_pos 778 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817375/*!*/;
insert into test(val,data) values(60,'ccc')
/*!*/;
# at 778
#090427 15:29:35 server id 1   end_log_pos 805 Xid = 14
COMMIT/*!*/;
# at 805
#090427 15:30:21 server id 1   end_log_pos 875 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817421/*!*/;
BEGIN
/*!*/;
# at 875
#090427 15:30:21 server id 1   end_log_pos 981 Query   thread_id=1    exec_time=0    error_code=0
SET TIMESTAMP=1240817421/*!*/;
delete from test where id between 4 and 5
/*!*/;
# at 981
#090427 15:30:21 server id 1   end_log_pos 1008     Xid = 15
COMMIT/*!*/;
# at 1008
#090427 15:30:34 server id 1   end_log_pos 1078     Query   thread_id=1    exec_time=0     error_code=0
SET TIMESTAMP=1240817434/*!*/;
BEGIN
/*!*/;
# at 1078
#090427 15:30:34 server id 1   end_log_pos 1106     Intvar
SET INSERT_ID=7/*!*/;
# at 1106
#090427 15:30:34 server id 1   end_log_pos 1214     Query   thread_id=1    exec_time=0     error_code=0
SET TIMESTAMP=1240817434/*!*/;
insert into test(val,data) values(70,'ddd')
/*!*/;
# at 1214
#090427 15:30:34 server id 1   end_log_pos 1241     Xid = 16
COMMIT/*!*/;
# at 1241
#090427 15:30:41 server id 1   end_log_pos 1282     Rotate to liangck.000003   pos: 4
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
 
――――――――――――――――――――――――――――――――――――― 
 
在这个文件中,我们可以看到 DELETE 的操作的起始位置是 875 ,终止位置是 1008. 那么我们只要重做第二个日志文件的开头到 875 的操作,然后再从 1008 到末尾的操作,我们就可以把数据给恢复回来,而不会 DELETE
F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000002 --stop-pos=875 | mysql -uroot -p
 
F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000002 --start-pos=1008 | mysql -uroot -p mytest
 
OK,
第三个日志文件也是同理,只要找到 DROP TABLE
F:\Program Files\MySQL_Data\data\log>mysqlbinlog liangck.000003 --stop-pos=574 | mysql -uroot –p
 
现在我们再查一下数据看看 :
mysql> select * from test;
+----+------+-------+
| id | val   | data   |
+----+------+-------+
|   1 |    10 | liang |
|   2 |    20 | jia    |
|   3 |    30 | hui    |
|   4 |    40 | aaa    |
|   5 |    50 | bbb    |
|   6 |    60 | ccc    |
|   7 |    70 | ddd    |
|   8 |    80 | dddd   |
|   9 |    90 | eeee   |
+----+------+-------+
9 rows in set (0.00 sec)

 

可以看到,全部数据都回来了。