本文共 4467 字,大约阅读时间需要 14 分钟。
环境: centos 7.4+ mysql 5.7.19
在无备份的情况下数据库损坏时,可以从磁盘文件中恢复出数据,不一定百分百成功,使用的工具是undrop-for-innodb-master.zip
#yum localinstall mysql-connector-python-2.1.7-1.el7.x86_64 -y#yum localinstall autoconf mysql-utilities-1.5.6-1.el7.noarch -y
#service mysql start#mysqlfrm --server=root:root@localhost:3306 --port=3310 --user=mysql /root/a.frm #3310这个端口可以随便写,但是这个端口不能被占用. root:root@localhost:3306,这个表示登录本机mysql的用户名密码和端口信息WARNING: Using a password on the command line interface can be insecure.# Source on localhost: ... connected.# Spawning server with --user=mysql.# Starting the spawned server on port 3310 ... done.# Reading .frm files## Reading the a.frm file.## CREATE statement for ./a.frm:#CREATE TABLE `a` ( `id` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4#...done.[root@slave1 ~]# mysqlfrm --server=root:root@localhost:3306 --diagnostic --show-stats --port=3310 --user=mysql ./servers.frm ########diagnostic 模式
创建测试表,插入数据
[root@master ~]# yum -y install flex bison[root@master ~]# unzip undrop-for-innodb-master.zip[root@master ~]# makemysql> select * from test.aa;| id | name |+----+------+| 1 | guo || 2 | jia || 3 | tt |mysql> drop table aa;
[root@master ~]# service mysql stop
[root@master ~]# cd undrop-for-innodb-master/[root@master undrop-for-innodb-master]# cat aa.sqlCREATE TABLE `aa` ( `id` int(11) NOT NULL, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
[root@master ~]# cp /data/mysql/ibdata1 undrop-for-innodb-master/ibdata1[root@master ~]# cd undrop-for-innodb-master/[root@master ~]# ./stream_parser -f ./ibdata1可以看到当前目录下生成了pages-ibdata1文件夹:[root@master undrop-for-innodb-master]# ll pages-ibdata1/ drwxr-xr-x 2 root root 4096 Oct 9 11:06 FIL_PAGE_INDEXdrwxr-xr-x 2 root root 4096 Oct 9 11:06 FIL_PAGE_TYPE_BLOFIL_PAGE_INDEX为索引好的数据页目录,我们要恢复的数据就在这个文件夹里面.FIL_PAGE_TYPE_BLOB: 如果遇上较大的数据(例如comments里有text类型的数据并且内容较多),InnoDB会使用BLOB类PAGE存储数据。需检查此目录是否有文件。如有,说明此表使用了BLOB,之后提取命令需要用-b参数指定此目录进行提取
[root@master undrop-for-innodb-master]# ./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page -t dictionary/SYS_TABLES.sql |grep aaSET FOREIGN_KEY_CHECKS=0;LOAD DATA LOCAL INFILE '/root/undrop-for-innodb-master/dumps/default/SYS_TABLES' REPLACE INTO TABLE `SYS_TABLES` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'SYS_TABLES\t' (`NAME`, `ID`, `N_COLS`, `TYPE`, `MIX_ID`, `MIX_LEN`, `CLUSTER_NAME`, `SPACE`);0000011FCC26 3C0000012E19FE SYS_TABLES "test/aa" 379 2 33 0 80 "" 527
aa表结构在"dictionary/SYS_INDEXES.sql"文件中可以看到,而此表对应的数据页文件是第三个数据页0000000000000003.page,于是:[root@master undrop-for-innodb-master]# ./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page -t dictionary/SYS_INDEXES.sql |grep 379SET FOREIGN_KEY_CHECKS=0;LOAD DATA LOCAL INFILE '/root/undrop-for-innodb-master/dumps/default/SYS_INDEXES' REPLACE INTO TABLE `SYS_INDEXES` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'SYS_INDEXES\t' (`TABLE_ID`, `ID`, `NAME`, `N_FIELDS`, `TYPE`, `SPACE`, `PAGE_NO`);0000011FCC26 3C0000012E1957 SYS_INDEXES 379 479 "PRIMARY" 1 3 527 4294967295
找到了aa表的主键索引信息后,其对应在mysql存储中的索引值为479,该索引标号对应的数据页文件中即存储了该索引的全部数据,于是扫描全盘找到对应的索引文件:(如果pages-ibdata1目录下存在479号索引文件,那就不需要再进行磁盘扫描,可以跳过步骤6)[root@master undrop-for-innodb-master]# df -hFilesystem Size Used Avail Use% Mounted on/dev/vda1 99G 83G 11G 89% /devtmpfs 3.9G 0 3.9G 0% /devtmpfs 3.9G 0 3.9G 0% /dev/shm[root@master undrop-for-innodb-master]# ./stream_parser -f /dev/vda1 -t 70000000k #mysql的数据存储在这个分区里面,所以需要扫描这个分区的所有页文件
[root@master undrop-for-innodb-master]# ./c_parser -6f pages-vda/FIL_PAGE_INDEX/0000000000000479.page -t aa.sql #aa.sql是最开始时候创建的表结构sql语句[root@master undrop-for-innodb-master]# ./c_parser -6f pages-vda/FIL_PAGE_INDEX/0000000000000479.page -t aa.sql > dumps/default/t_aa 2> dumps/default/t_aa.sql[root@master undrop-for-innodb-master]# mysql -uroot -p123456 test> source /root/undrop-for-innodb-master/aa.sql;> source /root/undrop-for-innodb-master/dumps/default/t_aa.sql;> select * from aa;| id | name |+----+------+| 1 | guo || 2 | jia || 3 | tt |至此数据全部恢复成功!
转载地址:http://xdjla.baihongyu.com/