您的位置:首页 > 数据库 > MySQL

【MySQL】InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

2015-08-20 10:14 996 查看
参考:/article/3471259.html

参考:/article/1241082.html

注意!此方法只适用于innodb_file_per_table独立表空间的InnoDB实例。

此种方法可以恢复ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据的100%恢复,目的是尽可能多的恢复。

恢复数据前提是表结构定义文件frm可以使用,如果有下面报错就比较麻烦,需要手动恢复frm文件。

我的链接:/article/7149969.html

150821 16:31:27 [ERROR] /usr/local/mysql51/libexec/mysqld: Incorrect information in file: './t/test1.frm'


InnoDB引擎ibdata和ibd文件结构

结构图:https://github.com/jeremycole/innodb_diagrams





恢复原理:

因为配置innodb_file_per_table的独立表空间后,InnoDB引擎表数据、索引保存在ibd文件,ibdata文件只负责undo、double write、insert buffer...一些环节。

当然如果MySQL是异常关闭同时ibdata损坏的情况下会丢失一部分数据,但如果数据库是单点,那尽量能恢复多少是多少,至少比数据全部丢失好很多。

ibdata文件中有一个数据字典data dictionary,记录的是实例中每个表在ibdata中的一个逻辑位置,而在ibd文件中也存储着同样的一个tablespace id,两者必须一致InnoDB引擎才能正常加载到数据,否则会报错:

2015-08-18 10:30:30 12571 [ERROR] InnoDB: Error: tablespace id in file ‘.\test\test1.ibd’ is 112, but in the InnoDB InnoDB: data dictionary it is 1


实际上我们对于ibdata文件中的 undo、double write、insert buffer数据可以并不担心,我们只需要利用一个空的实例,一个干净的ibdata文件,通过卸载和加载表空间把ibd文件与ibdata文件关联。

恢复步骤:

准备一台新实例

1、建表,在新实例中建需要恢复表的表名临时表。

这块建议一次性将表都建好,可以统一检查frm文件是否有损坏,注意字符集。

#循环建表
[root@test1 db1]$ for i in `ls | grep ".ibd" | awk -F '.' '{print $1}'`;do mysql -uroot -p -S /tmp/mysql.sock -e "use test;create table ${i} (a int)engine=innodb default charset=utf8"; done


2、停止实例,添加配置innodb_force_recovery = 6

3、替换frm文件

#备份新表frm
[root@test1 test]$ cp ./*.frm ./bak
[root@test1 test]$ ls ./bak
#删除新表frm,将需要恢复表的frm复制到test目录
[root@test1 test]$ rm -rf ./*.frm
[root@test1 db1]$ for i in `ls | grep ".frm" | awk -F '.' '{print $1}'`;do cp $i.frm ../db1/;done


4、启动实例,检查表

#循环检查表是否能够打开
[root@test1 db1]$ for i in `ls | grep ".ibd" | awk -F '.' '{print $1}'`;do mysql -uroot -p -S /tmp/mysql.sock -e "use test;show create table $i \G" --default-character-set=utf8 >> ./build1.txt 2>&1 ;done


如果在输出文件中出现以下错误则需要修复frm文件,没有错误可以继续。修复frm见帖子开始的链接。

150821 16:31:27 [ERROR] /usr/local/mysql51/libexec/mysqld: Incorrect information in file: './t/test1.frm'


5、获取ibd文件中的tablespace id

ibd文件需要hexdump打开,ibd文件的0x24,0x25位置记录的是该表的tablespace id,我们可以通过脚本一次性获取所有表的id。

[root@test1 db1]$ for i in `ls | grep ".ibd" | awk -F '.' '{print $1}'`;do a=`hexdump -C $i.ibd |head -n 3 |tail -n 1|awk '{print $6$7}'`;mysql -uroot -p -S /tmp/mysql.sock -e "select conv('$a',16,10)" | grep -v conv >> ./id.txt 2>&1;done


然后按照id从小到大排序,因为后面需要按照id从小到大恢复,不用反复重做新实例。

6、去掉innodb_force_recovery = 6配置,重启生效

7、建表生成tablespace id

这里注意,如果ibd文件中的tablespace id是5001,那么就需要建5000个临时表。

另外注意建表后系统的openfile可能会很大,需要先修改系统的参数,或者建和删表可以一起做。

[root@test1 db1]$ for i in {1..5000};do mysql -uroot -p -S /tmp/mysql.sock -e "use tmp_table;create table table_${i} (a int)engine=innodb default charset=utf8"; done


8、建需要恢复的表结构

9、卸载表空间

alter table table_name discard tablespace;


10、替换ibd文件

11、加载表空间

alter table table_name import tablespace;


官方对于卸载表和加载表的说明:

ALTER TABLE tbl_name DISCARD TABLESPACE;
This deletes the current .ibd file, so be sure that you have a backup first. Attempting to modify the table contents while the tablespace file is discarded results in an error. You can perform the DDL operations listed in Section 14.10, “InnoDB and Online DDL” while the tablespace file is discarded.
To import the backup .ibd file back into the table, copy it into the database directory, and then issue this statement:
ALTER TABLE tbl_name IMPORT TABLESPACE;
The tablespace file need not necessarily have been created on the server into which it is imported later. In MySQL 5.6, importing a tablespace file from another server works if the both servers have GA (General Availablility) status and their versions are within the same series. Otherwise, the file must have been created on the server into which it is imported.


按照以上步骤就可以把数据读取出来,然后使用mysqldump导出。

如果字符集不一致或者字段类型不一致可能读取出来的数会出现数据错误、乱码或者串列。

MySQL 5.6对于表结构要求很严格,如果字段类型与原表不一致会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: