IE盒子

搜索
查看: 121|回复: 1

Mysql之利用frm和idb文件进行数据恢复

[复制链接]

3

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2023-1-12 12:45:25 | 显示全部楼层 |阅读模式
Mysql之利用frm和idb文件进行数据恢复(当前Mysql版本是5.7)
需要恢复的数据文件:test.frm、test.ibd




现尝试在其mysql 5.7上进行恢复。具体如下:
1.创建一个数据库、表。并查看创建的表信息

mysql> create database data_backup;
No connection. Trying to reconnect...
Connection id:    5

Query OK, 1 row affected (0.00 sec)


mysql> use data_backup;
Database changed
mysql> create table test(a int);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test\G;
*************************** 1. row***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified

2.拷贝需要恢复的frm文件到对应位置,并覆盖

这里是将需要恢复的frm文件,拷贝到步骤1新建的数据库表(C:\ProgramData\MySQL\MySQL Server 5.7\Data\data_backup)
3.覆盖之后,查看表结构信息,并关闭打开的表,并且刷新查询缓存。

mysql> show create table test\G;
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> flush table test;
Query OK, 0 rows affected (0.00 sec)

mysql> show create table test\g;
ERROR 1146 (42S02): Table 'data_backup.test' doesn't exist
ERROR:
No query specified4.此时报错,表报错,意料中的事情,

查看mysql的错误日志(C:\ProgramData\MySQL\MySQL Server 5.7\Data\*.err),发现对于InnoDB存储引擎,表data_backup.test 定义了1个列,但是MySQL中有6列。
2022-06-22T05:54:02.574668Z 7 [Warning] InnoDB: Table data_backup/test contains 1 user defined columns in InnoDB, but 6 columns in MySQL. Please check INFORMATION_SCHEMA.INNODB_SYS_COLUMNS and http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue.
5.删除原来的表,建立相同个数的列,名称和类型随意。

mysql> drop table test;
Query OK, 0 rows affected (0.01 sec)

mysql> create table test(a1 int,a2 int, a3 int,a4 int, a5 int,a6 int);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test\g;
+-------+------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                              |
+-------+------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `a1` int(11) DEFAULT NULL,
  `a2` int(11) DEFAULT NULL,
  `a3` int(11) DEFAULT NULL,
  `a4` int(11) DEFAULT NULL,
  `a5` int(11) DEFAULT NULL,
  `a6` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------+
1 row in set (0.00 sec)6. 重复第二步,覆盖frm文件(需要恢复的frm文件,拷贝到步骤1新建的数据库表

(C:\ProgramData\MySQL\MySQL Server 5.7\Data\data_backup),就可以获得定义语句,结果与第一部分得出的一样
mysql> show create table test\g;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                              |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `a1` int(11) DEFAULT NULL,
  `a2` int(11) DEFAULT NULL,
  `a3` int(11) DEFAULT NULL,
  `a4` int(11) DEFAULT NULL,
  `a5` int(11) DEFAULT NULL,
  `a6` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> flush table test;
Query OK, 0 rows affected (0.00 sec)

mysql> show create table test\g;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                           |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `weight` double DEFAULT NULL,
  `height` double DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.07 sec)注意啦!!!:6.1.如果还原之后发现还原的数据乱麻麻的!!!,请做如下操作
将新建的表删除,并根据6步骤得到的表结构信息,重新建一个表:
drop table test;

CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `weight` double DEFAULT NULL,
  `height` double DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf87.替换idb文件。

7.1.首先执行下面语句,数据库中的ibd文件会被删除。
alter table test_innodb discard tablespace;7.2.然后将要还原的ibd文件复制到指定路径下(就是刚刚新建表的路径下!!!顺序不要错了!!!)
8.恢复ibd文件。执行下面语句即可完成恢复

【import命令一直报错表不存在,后来发现可以用chown -R mysql.mysql table_name.ibd给mysql账户赋予恢复权限后就可以了(可以vi /etc/passwd看下是否有mysql账户)】
alter table test_innodb import tablespace;
回复

使用道具 举报

2

主题

5

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2025-5-27 11:16:23 | 显示全部楼层
这么强,支持楼主,佩服
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表