首页 > 数据库 > MySQL > mysql bin-log恢复
#vi/etc/my.cnf 路径根据实际情况
log-bin=mysql-bin
在mysql主目录下会有mysql-bin.000001的文件。
每次重启都会有一个,如果这个文件达到最大容量了也会有一个新的产生。如果强制刷日志切换也会有一个。
Mysql > Use test;
Mysql > create table t1(id int);
Mysql > show tables;
Mysql > insert into t1 values(1);
Mysql > insert into t1 values(2);
Mysql > select * from t1;
这个时候进行一下全备。
[root@namenode mysql]# mysqldump -uroot -p test > /data/backups/138.sql
用这种方式全备。
接下来。
Mysql > insert into t1 values(3);
Mysql > insert into t1 values(4);
Mysql > insert into t1 values(5);
最后将t1表删除。
[root@namenode mysql]# mysql -uroot -p1 < /data/backups/138.sql
用这种方式可以恢复到刚才备份的时间点。
但是我们后续插入的3条数据是看不到的。这就需要bin-log来恢复了。
[root@namenode mysql]# mysqlbinlog --no-defaults mysql-bin.000003
执行红色命令后可以输出下面的
注意下面红色部分就是3条丢失的数据。
#150629 18:28:19 server id 136 end_log_pos 10053 CRC32 0x6b64e0cd Query thread_id=10 exec_time=461 error_code=0
SET TIMESTAMP=1435573699/*!*/;
insert into t3 values(3)
/*!*/;
# at 10053
#150629 18:28:19 server id 136 end_log_pos 10084 CRC32 0xd8741349 Xid = 548
COMMIT/*!*/;
# at 10084
#150629 18:28:19 server id 136 end_log_pos 10163 CRC32 0x1fef63bb Query thread_id=10 exec_time=461 error_code=0
SET TIMESTAMP=1435573699/*!*/;
BEGIN
/*!*/;
# at 10163
#150629 18:28:19 server id 136 end_log_pos 10261 CRC32 0x8a93f129 Query thread_id=10 exec_time=461 error_code=0
SET TIMESTAMP=1435573699/*!*/;
insert into t3 values(4)
/*!*/;
# at 10261
#150629 18:28:19 server id 136 end_log_pos 10292 CRC32 0x430a1830 Xid = 553
COMMIT/*!*/;
# at 10292
#150629 18:28:20 server id 136 end_log_pos 10371 CRC32 0x09612e30 Query thread_id=10 exec_time=460 error_code=0
SET TIMESTAMP=1435573700/*!*/;
BEGIN
/*!*/;
# at 10371
#150629 18:28:20 server id 136 end_log_pos 10469 CRC32 0x83e2b8e7 Query thread_id=10 exec_time=460 error_code=0
SET TIMESTAMP=1435573700/*!*/;
insert into t3 values(5)
/*!*/;
# at 10469
#150629 18:28:20 server id 136 end_log_pos 10500 CRC32 0xed5aca9e Xid = 558
COMMIT/*!*/;
# at 10500
#150629 18:37:04 server id 136 end_log_pos 10523 CRC32 0x9f110cd8 Stop
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
恢复方式1:[root@namenode mysql]# mysqlbinlog --no-defaults mysql-bin.000003 --start-position="633" --stop-position="1257" | mysql –u root –p
这里的开始和结束就是我们丢失数据的位置end_log_pos(到上面找这个关键字)
恢复方式2:[root@namenode mysql]# mysqlbinlog --no-defaults mysql-bin.000003 --start-position="633" --stop-position="1257" > /data/backups/137.sql 将这些输出到指定的sql,然后就和全库恢复的命令一样(参照上面绿色的命令),将sql全部灌回数据库。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/637517/viewspace-1762820/ ,如需转载,请注明出处,否则将追究法律责任。