假设我们在项目中,每周日0时做一次全备,刚好在周日的00:30不小心误删除了一条记录,那么问题来了,我们应该怎么做恢复呢?我对它处理的思路是:
1.首先关闭外部连接或禁止进行写操作,关闭日志写:mysql>flush logs; mysql>setglobal sql_log_bin=0;
2.用全备做恢复:
mysql -uroot -proot fancy </opt/fancy.sql
3.找出全备结束时所在binlog的位置: grep -i "change" /opt/fancy.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysqlfancy.000004', MASTER_LOG_POS=7974;
4.查年当前的binlog的位置,并确定要恢复哪几个binlog日志:mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysqlfancy.000004 | 8363 | | |
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysqlfancy.000004 | 8363 | | |
+-------------------+----------+--------------+------------------+
5.读取要恢复的日志并转化成bin.sql文件:mysqlbinlog --no-defaults /data/mysql/data/mysqlfancy.000004 >/opt/bin.sql
6.打开bin.sql文件,删除误操作的步骤并保存:vi /opt/bin.sql
7.对bin.sql进行增量恢复: mysql -uroot -proot fancy </opt/bin.sql
8.开启日志写:mysql>setglobal sql_log_bin=0;