mysql innodb只有 .frm .ibd 文件 如何恢复数据

来源:互联网 发布:兰州seo安安网站建设 编辑:程序博客网 时间:2024/05/17 08:37

今天同学给我发信息说mysql库坏了,问怎么恢复,然后给我发来两个文件attachment.frm attachment.ibd文件,让我帮忙恢复一下数据,现在对这个恢复过程记录一下:

1.确认innodb_file_per_table = 1

这里恢复方法只适用innodb_file_per_table = 1也就是独立表空间的表,不适用公用表空间的表。

这个可以从配置文件里查看,或者登录原mysql执行show variables like '%innodb_file_per_table%';查看,为1或为on。


2.确认mysql版本

确认原mysql的版本,恢复使用的mysql最好与原mysql是同一个版本,查看版本的方式有许多,例如:select version();

我同学的mysql是5.1的,我没有那么古老的版本实例,就用了5.6,结果发现也可以。


3.恢复表结构

在恢复实例上创建一个全新的数据库test2:

mysql> create database test2;

创建与文件名一样的表,表的字段随便创建一个就行,因为我们也不知道原表都有什么字段:

mysql> CREATE TABLE `attachment`( `weiboid` bigint(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8;


4.关闭mysql

/etc/init.d/mysql stop


5.复制文件

将同学发来的要恢复的attachment.frm文件复制到test2的数据目录下,覆盖掉原来的attachment.frm文件

[root@test1 test2]# mv attachment.frm attachment.frm.bak

[root@test1 test2]# cp ../attachment.frm ./ 

注意一定要修改文件的权限:
[root@test1 test2]# chown mysql.mysql attachment.frm
[root@test1 test2]# ll
total 120
-rw-r--r-- 1 mysql mysql  8979 Aug 12 21:55 attachment.frm
-rw-rw---- 1 mysql mysql  8870 Aug 13 00:38 attachment.frm.bak
-rw-rw---- 1 mysql mysql 98304 Aug 13 00:38 attachment.ibd


6.修改my.cnf

vi /etc/my.cnf

#在[mysqld]项下增加以下参数

innodb_force_recovery = 6


7.重启mysql

/etc/init.d/mysql start


8.查看表结构

mysql> use test2

mysql> show create table attachment;
ERROR 1146 (42S02): Table 'test2.attachment' doesn't exist

报错,去看看错误日志,发现问题还不少,有如下一段信息:

2016-08-13 00:33:26 31238 [ERROR] InnoDB: Failed to find tablespace for table '"test2"."attachment"' in the cache. Attempting to load the tablespace with space id 36.
2016-08-13 00:33:26 31238 [Warning] InnoDB: table test2/attachment contains 1 user defined columns in InnoDB, but 9 columns in MySQL. Please check INFORMATION_SCHEMA.INNODB_SYS_COLUMNS and http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how to resolve it
2016-08-13 00:33:26 31238 [Warning] InnoDB: Cannot open table test2/attachment from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
2016-08-13 00:33:41 7f2441eb5700  InnoDB: table "test2"."attachment"is corrupted. Please drop the table and recreate
2016-08-13 00:33:41 31238 [Warning] InnoDB: Cannot open table test2/attachment from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.

标红的信息说attachment表只定义了一列但现在发现了9列。没关系我们只需重建一个有9列的attachment表就行,列名和类型等随意。

关闭mysql,将innodb_force_recovery = 6注释掉或=0,重启mysql,然后重建attachment表:

mysql> drop table attachment;
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> CREATE TABLE `attachment`( 
    -> `weiboid` bigint(20),
    -> `weiboid2` bigint(20),
    -> `weiboid3` bigint(20),
    -> `weiboid4` bigint(20),
    -> `weiboid5` bigint(20),
    -> `weiboid6` bigint(20),
    -> `weiboid7` bigint(20),
    -> `weiboid8` bigint(20),
    -> `weiboid9` bigint(20),
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.04 sec)

再关闭mysql,复制attachment.frm文件,修改参数innodb_force_recovery = 6,重启mysql,再查看表结构:

mysql> use test2

mysql> show create table attachment;

mysql> show create table attachment\G
*************************** 1. row ***************************
       Table: attachment
Create Table: CREATE TABLE `attachment` (
  `AID` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ؔնΨһID',
  `POSITION` tinyint(3) unsigned NOT NULL COMMENT 'λ׃',
  `MODULE` tinyint(3) unsigned NOT NULL COMMENT '¶Փ¦attachment_module±ģ¿鶄module_idºç,
  `YM` char(4) NOT NULL COMMENT 'ŪՂ',
  `ATTACH_ID` int(11) NOT NULL COMMENT '¸½¼þID',
  `ATTACH_FILE` varchar(200) NOT NULL COMMENT '¸½¼þτ¼þ',
  `ATTACH_NAME` varchar(200) NOT NULL COMMENT '¸½¼þĻ³ħ,
  `ATTACH_SIGN` bigint(20) NOT NULL COMMENT '±꽇',
  `DEL_FLAG` tinyint(3) unsigned NOT NULL COMMENT 'ɾ³
0 0
原创粉丝点击