Mysql 误删数据 使用 binlog 恢复

2019年6月28日

Mysql  误删数据 使用   binlog 恢复
示例:Centos6.* Mysql 5.5.*

1) 找到误操作时的时间或者  end_log_pos
   进入mysql 的目录
   cd  /var/lib/mysql
   查看binlog 文件 binlog文件不适合直接查看需要转换格式 mysqlbinlog
   mysqlbinlog mysql-bin.000001(binglog file name) > mysql-bin.000001.sql(自定义)
   vim mysql-bin.000001.sql

# at 1961292
#150611  7:03:56 server id 1635  end_log_pos 1961784    Query   thread_id=18    exec_time=0     error_code=0
SET TIMESTAMP=1433977436/*!*/;
INSERT INTO `products` VALUES (2089, 298547, 98, 'SCO076 50cc Scooter - Metallic Burgundy/Black', 'SCO07653', 'no-link', 899.0000, 0.0000, 0.0000, 0.0000, 1.0000, -99, '', '', '', '', 1381802437, 1381805112, '', '', '', 0, 219, 0, 0, 1, '', 'a:1:{s:12:"images_order";s:47:"12771,12765,12777,12779,12773,12775,12769,12767";}', ' Metallic Burgundy/Black', 0, 0, 0, NULL, '', NULL, 0, NULL, NULL, NULL, NULL, 'PMZ50-19NMBURBLK')
/*!*/;
# at 1961784
#150611  7:03:56 server id 1635  end_log_pos 1961853    Query   thread_id=18    exec_time=0     error_code=0
SET TIMESTAMP=1433977436/*!*/;
COMMIT
/*!*/;
# at 1961853
#150611  7:05:39 server id 1635  end_log_pos 1961921    Query   thread_id=27    exec_time=0     error_code=0
SET TIMESTAMP=1433977539/*!*/;
BEGIN
/*!*/;
# at 1961921
#150611  7:05:39 server id 1635  end_log_pos 1962961    Query   thread_id=27    exec_time=0     error_code=0
SET TIMESTAMP=1433977539/*!*/;
DELETE FROM `products`
WHERE ((`id` = '2089') OR (`id` = '2087') OR (`id` = '2085') OR (`id` = '2083') OR (`id` = '2081') OR (`id` = '2079') OR (`id` = '2077') OR (`id` = '2075') OR (`id` = '2073') OR (`id` = '2071') OR (`id` = '2069') OR (`id` = '2067') OR (`id` = '2065') OR (`id` = '2063') OR (`id` = '2061') OR (`id` = '2059') OR (`id` = '2057') OR (`id` = '2055') OR (`id` = '2053') OR (`id` = '2051') OR (`id` = '2049') OR (`id` = '2045') OR (`id` = '2043') OR (`id` = '2041') OR (`id` = '2039') OR (`id` = '2037') OR (`id` = '2035') OR (`id` = '2033') OR (`id` = '2031') OR (`id` = '2029') OR (`id` = '2027') OR (`id` = '2025') OR (`id` = '2023') OR (`id` = '2021') OR (`id` = '2019') OR (`id` = '2017') OR (`id` = '2015') OR (`id` = '2013') OR (`id` = '2011') OR (`id` = '2009') OR (`id` = '2007') OR (`id` = '2005') OR (`id` = '2003') OR (`id` = '2001') OR (`id` = '1999') OR (`id` = '1997') OR (`id` = '1995') OR (`id` = '1993') OR (`id` = '1991') OR (`id` = '1989'))
/*!*/;
# at 1962961
#150611  7:05:39 server id 1635  end_log_pos 1963030    Query   thread_id=27    exec_time=0     error_code=0
SET TIMESTAMP=1433977539/*!*/;
COMMIT
/*!*/;
DELIMITER ;
# End of log file

转换后的binlog格式 在里面查找你犯错的时间点 上述可以看到 我是在 #150611  7:05:39  执行了万恶的DELETE 在这里时间有相同 所以我使用了 唯一的 end_log_pos 值 进行恢复

mysqlbinlog --no-defaults --stop-position="1961853" mysql-bin.000001| mysql -uroot -p moto
mysqlbinlog --no-defaults mysql-bin.000001 --start-position="794" --stop-position="1055" | mysql -uroot -p moto

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注