MySQL 从 frm 和 ibd 文件恢复表结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26868956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:10:49  来源:igfitidea点击:

Restore table structure from frm and ibd files

mysqlsqlphpmyadmininnodb

提问by fraggley

I am trying to restore a database in PMA but only have access to frm and ibd files - not the ib_log files which I understand you need.

我正在尝试在 PMA 中恢复数据库,但只能访问 frm 和 ibd 文件 - 而不是我理解您需要的 ib_log 文件。

I know I may not be able to recover the database data but is it possible to recover the structure of the tables from the frm files?

我知道我可能无法恢复数据库数据,但是否可以从 frm 文件中恢复表的结构?

回答by Jin Lin

I restored the table from only .frmand .idbfiles.

我只从.frm.idb文件恢复了表。

Get the SQL query to create the tables

获取 SQL 查询以创建表

If you already know the schema of your tables, you can skip this step.

如果您已经知道表的架构,则可以跳过此步骤。

  1. First, install MySQL Utilities. Then you can use mysqlfrmcommand in command prompt (cmd).

  2. Second, get the SQL queries from .frmfiles using mysqlfrmcommand:

    mysqlfrm --diagnostic <path>/example_table.frm
    
  1. 首先,安装MySQL Utilities。然后您可以mysqlfrm在命令提示符(cmd)中使用命令。

  2. 其次,.frm使用mysqlfrm命令从文件中获取 SQL 查询:

    mysqlfrm --diagnostic <path>/example_table.frm
    

Then you can get the SQL query to create same structured table. Like this:

然后您可以获取 SQL 查询以创建相同的结构化表。像这样:

CREATE TABLE `example_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `photo_url` varchar(150) NOT NULL,
  `password` varchar(600) NOT NULL,
  `active` smallint(6) NOT NULL,
  `plan` int(11) NOT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

Create the tables

创建表

Create the table(s) using the above SQL query.

使用上述 SQL 查询创建表。

If the old data still exists, you may have to drop the respective database and tables first. Make sure you have a backup of the data files.

如果旧数据仍然存在,您可能必须先删除相应的数据库和表。确保您有数据文件的备份。

Restore the data

恢复数据

Run this query to remove new table data:

运行此查询以删除新表数据:

ALTER TABLE example_table DISCARD TABLESPACE;

This removes connections between the new .frmfile and the (new, empty) .idbfile. Also, remove the .idbfile in the folder.

这将删除新.frm文件和(新的,空的).idb文件之间的连接。另外,删除.idb文件夹中的文件。

Then, put the old .idbfile into the new folder, e.g.:

然后,将旧.idb文件放入新文件夹中,例如:

cp backup/example_table.ibd <path>/example_table.idb

Make sure that the .ibdfiles can be read by the mysqluser, e.g. by running chown -R mysql:mysql *.ibdin the folder.

确保用户.ibd可以读取文件mysql,例如通过chown -R mysql:mysql *.ibd在文件夹中运行。

Run this query to import old data:

运行此查询以导入旧数据:

ALTER TABLE example_table IMPORT TABLESPACE;

This imports data from the .idbfile and will restore the data.

这将从.idb文件中导入数据并将恢复数据。

回答by Bill Karwin

InnoDB needs the ib_log files for data recovery, but it also needs the ibdata1 file which contains the data dictionary and sometimes contains pending data for the tables.

InnoDB 需要 ib_log 文件进行数据恢复,但它也需要包含数据字典的 ibdata1 文件,有时还包含表的待处理数据。

The data dictionary is kind of a duplicate system that records table structure and also matches a table id to the physical .ibd file that contains the table data.

数据字典是一种重复系统,它记录表结构,并将表 ID 与包含表数据的物理 .ibd 文件相匹配。

You can't just move .ibd files around without the InnoDB data dictionary, and the data dictionary must match the table id found inside the .ibd file. You canreattach a .ibd file and recover the data, but the procedure is not for the faint of heart. See http://www.chriscalender.com/recovering-an-innodb-table-from-only-an-ibd-file/

您不能在没有 InnoDB 数据字典的情况下移动 .ibd 文件,并且数据字典必须与在 .ibd 文件中找到的表 ID 匹配。您可以重新附加 .ibd 文件并恢复数据,但该过程不适合胆小的人。见http://www.chriscalender.com/recovering-an-innodb-table-from-only-an-ibd-file/

You can recover the structure using the .frm files with some file trickery, but you will not be able to create them as InnoDB tables at first. Here's a blog that covers a method for recovering .frm files as MyISAM tables: http://www.percona.com/blog/2008/12/17/recovering-create-table-statement-from-frm-file/

您可以使用带有一些文件技巧的 .frm 文件来恢复结构,但是一开始您将无法将它们创建为 InnoDB 表。这是一个博客,涵盖了将 .frm 文件恢复为 MyISAM 表的方法:http: //www.percona.com/blog/2008/12/17/recovering-create-table-statement-from-frm-file/

You won't be able to use PMA for this. You need superuser access to the data directory on the server.

您将无法为此使用 PMA。您需要超级用户访问服务器上的数据目录。

回答by igorsf

You can recover table structure from .frm files and data from ibd files.

您可以从 .frm 文件和 ibd 文件中恢复表结构。

Using mysqlfrm tool which is part of MySQL Utilities

使用 mysqlfrm 工具,它是MySQL Utilities 的一部分

shell> mysqlfrm --diagnostic myfile.frm

shell> mysqlfrm --diagnostic myfile.frm

Recreate the table in a database of the same name using table structure.

使用表结构在同名数据库中重新创建表。

mysql> CREATE mytable (int i);

mysql> CREATE mytable (int i);

Discard the tablespace of the newly created table.

丢弃新创建的表的表空间。

mysql> ALTER TABLE mytable DISCARD TABLESPACE;

mysql> ALTER TABLE mytable DISCARD TABLESPACE;

Copy the orphan .idb file from your backup directory to the new database directory. Ensure that the .ibd file has the necessary file permissions.

将孤立的 .idb 文件从备份目录复制到新的数据库目录。确保 .ibd 文件具有必要的文件权限。

Import the orphan .ibd file. A warning is issued indicating that InnoDB will attempt to import the file without schema verification.

导入孤立的 .ibd 文件。发出警告,指示 InnoDB 将尝试在没有模式验证的情况下导入文件。

mysql> ALTER TABLE r IMPORT TABLESPACE;SHOW WARNINGS;

mysql> ALTER TABLE r IMPORT TABLESPACE;SHOW WARNINGS;

回答by Marcus Edensky

This is actually quite easy as long as you know how to do it, and no external software or shell commands are needed.

只要您知道如何操作,这实际上很容易,并且不需要外部软件或 shell 命令。

The database data is stored in C:\xampp\mysql\data\ or similar by default. The folders are the database tables. Inside each folder, the .frm file are the columns. The .ibd hold the row values.

数据库数据默认存放在 C:\xampp\mysql\data\ 或类似位置。文件夹是数据库表。在每个文件夹中,.frm 文件是列。.ibd 保存行值。

First create the database(s) in PHPMyAdmin.

首先在 PHPMyAdmin 中创建数据库。

Get the SQL query generated from this site, under menu Recover structure > From .frm file:

获取从该站点生成的 SQL 查询,在菜单 Recover structure > From .frm 文件下:

https://recovery.twindb.com/

https://recovery.twindb.com/

Upload each .frm file, and then copy and paste these queries into the SQL command to create the tables in PHPMyAdmin.

上传每个 .frm 文件,然后将这些查询复制并粘贴到 SQL 命令中以在 PHPMyAdmin 中创建表。

Then, on each table, do this SQL query:

然后,在每个表上,执行以下 SQL 查询:

ALTER TABLE table_name DISCARD TABLESPACE

This will automatically remove the new .ibd file from the database directory. Copy the old .ibd file into the database folder. Run the following command to activate the table again:

这将自动从数据库目录中删除新的 .ibd 文件。将旧的 .ibd 文件复制到数据库文件夹中。运行以下命令再次激活表:

ALTER TABLE table_name IMPORT TABLESPACE

And that's it! You should be able to view and access all of your old values again.

就是这样!您应该能够再次查看和访问所有旧值。

回答by Md Tahmilur Rahman

You can also try with mysql utility.

您也可以尝试使用 mysql 实用程序。

From book.frm file to a file book.sql:

从 book.frm 文件到文件 book.sql:

mysqlfrm --server=root:mysqladmin@localhost:3306 D:\yahwehdb\yahweh_altera\book.frm > D:\yahwehdb\yahweh_altera\book.frm\book.sql --diagnostic --port=3307 --user=root

From a directory containing all .frm file to a file all.sql:

从包含所有 .frm 文件的目录到文件 all.sql:

mysqlfrm --server=root:mysqladmin@localhost:3306 D:\yahwehdb\yahweh_altera > D:\yahwehdb\yahweh_altera\all.sql --diagnostic --port=3307 --user=root