技术开发 频道

MySQL技巧 使用二进制日志恢复数据

# 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的操作的起始位置是,终止位置是.那么我们只要重做第二个日志文件的开头到的操作,然后再从到末尾的操作,我们就可以把数据给恢复回来,而不会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)

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

0
相关文章