1. 程式人生 > >MySQL:生產誤刪除資料恢復方法

MySQL:生產誤刪除資料恢復方法

因為生產上誤執行語句,需要找回原資料

delete from `xxx` where a = 1;

步驟

1、解析主的binlog找到執行刪除語句時對應的pos點,如下:

# at 272065343
#160815 16:27:46 server id 1  end_log_pos 272065370     Xid = 12731823587
COMMIT/*!*/;
# at 272065370
#160815 16:27:21 server id 1  end_log_pos 272065441     Query   thread_id=83836962      exec_time=26    error_code=0
SET TIMESTAMP=1471249641/*!*/;
SET @@session.sql_mode=0/*!*/;
BEGIN
/*!*/;
# at 272065441
#160815 16:27:21 server id 1  end_log_pos 272065566     Query   thread_id=83836962      exec_time=26    error_code=0
SET TIMESTAMP=1471249641/*!*/;
delete from `xxx` where a = 1;
/*!*/;
# at 272065566
#160815 16:27:21 server id 1  end_log_pos 272065593     Xid = 12731793231
COMMIT/*!*/;

2、找一臺空機器,恢復備份
/usr/bin/innobackupex-1.5.1 --user=root --host=127.0.0.1 --port=3306 --defaults-file=/data/mysql/mysql3307/my.cnf --apply-log /data/mysqlbak/db_bak/20160815_3306
/usr/bin/innobackupex-1.5.1 --user=root --host=127.0.0.1 --port=3306 --defaults-file=/data/mysql/mysql3307/my.cnf --copy-back /data/mysqlbak/db_bak/20160815_3306

3、因為只需要恢復一張表 xxx,修改my.cnf新增引數
replicate-wild-do-table = x.xxx 

4、搭建從庫,恢復到制定位置

change master to master_host='192.168.x.x', 
                            master_port=3306,
                            master_user='qqq', 
                            master_password='qqq', 
                            master_log_file='mysql-bin.001002', 
                            master_log_pos=203021225;
								
START SLAVE UNTIL master_log_file='mysql-bin.001003',master_log_pos=272065343;