数据文件物理误删除ibd文件处理方法
在MySQL数据库中表的ibd即表空间文件被误删除影响比较大,虽然删除后可以正常读写表数据,但是在重启后表的任何访问动作都将报错:
## 删除表空间后,表仍然可以读写
# rm scott_tab.ibd -f
mysql> insert into scott_tab values ( 'test');
Query OK, 1 row affected (0.00 sec)
mysql> select * from scott_tab where name='test';
+------+
| name |
+------+
| test |
+------+
1 row in set (0.00 sec)
## 重启后,任何访问表操作都将报错
mysql> select * from scott_tab where name='test';
ERROR 1812 (HY000): Tablespace is missing for table `scott`.`scott_tab`.
mysql> insert into scott_tab values ( 'test2');
ERROR 1812 (HY000): Tablespace is missing for table `scott`.`scott_tab`.
解决方法
对于.ibd文件误删除后最为快速的修复方式是通过全备份、表空间导入、回放binlog的方式进行组合还原,具体操作步骤如下
## 通过全备份文件(本例是逻辑全备)还原到临时或测试实例
mysql -P3307 -uroot -proot < all_db_with_data.sql
## 回放binlog(注意从全备文件找到起始位置、并只过滤 scott 库节省时间)
mysqlbinlog -vvv mysql-bin.000007 --start-position=604 --database=scott |mysql -uroot -proot -P3307
## 故障实例丢弃表空间(稳妥操作,虽然表空间已被误删除)
mysql> alter table scott_tab discard tablespace;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> show warnings\G
*************************** 1. row ***************************
Level: Warning
Code: 1812
Message: InnoDB: Tablespace is missing for table scott/scott_tab.
*************************** 2. row ***************************
Level: Warning
Code: 1812
Message: InnoDB: Tablespace is missing for table scott/scott_tab.
2 rows in set (0.00 sec)
## 临时实例导出备份表的表空间
mysql> flush table scott_tab for export;
Query OK, 0 rows affected (0.00 sec)
## 拷贝临时实例的ibd表空间到故障实例指定数据目录下
cp /data/mysql_3307/test/scott_tab.ibd ./
## 不要忘记最后解锁临时实例对应表scott_tab
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
## 故障实例导入表空间
mysql> alter table scott_tab import tablespace;
Query OK, 0 rows affected, 1 warning (0.21 sec)
## 验证表是否读写正常
mysql> select * from scott_tab ;