數據文件物理誤刪除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 ;