Mysql关于InnoDB表误删除掉.frm文件无法删除也无法创建的情况

无意间进入了/data/mysql/$(database_name)/ 下,误删了$(table_name).frm,之后drop 或者 create都报错,直接创建也会提示此表已经存在,是删也删不掉,建也建不了,网上查了下说是因为表在 innodb 内部数据字典中已存在,没有相对应的 .frm 文件,在 innodb 的数据文件中就形成了一个孤表(orphaned table)。

ERROR 如下:

1
2
3
[Err] 1005 - Can't create table
or
Table already exist

经查询在下面的文章中找到了答案:

1
2
3
4
5
6
table test/parent already exists in innodb internal data dictionary.
have you deleted the .frm file and not used drop table?
have you used drop database for innodb tables in mysql version <= 3.23.43?
see the restrictions section of the innodb manual.
you can drop the orphaned table inside innodb creating an innodb table with the same name in another database and moving the .frm file to the current database.
then mysql thinks the table exists, and drop table will innodb: succeed.

意思就是说:你可以通过在另外一个数据库中创建一个相同名字的innodb表,并把.frm拷贝到这个数据库目录下,数据库就会认为这个表存在,再执行drop table就可以删除掉了。

操作如下:

1
2
3
4
## 在其他数据库创建一个相同命明的表名,内部结构随意,我只创建了一个 id 类型
## 复制上一步生成的.frm 到你之前误删除的的目录下
## chown mysql:mysql 你复制过来的******.frm
## 以上三部修复完成

写的很清楚了,还不会。。。