首先呢,请各位注意此工具的适用范围:
1)本次应用的恢复工具仅适用与innodb存储引擎,Myisam不支持
2)Truncate tabe 不能恢复
3)Drop table 想也别想了
恢复原理:
对于INNODB存储引擎而言,DELETE操作,不是真正删除物理文件上的行,而是给删除的行添加了一个删除的标记,我们利用此工具找到那些标注了删除标记的行,然后将其存放到一个文本中去,最后通过load data恢复数据;而truncate操作,是直接将数据行清空,并非添加删除标记(查看物理文件,执行truncate的表的ibd文件会缩小,而执行了DELETE的表,甚至比之前大)
正文开始:
1、安装Percona Data Recovery Tool for InnoDB工具
好吧,总是需要先安装一些依赖包啥的
yum -y install ncurses-devel.x86_64
不安此包,会报错的,啥错,忘记写进来了
rpm -ivh glibc-static-2.17-55.el7.x86_64.rpm --force --nodeps ()
不安此包:看看啥报错:
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -static -lrt -o page_parser page_parser.c lib/tables_dict.o lib/libut.a
/usr/bin/ld: cannot find -lrt
collect2: ld 返回 1
make: *** [page_parser] 错误 1
2、正式配置安装
cd percona-data-recovery-tool-for-innodb-0.5/mysql-source/
./configure
cd ..
make
[[email protected] percona-data-recovery-tool-for-innodb-0.5]# make
gcc -I include -I mysql-source/include -I mysql-source/innobase/include -o innochecksum innochecksum.c
gcc -o ibdconnect ibdconnect.c
最后,没什么ERROR报错信息,就是正常的
3、OK,准备工作已经完成,来恢复你的数据
1)从服务器的层面提取你误删的数据
[[email protected] percona-data-recovery-tool-for-innodb-0.5]# ./page_parser -5 -f /var/lib/mysql/test/site_ksteam.ibd
[[email protected] percona-data-recovery-tool-for-innodb-0.5]# ls
check_data.c create_defs.pl ibdconnect incrementalupdate.c INSTALL mysql-source pages-1494339302 split_dump.pl
constraints_parser docs ibdconnect.c innochecksum lib page_parser pages-1494339674 tables_dict.c
[[email protected] percona-data-recovery-tool-for-innodb-0.5]# ls pages-1494339302/FIL_PAGE_INDEX/
0-2475 0-2476 0-2477
在FIL_PAGE_INDEX目录下会生成主键和索引,数据最小的是主键,后面的依次是每个列的索引,如0-2475是site_ksteam表的主键,0-2476是表的索引
2)生成site_ksteam表的表结构:
[[email protected]]# ./create_defs.pl --host localhost --port 3306 --user root --password 123456 --db test --table site_ksteam > include/table_defs.h 正常情况下没有任何的报错
3)还得再次执行make命令
[[email protected] percona-data-recovery-tool-for-innodb-0.5]# make
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -static -lrt -o page_parser page_parser.c lib/tables_dict.o lib/libut.a
最后是这条信息,就是正常的
4)将查找到的数据保存到/tmp/site_ksteam.txt文件
[[email protected]percona-data-recovery-tool-for-innodb-0.5]# ./constraints_parser -D -5 -f pages-1494339674/FIL_PAGE_INDEX/0-2351/ > /tmp/site_ksteam.txt -D 恢复删除的行;-5表的文件格式,默认Compact;
-f 指定site_ksteam表的主键目录
LOAD DATA INFILE ‘/com/percona-data-recovery-tool-for-innodb-0.5/dumps/default/site_ksteam‘ REPLACE INTO TABLE `site_ksteam` FIELDS TERMINATED BY ‘\t‘ OPTIONALLY ENCLOSED BY ‘"‘ LINES STARTING BY ‘site_ksteam\t‘ (id, catid, typeid, title, style, thumb, keywords, description, posids, url, listorder, status, sysadd, islink, username, inputtime, updatetime, contact, titles, level, cimage, bimage, language, subject);
5)连接Mysql,将数据导入表中
mysql> LOAD DATA INFILE ‘/tmp/site_ksteam.txt‘ REPLACE INTO TABLE `site_ksteam` FIELDS TERMINATED BY ‘\t‘ OPTIONALLY ENCLOSED BY ‘"‘ LINES STARTING BY ‘site_ksteam\t‘ (id, catid, typeid, title, style, thumb, keywords, description, posids, url, listorder, status, sysadd, islink, username, inputtime, updatetime, contact, titles, level, cimage, bimage, language, subject);
最后呢,还是想吐槽下,开始搞这个工具的时候,网上的资料,呵呵,人云亦云的,也不说中间可能遇到啥问题,怎么解决,最最重要的是, 此工具对在线运行的数据库进行恢复操作,会出现数据丢失现象!!!
到底是我哪里没理解到,还是本是如此,欢迎留言!