怎样从frm,ibd恢复MYSQL的相关推荐
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样从frm,ibd恢复MYSQL的相关推荐相关的知识,希望对你有一定的参考价值。
创建已经丢失的表结构
先要安装 mysql-utilities。
// RedHatyum -y install mysql-server mysql-utilities// Debianapt install mysql-utilities
使用 mysqlfrm 从 .frm 文件里面找回建表语句。
// 分析一个 .frm 文件生成建表的语句mysqlfrm --diagnostic /var/lib/mysql/test/t1.frm// 分析一个目录下的全部.frm文件生成建表语句root@username:~# mysqlfrm --diagnostic /var/lib/mysql/my_db/bk/ >createtb.sqlroot@username:~# grep "^CREATE TABLE" createtb.sql |wc -l124
可以看到一共生成了 124 个建表语句。
有很多时候也可以从其它库里面生成建表语句,如同一个应用的其它数据库或不同的测试环境,采用下面的 mysqldump 生成建表语句:
mysqldump --no-data --compact my_db>createtb.sql
登录 MySQL 生成表。
mysql> create database my_db;mysql> use my_dbDatabase changedmysql> source createtb.sqlQuery OK, 0 rows affected (0.07 sec)......
导入旧的数据文件
将新建的没有包括数据的 .ibd 文件抛弃
root@username:/var/lib/mysql/my_db# ll *.ibd|wc12411167941root@username:/var/lib/mysql/my_db# mysql -e "show tables from my_db" \\| grep -v Tables_in_my_db \\| while read a; do mysql -e "ALTER TABLE my_db.$a DISCARD TABLESPACE"; doneroot@username:/var/lib/mysql/my_db# ll *.ibd|wcls: cannot access '*.ibd': No such file or directory000
可以看到所有的 .idb 文件都已经被抛弃了。然后把旧的有数据的 .ibd 文件拷贝到这个 my_db 目录下面,别忘了把属主改过来:chown mysql. *,再把这些数据文件 import 到数据库中。
root@username:/var/lib/mysql/my_db# mysql -e "show tables from my_db" \\| grep -v Tables_in_my_db \\| while read a; \\do mysql -e "ALTER TABLE my_db.$a import TABLESPACE"; done
导入完成后检查表
使用 mysqlcheck 对数据库 my_db 下的所有表进行检查:
root@username:/var/lib/mysql/my_db# mysqlcheck -c my_dbmy_db.cdp_backup_point OK......
所有的表都导入成功。
1. Start up clean/fresh instance of MySQL with innodb_file_per_table enabled.
2. Now, we need to find the table id that MySQL is currently set at, as well as the table id for the table we need to recover.
Note:
Step 2 (2a – 2f) is simply to find the table id that is stored inside of
the .ibd file. I’ve written a php script to determine this, so using
the script can save a bunch of time. See the bottom of this page (under
“Associated Files”) for the exact script.
2a. Create a test database:
mysql> CREATE DATABASE test1;
mysql> USE test1;
2b. Issue the create table command for the table:
mysql> CREATE TABLE `product` (
`PRODUCT_ID` bigint(20) unsigned NOT NULL auto_increment,
`BRAND_ID` int(10) unsigned default NULL,
`PRODUCT_TYPE_ID` int(10) unsigned default NULL,
`GROUP_ID` int(10) unsigned default NULL,
`PRODUCT_NAME` varchar(500) NOT NULL,
`DEFAULT_EMAIL_ID` varchar(48) default NULL,
`PRODUCT_STATUS` tinyint(1) NOT NULL,
`CLIENT_ID` bigint(20) unsigned default NULL,
`LAST_MODIFIED_BY` varchar(45) NOT NULL,
`LAST_MODIFIED_DATE` datetime NOT NULL,
PRIMARY KEY (`PRODUCT_ID`)
) ENGINE=InnoDB;
2c. Discard the tablespace, which will delete the newly created .ibd file:
mysql> ALTER TABLE product DISCARD TABLESPACE;
2d. Copy the pre-existing .ibd file to the datadir/test1 folder
2e. Import this tablespace:
mysql> ALTER TABLE product IMPORT TABLESPACE;
This should produce the following error (at least this is most likely).
The only way it would not is if MySQL’s current table id matched that of
the preexisting ibd table id. In which case, you can now dump your
table.
ERROR 1030 (HY000): Got error -1 from storage engine
2f. So, now to check the error log (manually). Look for the following entry:
081010 11:47:40 InnoDB: Error: tablespace id in file
'.test1product.ibd' is 1193, but in the InnoDB
InnoDB: data dictionary it is 1.
So, now we know the internal table id is at 1, and that of the ibd table is 1193.
3. Clean up working database:
3a. Manually move the ibd file from the $datadir to a safe location (as you will need this file again).
3b. Drop this table.
mysql> DROP TABLE product;
Note this does not re-set the internal table counter.
4. You’ll need to create the number of tables you need to increase the internal table id value.
In this case, you’d create 1191 test InnoDB tables (already at 1, and
need to leave 1 for the actual table, so 1193-2=1191). Run below in a
loop.
for ($1=1; $i<=1191; $1++)
CREATE TABLE t# (id int) ENGINE=InnoDB;
I accomplished this via a simple php script. See the bottom of this page (under "Associated Files") for the exact script.
5. After these are created, go ahead and drop this database and all tables (as they are not needed).
DROP DB test1;
6. Now, re-perform steps 2a through 2e.
mysql> CREATE DATABASE test1;
mysql> USE test1;
mysql> CREATE TABLE `product` ( ... ) ENGINE=InnoDB;
mysql> ALTER TABLE product DISCARD TABLESPACE;
<-- Here is where you copy back the original ibd file to /$datadir/test1/ -->
mysql> ALTER TABLE product IMPORT TABLESPACE;
Success!本回答被提问者和网友采纳
如何使用frm和ibd文件恢复mysql数据库
一.首先从数据库data目录下将对应数据库文件夹下的frm文件提取表结构创建sql语句
通过工具mysqlfrm提取。安装如下:
依次执行以下命令
cd /usr/local/src/
wget https://downloads.mysql.com/a...
tar -zxvf mysql-utilities-1.6.5.tar.gz
查看python版本,需要2.6以上
python -V
cd mysql-utilities-1.6.5
编译安装
python ./setup.py build
python ./setup.py install
将一个目录下的全部.frm文件生成建表语句
mysqlfrm --diagnostic /usr/local/mysql/data/data1/ >createtable.sql
二.创建数据库,并执行sql语句创建表
三.解除所有表的表空间绑定 语句如下:
ALTER TABLE test DISCARD TABLESPACE;
四.将对应的ibd文件放到新建的数据库data目录下
五.绑定表空间,语句如下
ALTER TABLE test IMPORT TABLESPACE;
中间可能会有因为mysql版本不同出现的问题,对建表语句做对应调整
以上是关于怎样从frm,ibd恢复MYSQL的相关推荐的主要内容,如果未能解决你的问题,请参考以下文章