MySQL binlog恢复
Posted qiezijiajia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL binlog恢复相关的知识,希望对你有一定的参考价值。
mysql binlog恢复数据,有时候可能不小心delete了数据,一下子捉急了,怎么办? binlog来恢复(前提是你开启了binlog),怎么开启呢?
在my.cnf文件中添加如下
[mysqld]
log_bin = mysql_bin
重启服务即可
接下来讲讲怎么恢复,删除数据后,马上查看当前的数据库处于哪个binlog文件:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 1016 | | | |
+------------------+----------+--------------+------------------+-------------------+
接下来查看binlog文件在哪
find / -name mysql_bin.000001
去到binlog的目录下,执行mysqlbinlog mysql_bin.000001(这里我的文件比较小,文件大的话加上过滤或者分页)
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180409 20:56:53 server id 1 end_log_pos 120 CRC32 0xf20fd50f Start: binlog v 4, server v 5.6.39-log created 180409 20:56:53 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG ‘
FWPLWg8BAAAAdAAAAHgAAAABAAQANS42LjM5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAVY8taEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAQ/V
D/I=
‘/*!*/;
# at 120
#180409 20:58:03 server id 1 end_log_pos 199 CRC32 0x2c3cc333 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1523278683/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
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=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 199
#180409 20:58:03 server id 1 end_log_pos 313 CRC32 0xd0d61192 Query thread_id=2 exec_time=0 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1523278683/*!*/;
insert into by_year values(‘2019-10-10‘)
/*!*/;
# at 313
#180409 20:58:03 server id 1 end_log_pos 344 CRC32 0x614b6da7 Xid = 14
COMMIT/*!*/;
# at 344
#180409 20:58:20 server id 1 end_log_pos 423 CRC32 0x7610d8d6 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1523278700/*!*/;
BEGIN
/*!*/;
# at 423
#180409 20:58:20 server id 1 end_log_pos 537 CRC32 0xea1fbbcb Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1523278700/*!*/;
delete from by_year where d=‘2019-10-10‘ (删除语句)
/*!*/;
# at 537
#180409 20:58:20 server id 1 end_log_pos 568 CRC32 0xacbe35a7 Xid = 15
COMMIT/*!*/;
很明显,这时候只需要把前面插入的语句恢复即可
找到对应的时间点,将binlog导出到sql文件中
mysqlbinlog --start-datetime=‘2018-04-09 20:58:00‘ --stop-datetime=‘2018-04-09 20:58:18‘ mysql_bin.000001 > tmp.sql
再将sql导入即可
以上是关于MySQL binlog恢复的主要内容,如果未能解决你的问题,请参考以下文章
mysql的binlog开启方式,查看方式.三种binlog模式介绍.以及使用binlog恢复数据.删除binlog