使用场景
InnoDB数据文件损坏,数据库无法启动,且无数据备份的情况下,进行数据恢复。
仅对MySQL InnoDB存储引擎数据文件有效。
所需资源
1.
InnoDB 数据文件
2.
可获取表结构的 mysql server
操作说明
1. 使用 percona-data-recovery-tool-for-innodb 构建恢复工具。主要构建出2个工具,page_parser & constraints_parser。
2. 使用 page_parser 提取需要恢复的数据页,会在当前目录下生成 pages-$timestamp 数据文件目录
[root@mysql percona-data-recovery-tool-for-innodb]# page_parser -5 -f /path/to/ibdata1
##命令使用方法
[root@mysql percona-data-recovery-tool-for-innodb]# ./page_parser -h
Error: Usage: ./page_parser -4|-5 [-dDhcCV] -f <innodb_datafile> [-T N:M] [-s size] [-t size]
Where
-h -- Print this help
-V -- Print debug information
-d -- Process only those pages which potentially could have deleted records (default = NO)
-s size -- Amount of memory used for disk cache (allowed examples 1G 10M). Default 100M
-T -- retrieves only pages with index id = NM (N - high word, M - low word of id)
-c -- count pages in the tablespace and group them by index id
-C -- count pages in the tablespace and group them by index id (and ignore too high/zero indexes)
-t size -- Size of InnoDB tablespace to scan. Use it only if the parser can't determine it by himself.
3.
生成相关表定义文件,并重新编译构建 constraints_parser 工具(需要连接数据库,有表结构即可)
[root@mysql percona-data-recovery-tool-for-innodb]# ./create_defs.pl --host=$db_host --user=$user --password=$pass --db=$database --table=$table > include/table_defs.h
[root@mysql percona-data-recovery-tool-for-innodb]# make
4.
从 pages-$timestamp 目录提取数据,可以导出到文件
[root@mysql percona-data-recovery-tool-for-innodb]# ./constraints_parser -5 -f pages-1493793157/FIL_PAGE_INDEX/0-22/0-00000003.page > datafile
LOAD DATA INFILE '/root/percona-data-recovery-tool-for-innodb/dumps/default/test' REPLACE INTO TABLE `test` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'test\t' (id, name);
[root@mysql percona-data-recovery-tool-for-innodb]# cat datafile
test 1 "test"
test 2 "test2"
test 3 "test3"
test 4 "test4"
5. 进行数据导入恢复