AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 16875
Accepted
Tong Wang
Tong Wang
Asked: 2012-04-22 19:59:53 +0800 CST2012-04-22 19:59:53 +0800 CST 2012-04-22 19:59:53 +0800 CST

从 .frm 和 .ibd 文件恢复表?

  • 772

我之前保存了 /var/lib/mysql/ddms 目录的副本(“ddms”是模式名称)。现在我通过运行在新安装的 Ubuntu 10.04.3 LTS 上安装了一个新的 MySQL apt-get install mysql-server,我相信已经安装了 5.1 版。在我复制 /var/lib/mysql 下的 ddms 目录后,它的一些表工作正常,这些表具有一组关联的三个文件:一个 .frm 文件、一个 .MYD 文件和一个 .MYI 文件。

但是,有两个表具有不同的文件集:一个 .frm 文件和一个 .ibd 文件。这两个表没有出现在 phpMyAdmin 的表列表中。当我查看错误日志时,它说:

[ERROR] Cannot find or open table ddms/dictionary_item from
the internal data dictionary of InnoDB though the .frm file for the
table exists. Maybe you have deleted and recreated InnoDB data
files but have forgotten to delete the corresponding .frm files
of InnoDB tables, or you have moved .frm files to another database?
or, the table contains indexes that this version of the engine
doesn't support.

请帮助恢复这两个表。谢谢。

mysql innodb
  • 10 10 个回答
  • 346407 Views

10 个回答

  • Voted
  1. Best Answer
    RolandoMySQLDBA
    2012-04-24T11:33:26+08:002012-04-24T11:33:26+08:00

    InnoDB 表不能像 MyISAM 表一样被复制。

    只是将 .frm 和 .ibd 文件从一个位置复制到另一个位置是自找麻烦。仅当且仅当您可以保证 .ibd 文件的表空间 id 与 ibdata1 文件的元数据中的表空间 id 条目完全匹配时,复制 InnoDB 表的 .frm 和 .ibd 文件才有效。

    我在 DBA StackExchange 上写了两篇关于这个表空间 id 概念的文章

    • InnoDB 中的表压缩?(在“恢复数据库”标题下)
    • 如何恢复文件被移动的 InnoDB 表

    这是关于如何在表空间 id 不匹配的情况下将任何 .ibd 文件重新附加到 ibdata1 的绝佳链接:http: //www.chriscalender.com/ ?tag=innodb-error-tablespace-id-in-file 。阅读本文后,您应该立即意识到复制 .ibd 文件简直是疯了。

    您可以应用 Chris Calendar 链接中的建议,或者您可以回到 mysql 的旧安装,启动 mysql,然后 mysqldumpddms数据库。然后,将该 mysqldump 导入新的 mysql 实例。相信我,这会容易得多。

    • 51
  2. Jordan Ryan
    2013-05-24T09:18:40+08:002013-05-24T09:18:40+08:00

    我最近遇到了同样的问题。这是我用来解决它的步骤,而不必像上面提到的 RolandoMySQLDBA 那样弄乱表空间 id。我在 Mac 上,所以我使用 MAMP 将数据库恢复到可以将其导出到 MySQL 转储中的位置。

    你可以在这里阅读完整的博客文章:http ://www.quora.com/Jordan-Ryan/Web-Dev/How-to-Recover-innoDB-MySQL-files-using-MAMP-on-a-Mac

    你必须有:

    -ibdata1

    -ib_logfile0

    -ib_logfile1

    -.FRM 来自 mysql_database 文件夹的文件

    - 全新安装您愿意销毁的 MAMP / MAMP Pro(如果需要)

    1. 通过 SSH 连接到您的 Web 服务器(开发、生产、没有区别)并浏览到您的 mysql 文件夹(我的文件夹位于 /var/lib/mysql 以在 Linux 上安装 Plesk)
    2. 压缩mysql文件夹
    3. 下载 mysql 文件夹的存档,该文件夹应包含所有 mySQL 数据库,无论是 MyISAM 还是 innoDB(如果需要,您可以 scp 此文件,或将其移动到可下载的目录)
    4. 安装 MAMP(Mac、Apache、MySQL、PHP)
    5. 浏览到 /Applications/MAMP/db/mysql/
    6. 将 /Applications/MAMP/db/mysql 备份到 zip 存档(以防万一)
    7. 从生产服务器(在我的情况下为 mt Plesk 环境)复制包含在 mysql 文件夹存档中的所有文件夹和文件,除非不要覆盖:

      -/应用程序/MAMP/db/mysql/mysql/

      -/应用程序/MAMP/db/mysql/mysql_upgrade_info

      -/应用程序/MAMP/db/mysql/performance_schema

    8. 瞧,您现在应该可以从 phpMyAdmin 访问数据库了,真是一种解脱!

    但是我们还没有完成,您现在需要执行 mysqldump 以将这些文件恢复到您的生产环境,并且 phpmyadmin 界面对于大型数据库会超时。请按照此处的步骤操作:

    http://nickhardeman.com/308/export-import-large-database-using-mamp-with-terminal/

    复制如下以供参考。请注意,在默认 MAMP 安装中,密码为“root”。

    如何使用终端为 MAMP 运行 mysqldump

    从 MAMP[1] 导出数据库

    第一步: 打开一个新的终端窗口

    第二步: 通过在终端 cd /applications/MAMP/library/bin 中输入以下行来导航到 MAMP 安装 按回车键

    第三步: 编写转储命令 ./mysqldump -u [USERNAME] -p [DATA_BASENAME] > [PATH_TO_FILE] 按回车键

    例子:

    ./mysqldump -u root -p wp_database > /Applications/MAMP/htdocs/symposium10_wp/wp_db_onezero.sql
    

    快速提示:要快速导航到文件夹,您可以将文件夹拖到终端窗口中,它将写入文件夹的位置。有人向我展示了这一点,这是美好的一天。

    第四步: 这行文字应该在你按回车键后出现 输入密码:猜猜看,输入你的密码,记住字母不会出现,但它们在那里 按回车键

    第五步: 检查你存储文件的位置,如果在那里,成功 现在你可以导入数据库,这将在下面概述。

    现在您已经导出了 mysql 数据库,您可以在生产环境中导入它。

    • 22
  3. Ecd
    2016-01-31T09:02:35+08:002016-01-31T09:02:35+08:00

    我已经使用 MySQL Utilites 和 MariaDB 10 恢复了我的 MySQL 5.5 *.ibd 和 *.frm 文件。

    1) 生成创建 SQL。
    您可以从 frm 文件中获取您的创建 sql。您必须使用:https ://downloads.mysql.com/archives/utilities/shell

    > mysqlfrm --server=root:pass@localhost:3306 c:\MY\t1.frm --port=3310

    您可能有其他方式你创建 sql 的。

    2) 创建您
    的表 在数据库上创建您的表。

    3) alter table xxx discard tablespace
    丢弃你想要替换*.ibd 文件的表。

    4) 将您的 *.ibd 文件(MySQL 或 MariaDB)复制到 MariaDB 的数据路径
    首先我尝试使用 MySQL 5.5 和 5.6 进行恢复,但数据库崩溃并立即停止关于表空间 id 损坏的错误。( ERROR 1030 (HY000): Got error -1 from storage engine )
    在我使用 MariaDB 10.1.8 之后,我已经成功恢复了我的数据。

    5) alter table xxx import tablespace
    当你运行这个语句时,MariaDB 会警告文件但它并不比恢复你的数据重要:) 数据库仍然继续,你可以看到你的数据。

    我希望这些信息对您有所帮助。

    • 21
  4. Tue
    2014-08-25T11:38:43+08:002014-08-25T11:38:43+08:00

    我只有将文件作为备份时遇到了完全相同的问题。

    我为解决它所做的是将数据库文件复制到/var/lib/mysql/yourdb 和放在/var/lib/mysql 中的ibdata1。

    然后我能够验证我是否可以访问表 mysql -u root -p dbname并查询以前损坏的一些表。

    之后我使用mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql 转储了数据库

    • 4
  5. Peter Wakeman
    2016-10-05T21:28:06+08:002016-10-05T21:28:06+08:00

    如果您使用的是 MAMP,并且在复制文件后无法启动 MySQL,我将其innodb_force_recovery = 2放入my.ini其中,然后我能够启动 mysql 并将我的数据库导出。

    • 3
  6. Jahirul Islam Mamun
    2020-06-01T07:53:55+08:002020-06-01T07:53:55+08:00

    Note: Try to install MySQL same version, if your crashed MySQL version was 5.6, please install MySQL 5.6. (brew install mysql@5.6). If following process fails try the same things with MariaDB. (In my case, by MariaDB, I get back all database)

    Process 1

    • Before start, must stop MySQL or MariaDB. Use this command (brew services stop mysql), For MariaDB brew services stop mariadb
    • ibdata1 file copy from backup mysql folder and paste and replace it inside new installed mysql folder's ibdata1 (usr/local/var/mysql) or mamp/xampp mysql folder location.
    • ib_logfile0 and ib_logfile1 no need to replace first. If above process don't work, copy this file from backup mysql folder and replace. (In my case, i did not replace)
    • Now copy 1 database for test (for example : testdb) from backup mysql folder and paste it inside new installed mysql folder. In testdb must have .frm and .ibd file.
    • Restart mysql/mariadb (brew services start mysql) or (brew services start mariadb)
    • Login MySQL -> mysql -u root -p
    • If everything follow, mysql login successfully.
    • Now check database, SHOW DATABASES;
    • USE testdb;
    • SHOW TABLES;
    • SELECT * FROM testdbtable;
    • If there don't show any massage, then its successfully restore.

    Process 2

    • If process1 not work, you have to install mysqlfrm (https://downloads.mysql.com/archives/utilities/)
    • To find database table schema >> mysqlfrm --diagnostic /BACKUP/mysql/testdb > /RESTORE/testdb.sql
    • Open testdb.sql in notepad/sublime you will see all database table schema but not data.
    • Add ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; end of every CREATE TABLE....ENGINE=InnoDB otherwise table may not created.
    • Create new database "newtestdb" and run the command >> mysqldump -uroot -p --databases newtestdb > testdb.sql
    • It should create all database tables, Now if you check inside "newtestdb" you will see .frm and .ibd file.
    • Now again login mysql >> mysql -u root -p
    • SHOW DATABASES; USE newtestdb;
    • Run this command one by one for all table, ALTER TABLE tablename DISCARD TABLESPACE; if you check newtestbd .ibd file will disapear.
    • Now Copy only .ibd file from backup database testdb folder and paste it in newtestdb.
    • Now run ALTER TABLE tablename IMPORT TABLESPACE;
    • Hope now you will get back your data.
    • 2
  7. T.Todua
    2017-01-07T09:47:04+08:002017-01-07T09:47:04+08:00

    (首先,你需要在你的操作系统中安装一个名为“mysql-utilities”的小包,mysqlfrm --port=3333 your_table.frm > result.sql --basedir=/path/to/your/installation-folder/mysql用于恢复建表sql),之后,我收集了类似主题的帖子,没有提到在本主题中:

    解决方案0:https ://dba.stackexchange.com/a/57157/44247

    解决方案1:https ://dba.stackexchange.com/a/59978

    解决方案2:https ://dba.stackexchange.com/a/71785 (+其他帖子)

    解决方案 3:表恢复工具包:https ://twindb.com/how-to-recover-innodb-dictionary/

    解决方案 4:从 ibd 文件中恢复没有 ibdata1 的数据文件夹中的 MySQL 数据库

    解决方案5:https ://dba.stackexchange.com/a/159001

    解决方案6:https ://dba.stackexchange.com/a/144573

    • 1
  8. Samuraj
    2019-03-07T23:13:38+08:002019-03-07T23:13:38+08:00

    如果您能够将 *.ibd 文件恢复到原始 MySQL 服务器,请不要忘记也恢复文件访问权限。就我而言(CentOS7 上的 MySQL8),我将文件恢复到 /var/lib/mysql/db/tablename.ibd 并运行:

    chown mysql tablename.ibd
    chgrp mysql tablename.ibd
    chmod 0640 tablename.ibd
    

    在修复访问权限之前,访问表导致错误“2006 MySQL 服务器已消失”。修复访问权限后,该表工作(即使没有重新启动 mysqld 服务)。

    • 1
  9. user2804064
    2019-05-21T16:59:57+08:002019-05-21T16:59:57+08:00

    我只想为 macos El Capitan 用户再添加一件事。此版本不支持 MySQL 实用程序,因此 mysqlfrm 命令没有帮助。我所做的是使用 dbsake 恢复我的表结构,如以下链接所示:https ://www.percona.com/blog/2015/12/16/recovering-table-structure-from-frm-files-using-dbsake/

    您需要做的就是安装 dbsake:

    # curl -s http://get.dbsake.net > dbsake
    # chmod u+x dbsake
    

    然后使用 frmdump 命令并提供 .frm 文件的路径:

    # ./dbsake frmdump /var/lib/mysql/sakila/staff.frm
    

    你会得到 create 语句。完成此操作后,我只需按照@Ecd 已经提到的步骤 2 到 5 进行操作。希望它可以帮助某人。

    • 0
  10. Yersskit
    2019-08-06T18:27:11+08:002019-08-06T18:27:11+08:00

    我非常感谢 Ecd。什么对我有用:

    1.-几个月前我有一个基础备份,这帮助我在 Windows 10 的 xampp 中解除了这个备份并创建了具有结构的表 (配置:Windows 10,xampp-windows-x64-7.1.30- 5-VC14) mysql my.ini 配置文件末尾

    NOTE: Some tables did not have ROW_FORMAT = COMPACT, so I went to operations on each 
        table and changed it manually.
        (If I did not do that, an error appeared and I did not let the import).
    
    NOTE2: I had the backup of months ago but it should also work by first recovering 
        the structure of the .frm files in case of not having a backup at hand.
        (You can try this link:
        https://www.percona.com/blog/2014/01/02/recover-table-structure-frm-files-mysql- 
        utilities/)
    

    2.-启动旧数据库后,我继续对要恢复的数据库中的每个表执行 alter table xxx discard tablespace,然后在 C:/xampp/mysql/data/system 中的 data 文件夹的 .ibd 文件已被删除(在这种情况下是这条路径)

    3.-我继续将 .ibd 文件从要恢复的数据库复制到旧数据库的 xampp 文件夹中

    4.-复制完文件,运行:alter table xxx import tablespace 对于数据库中的每个表,都会出现警告但我们将忽略它,数据将加载到表中,稍后可以导出。

    5.-将整个数据库导出到一个 sql 文件中,然后继续在生产中构建它并成功!

    # Example MySQL config file for small systems.
    #
    # This is for a system with little memory (<= 64M) where MySQL is only used
    # from time to time and it's important that the mysqld daemon
    # doesn't use much resources.
    #
    # You can copy this file to
    # C:/xampp/mysql/bin/my.cnf to set global options,
    # mysql-data-dir/my.cnf to set server-specific options (in this
    # installation this directory is C:/xampp/mysql/data) or
    # ~/.my.cnf to set user-specific options.
    #
    # In this file, you can use all long options that a program supports.
    # If you want to know which options a program supports, run the program
    # with the "--help" option.
    
    # The following options will be passed to all MySQL clients
    [client] 
    # password       = your_password 
    port            = 3306 
    socket          = "C:/xampp/mysql/mysql.sock"
    
    
    # Here follows entries for some specific programs 
    
    # The MySQL server
    [mysqld]
    port= 3306
    socket = "C:/xampp/mysql/mysql.sock"
    basedir = "C:/xampp/mysql" 
    tmpdir = "C:/xampp/tmp" 
    datadir = "C:/xampp/mysql/data"
    pid_file = "mysql.pid"
    # enable-named-pipe
    key_buffer = 160M
    max_allowed_packet = 300M
    sort_buffer_size = 1204K
    net_buffer_length = 80K
    read_buffer_size = 512K
    read_rnd_buffer_size = 1024K
    myisam_sort_buffer_size = 8M
    log_error = "mysql_error.log"
    
    # Change here for bind listening
    # bind-address="127.0.0.1" 
    # bind-address = ::1          # for ipv6
    
    # Where do all the plugins live
    plugin_dir = "C:/xampp/mysql/lib/plugin/" 
    
    # Don't listen on a TCP/IP port at all. This can be a security enhancement,
    # if all processes that need to connect to mysqld run on the same host.
    # All interaction with mysqld must be made via Unix sockets or named pipes.
    # Note that using this option without enabling named pipes on Windows
    # (via the "enable-named-pipe" option) will render mysqld useless!
    # 
    # commented in by lampp security
    #skip-networking
    #skip-federated
    
    # Replication Master Server (default)
    # binary logging is required for replication
    # log-bin deactivated by default since XAMPP 1.4.11
    #log-bin=mysql-bin
    
    # required unique id between 1 and 2^32 - 1
    # defaults to 1 if master-host is not set
    # but will not function as a master if omitted
    server-id   = 1
    
    # Replication Slave (comment out master section to use this)
    #
    # To configure this host as a replication slave, you can choose between
    # two methods :
    #
    # 1) Use the CHANGE MASTER TO command (fully described in our manual) -
    #    the syntax is:
    #
    #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
    #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
    #
    #    where you replace <host>, <user>, <password> by quoted strings and
    #    <port> by the master's port number (3306 by default).
    #
    #    Example:
    #
    #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
    #    MASTER_USER='joe', MASTER_PASSWORD='secret';
    #
    # OR
    #
    # 2) Set the variables below. However, in case you choose this method, then
    #    start replication for the first time (even unsuccessfully, for example
    #    if you mistyped the password in master-password and the slave fails to
    #    connect), the slave will create a master.info file, and any later
    #    change in this file to the variables' values below will be ignored and
    #    overridden by the content of the master.info file, unless you shutdown
    #    the slave server, delete master.info and restart the slaver server.
    #    For that reason, you may want to leave the lines below untouched
    #    (commented) and instead use CHANGE MASTER TO (see above)
    #
    # required unique id between 2 and 2^32 - 1
    # (and different from the master)
    # defaults to 2 if master-host is set
    # but will not function as a slave if omitted
    #server-id       = 2
    #
    # The replication master for this slave - required
    #master-host     =   <hostname>
    #
    # The username the slave will use for authentication when connecting
    # to the master - required
    #master-user     =   <username>
    #
    # The password the slave will authenticate with when connecting to
    # the master - required
    #master-password =   <password>
    #
    # The port the master is listening on.
    # optional - defaults to 3306
    #master-port     =  <port>
    #
    # binary logging - not required for slaves, but recommended
    #log-bin=mysql-bin
    
    
    # Point the following paths to different dedicated disks
    #tmpdir = "C:/xampp/tmp"
    #log-update = /path-to-dedicated-directory/hostname
    
    # Uncomment the following if you are using BDB tables
    #bdb_cache_size = 40M
    #bdb_max_lock = 10000
    
    # Comment the following if you are using InnoDB tables
    #skip-innodb
    innodb_data_home_dir = "C:/xampp/mysql/data"
    innodb_data_file_path = ibdata1:10M:autoextend
    innodb_log_group_home_dir = "C:/xampp/mysql/data"
    #innodb_log_arch_dir = "C:/xampp/mysql/data"
    ## You can set .._buffer_pool_size up to 50 - 80 %
    ## of RAM but beware of setting memory usage too high
    innodb_buffer_pool_size = 16M
    ## Set .._log_file_size to 25 % of buffer pool size
    innodb_log_file_size = 50M
    innodb_log_buffer_size = 8M
    innodb_flush_log_at_trx_commit = 1
    innodb_lock_wait_timeout = 600
    
    ## UTF 8 Settings
    #init-connect=\'SET NAMES utf8\'
    #collation_server=utf8_unicode_ci
    #character_set_server=utf8
    #skip-character-set-client-handshake
    #character_sets-dir="C:/xampp/mysql/share/charsets"
    sql_mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION
    log_bin_trust_function_creators = 1
    
    [mysqldump]
    quick
    max_allowed_packet = 160M
    
    [mysql]
    no-auto-rehash
    # Remove the next comment character if you are not familiar with SQL
    #safe-updates
    
    [isamchk]
    key_buffer = 20M
    sort_buffer_size = 20M
    read_buffer = 2M
    write_buffer = 2M
    
    [myisamchk]
    key_buffer = 20M
    sort_buffer_size = 20M
    read_buffer = 2M
    write_buffer = 2M
    
    [mysqlhotcopy]
    interactive-timeout
    
    

    I hope it helps someone who has this situation, regards.

    English provided by Google

    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何查看 Oracle 中的数据库列表?

    • 8 个回答
  • Marko Smith

    mysql innodb_buffer_pool_size 应该有多大?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    从 .frm 和 .ibd 文件恢复表?

    • 10 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve