MySQL中truncate误操作后的数据恢复案例
实际线上的场景比较复杂,当时涉及了truncate, delete 两个操作,经确认丢数据差不多7万多行,等停下来时,差不多又有共计1万多行数据写入。 这里为了简单说明,只拿弄一个简单的业务场景举例。
测试环境: percona-server-5.6.16
日志格式: mixed 没起用gtid
表结构如下:
create table `tb_wubx` ( `id` int(11) not null auto_increment, `name` varchar(32) default null, primary key (`id`) ) engine=innodb auto_increment=2 default charset=utf8 create table `tb_wubx` ( `id` int(11) not null auto_increment, `name` varchar(32) default null, primary key (`id`) ) engine=innodb auto_increment=2 default charset=utf8
基于某个时间点有一个备份或是有全量的binlog是能恢复数据的一个唯一保证。 例如我们的备份就是一个表结构创建语句,binlog pos相关信息: mysql-bin.000004 , 4,然后进行了如下:
–t1时间 程序写入:
insert into tb_wubx(name) values(‘张三'),(‘李四'); insert into tb_wubx(name) values(‘隔壁老王');
–t2时间 某个人员失误
truncate table tb_wubx;
–t3时间 程序写入
insert into tb_wubx(name) values(‘老赵'); update tb_wubx set name='老赵赵' where id=1;
现在表里的数据情况:
mysql>select * from tb_wubx; +----+-----------+ | id | name | +----+-----------+ | 1 | 老赵赵 | +----+-----------+ 1 row in set (0.00 sec) mysql>select * from tb_wubx; +----+-----------+ | id | name | +----+-----------+ | 1 | 老赵赵 | +----+-----------+ 1 row in set (0.00 sec)
可以见truncate table操作后,表的自增id又变更为从1开始,原来写入的数据应该是:
+—-+———–+ | id | name | +—-+———–+ | 1 | 张三 | +—-+———–+ | 2 | 李四 | +—-+———–+ | 3 | 隔壁老王 | +—-+———–+
如果没生truncate table操作,实际的数据应该为:
+—-+———–+ | id | name | +—-+———–+ | 1 | 张三 | +—-+———–+ | 2 | 李四 | +—-+———–+ | 3 | 隔壁老王 | +—-+———–+ | 4 | 老赵赵 | +—-+———–+
而且线上的恢复那个表时和序序开发人员了解才知道,原来那个id和缓存及其它地方有依赖,因为id乱了,也会造成程序错乱。这个时间修复id在程序层错乱的事,留给开发人员了关建是给他们讲明白恢复的结果是什么样,我们的关建任务是把数据恢复出来。好,接下来的工作是开始从binlog中恢复数据。
利用: show binary logs; 查看当的log文件分布, 然后利用show binlog events in ‘binary log文件'; 查看log文件的内容,目的是找到truncate发生的日志位置。
另外因为基于备份(由log的启始位置)或是从量log, 如果基于备份有log的起始位置,我们需要处理的log文件是启始位置到发生truncate的日值(后面的数据处理不了,会发生主建冲突的错误造成truncate后的数据不能恢复),
如果是全量日志,需要从创建完mysql后库后的日志去处理到当前的发生truncate的位置(后面数据会因为主建冲突写不进去)
恢复准备工作,创建一个库用于恢复数据,这里创建了一个re_wubx, 及原结构的表: tb_wubx (相当于恢复了备份,过程省略)
mysql> show binary logs; +------------------+-----------+ | log_name | file_size | +------------------+-----------+ | mysql-bin.000001 | 143 | | mysql-bin.000002 | 261 | | mysql-bin.000003 | 562 | | mysql-bin.000004 | 1144 | +------------------+-----------+ 4 rows in set (0.00 sec) mysql> show binary logs; +------------------+-----------+ | log_name | file_size | +------------------+-----------+ | mysql-bin.000001 | 143 | | mysql-bin.000002 | 261 | | mysql-bin.000003 | 562 | | mysql-bin.000004 | 1144 | +------------------+-----------+ 4 rows in set (0.00 sec)
我这里有一个备份文件就是那个创建表的sql语句,位置是mysql-bin.000004 , 4
在这个案例里我只用cover住mysql-bin.000004这个文件。
mysql>show binlog events in 'mysql-bin.000004'; +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ | log_name | pos | event_type | server_id | end_log_pos | info | +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ | mysql-bin.000004 | 4 | format_desc | 753306 | 120 | server ver: 5.6.16-64.2-rel64.2-log, binlog ver: 4 | | mysql-bin.000004 | 120 | query | 753306 | 209 | use `wubx`; truncate table tb_wubx | | mysql-bin.000004 | 209 | query | 753306 | 281 | begin | | mysql-bin.000004 | 281 | table_map | 753306 | 334 | table_id: 91 (wubx.tb_wubx) | | mysql-bin.000004 | 334 | write_rows | 753306 | 393 | table_id: 91 flags: stmt_end_f | | mysql-bin.000004 | 393 | xid | 753306 | 424 | commit /* xid=1073 */ | | mysql-bin.000004 | 424 | query | 753306 | 496 | begin | | mysql-bin.000004 | 496 | table_map | 753306 | 549 | table_id: 91 (wubx.tb_wubx) | | mysql-bin.000004 | 549 | write_rows | 753306 | 602 | table_id: 91 flags: stmt_end_f | | mysql-bin.000004 | 602 | xid | 753306 | 633 | commit /* xid=1074 */ | | mysql-bin.000004 | 633 | query | 753306 | 722 | use `wubx`; truncate table tb_wubx | | mysql-bin.000004 | 722 | query | 753306 | 794 | begin | | mysql-bin.000004 | 794 | table_map | 753306 | 847 | table_id: 92 (wubx.tb_wubx) | | mysql-bin.000004 | 847 | write_rows | 753306 | 894 | table_id: 92 flags: stmt_end_f | | mysql-bin.000004 | 894 | xid | 753306 | 925 | commit /* xid=1081 */ | | mysql-bin.000004 | 925 | query | 753306 | 997 | begin | | mysql-bin.000004 | 997 | table_map | 753306 | 1050 | table_id: 92 (wubx.tb_wubx) | | mysql-bin.000004 | 1050 | update_rows | 753306 | 1113 | table_id: 92 flags: stmt_end_f | | mysql-bin.000004 | 1113 | xid | 753306 | 1144 | commit /* xid=1084 */ | +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ 19 rows in set (0.00 sec) mysql>show binlog events in 'mysql-bin.000004'; +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ | log_name | pos | event_type | server_id | end_log_pos | info | +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ | mysql-bin.000004 | 4 | format_desc | 753306 | 120 | server ver: 5.6.16-64.2-rel64.2-log, binlog ver: 4 | | mysql-bin.000004 | 120 | query | 753306 | 209 | use `wubx`; truncate table tb_wubx | | mysql-bin.000004 | 209 | query | 753306 | 281 | begin | | mysql-bin.000004 | 281 | table_map | 753306 | 334 | table_id: 91 (wubx.tb_wubx) | | mysql-bin.000004 | 334 | write_rows | 753306 | 393 | table_id: 91 flags: stmt_end_f | | mysql-bin.000004 | 393 | xid | 753306 | 424 | commit /* xid=1073 */ | | mysql-bin.000004 | 424 | query | 753306 | 496 | begin | | mysql-bin.000004 | 496 | table_map | 753306 | 549 | table_id: 91 (wubx.tb_wubx) | | mysql-bin.000004 | 549 | write_rows | 753306 | 602 | table_id: 91 flags: stmt_end_f | | mysql-bin.000004 | 602 | xid | 753306 | 633 | commit /* xid=1074 */ | | mysql-bin.000004 | 633 | query | 753306 | 722 | use `wubx`; truncate table tb_wubx | | mysql-bin.000004 | 722 | query | 753306 | 794 | begin | | mysql-bin.000004 | 794 | table_map | 753306 | 847 | table_id: 92 (wubx.tb_wubx) | | mysql-bin.000004 | 847 | write_rows | 753306 | 894 | table_id: 92 flags: stmt_end_f | | mysql-bin.000004 | 894 | xid | 753306 | 925 | commit /* xid=1081 */ | | mysql-bin.000004 | 925 | query | 753306 | 997 | begin | | mysql-bin.000004 | 997 | table_map | 753306 | 1050 | table_id: 92 (wubx.tb_wubx) | | mysql-bin.000004 | 1050 | update_rows | 753306 | 1113 | table_id: 92 flags: stmt_end_f | | mysql-bin.000004 | 1113 | xid | 753306 | 1144 | commit /* xid=1084 */ | +------------------+------+-------------+-----------+-------------+----------------------------------------------------+ 19 rows in set (0.00 sec)
看到这个表刚开始就发生一次truncate, 那其实也可以说明我就恢复刚开始那个truncate到后来那个误操作的truncate table的语句之间的数据就是丢失的数据。
这个恢复可以从mysql-bin.000004 pos: 4到mysql-bin.000004 pos: 633 即:
mysqlbinlog --rewrite-db='wubx->re_wubx' --start-position=4 --stop-position=633 mysql-bin.000004 |mysql -s /tmp/mysql.sock re_wubx mysqlbinlog --rewrite-db='wubx->re_wubx' --start-position=4 --stop-position=633 mysql-bin.000004 |mysql -s /tmp/mysql.sock re_wubx
恢复结果如下:
mysql -s /tmp/mysql.sock re_wubx; mysql>select count(*) from tb_wubx; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.02 sec) mysql>select * from tb_wubx; +----+--------------+ | id | name | +----+--------------+ | 1 | 张三 | | 2 | 李四 | | 3 | 隔壁老王 | +----+--------------+ 3 rows in set (0.00 sec) mysql>insert into tb_wubx(name) select name from wubx.tb_wubx; query ok, 1 row affected (0.00 sec) records: 1 duplicates: 0 warnings: 0 mysql> rename table wubx.tb_wubx to wubx.bak_tb_wubx; query ok, 0 rows affected (0.04 sec) mysql> rename table re_wubx.tb_wubx to wubx.tb_wubx; query ok, 0 rows affected (0.03 sec) mysql> select * from wubx.tb_wubx; +----+--------------+ | id | name | +----+--------------+ | 1 | 张三 | | 2 | 李四 | | 3 | 隔壁老王 | | 4 | 老赵赵 | +----+--------------+ 4 rows in set (0.00 sec) mysql -s /tmp/mysql.sock re_wubx; mysql>select count(*) from tb_wubx; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.02 sec) mysql>select * from tb_wubx; +----+--------------+ | id | name | +----+--------------+ | 1 | 张三 | | 2 | 李四 | | 3 | 隔壁老王 | +----+--------------+ 3 rows in set (0.00 sec) mysql>insert into tb_wubx(name) select name from wubx.tb_wubx; query ok, 1 row affected (0.00 sec) records: 1 duplicates: 0 warnings: 0 mysql> rename table wubx.tb_wubx to wubx.bak_tb_wubx; query ok, 0 rows affected (0.04 sec) mysql> rename table re_wubx.tb_wubx to wubx.tb_wubx; query ok, 0 rows affected (0.03 sec) mysql> select * from wubx.tb_wubx; +----+--------------+ | id | name | +----+--------------+ | 1 | 张三 | | 2 | 李四 | | 3 | 隔壁老王 | | 4 | 老赵赵 | +----+--------------+ 4 rows in set (0.00 sec)
恢复完成。