1. 程式人生 > >6種innodb數據字典恢復方法

6種innodb數據字典恢復方法

files cto 產生 nod ucc row temporary b- ima

6種innodb數據字典恢復方法

https://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html

問題一
誤刪frm文件或者把frm文件改名為其他名字,比如123.frm改為123.txt的解決辦法
或者把frm文件刪除了

CREATE TABLE Failure Due to Orphan Table

A symptom of an out-of-sync data dictionary is that a CREATE TABLE statement fails. If this occurs, look in the server‘s error log. If the log says that the table already exists inside the InnoDB internal data dictionary, you have an orphan table inside the InnoDB tablespace files that has no corresponding .frm file. The error message looks like this:


InnoDB: Error: table test/parent already exists in InnoDB internal
InnoDB: data dictionary. Have you deleted the .frm file
InnoDB: and not used DROP TABLE? Have you used DROP DATABASE
InnoDB: for InnoDB tables in MySQL version <= 3.23.43?
InnoDB: See the Restrictions section of the InnoDB manual.
InnoDB: You can drop the orphaned table inside InnoDB by
InnoDB: creating an InnoDB table with the same name in another
InnoDB: database and moving the .frm file to the current database.
InnoDB: Then MySQL thinks the table exists, and DROP TABLE will
InnoDB: succeed.

解決方法
如果只是改名把frm文件重新改回123.frm即可
如果是誤刪frm文件,那麽在另一個庫新建一個表結構和表名一樣的表,然後把frm文件拷貝到當前數據庫下



問題二
誤刪了ibd文件
ERROR 1016: Can‘t open file: ‘child2.ibd‘. (errno: 1)

解決方法
直接把frm文件刪除即可,整個表就會刪除


問題三
Orphan Intermediate Tables
刪除孤兒中間表
ALTER TABLE 操作 (ALGORITHM=INPLACE),一般會產生孤兒中間表


問題四
Orphan Temporary Tables
刪除孤兒臨時表
在做table-copying表數據拷貝的時候 ALTER TABLE 操作 (ALGORITHM=COPY),mysql突然掛了
會產生孤兒臨時表






問題五
表空間不存在
Tablespace Does Not Exist
ibd文件和frm文件被誤刪
但是ibdata1裏面的InnoDB data dictionary 數據字典依然保留著表空間ID tablespace id N

解決方法
在其他庫裏創建一個相同表結構的表,然後把frm文件拷貝到當前庫,然後drop table把表刪除
innodb會把ibd文件丟失的信息打印到errorlog裏面

問題六
還原孤兒 File-Per-Table ibd文件
Restoring Orphan File-Per-Table ibd Files
沒有frm文件只有ibd文件

解決方法
利用表空間傳輸,根據表結構新建同樣的表,然後導入ibd文件

mysql> CREATE DATABASE sakila;

mysql> USE sakila;

mysql> CREATE TABLE actor (
    ->    actor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->    first_name VARCHAR(45) NOT NULL,
    ->    last_name VARCHAR(45) NOT NULL,
    ->    last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ->    PRIMARY KEY  (actor_id),
    ->    KEY idx_actor_last_name (last_name)
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    
mysql> ALTER TABLE sakila.actor DISCARD TABLESPACE;

shell> cp /backup_directory/actor.ibd path/to/mysql-5.7/data/sakila/


mysql> ALTER TABLE sakila.actor IMPORT TABLESPACE; SHOW WARNINGS;    
Query OK, 0 rows affected, 1 warning (0.15 sec)

Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory)
Error opening ./sakila/actor.cfg, will attempt to import
without schema verification


mysql> SELECT COUNT(*) FROM sakila.actor;
+----------+
| count(*) |
+----------+
|      200 |
+----------+

6種innodb數據字典恢復方法