前提

  1. 有表结构备份
  2. 开启独立表空间show global variables like 'innodb_file_per_table'

    环境模拟

    1. 构建测试数据库及表

    ```sql create table test;

use test;

CREATE TABLE table_test ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

  1. <a name="GY51C"></a>
  2. #### 2. 故障模拟
  3. 1. 手动删除 test 库下的 table_test 表 frm文件,删除后执行 drop 后提示 "Unknown table 'test.table_test'",执行 select 提示 "Table 'test.table_test' doesn't exist"
  4. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/12566248/1647769019338-822a67aa-85ad-4b40-b507-5d5cda9012d7.png#clientId=u1eefff41-ef9c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=245&id=ua1d0249d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=245&originWidth=595&originalType=binary&ratio=1&rotation=0&showTitle=false&size=26424&status=done&style=none&taskId=u3e2d7d48-aaf1-44c4-a426-60ada2407d9&title=&width=595)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/12566248/1647769186776-a1ca3b2f-5fa0-4f4c-a243-6e7f0edeb743.png#clientId=u1eefff41-ef9c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=144&id=u8d01fdf6&margin=%5Bobject%20Object%5D&name=image.png&originHeight=132&originWidth=546&originalType=binary&ratio=1&rotation=0&showTitle=false&size=14678&status=done&style=none&taskId=u1ac4fd3c-6497-4842-b10f-65cd9a044cf&title=&width=597)
  5. <a name="qVSP2"></a>
  6. ### 恢复
  7. 1. 备份 ibd 文件 `cp table_test.ibd /your_dir/table_test.ibd`
  8. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/12566248/1647769843272-cfd58b14-e0eb-4ada-b409-29886bdb9db3.png#clientId=u1eefff41-ef9c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=167&id=ud037e5de&margin=%5Bobject%20Object%5D&name=image.png&originHeight=167&originWidth=541&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16446&status=done&style=none&taskId=uf32ed870-40d9-40bd-9242-cd2564966d4&title=&width=541)
  9. 2. 随便找一张其他表的 frm 文件命名为故障表的 frm 文件,并赋予 mysql 用户组
  10. ```shell
  11. cp test2.frm table_test.frm
  12. chown mysql.mysql table_test.frm

image.png

  1. 删除故障表

删除之后,故障表 frm、ibd文件都会被删除
image.png

  1. 使用故障表结构创建新表

此时生成新的 frm 和 ibd 文件
image.png
image.png

  1. 卸载新表表空间 alter table table_test discard tablespace;

此时新表 ibd 表空间被删除
image.png
image.png

  1. 复制 table_test.ibd.bk 到 table_test.ibd,赋予权限,并导入表空间 ```shell shell> cp /yourdir/table_test.ibd.bk table_test.ibd

shell> chown mysql.mysql table_test.ibd

导入表空间

mysql> alter table table_test discard tablespace; ``` image.png
image.png
image.png