MyISAM表的.frm文件丟失後的恢復方法

MyISAM表的.frm文件丟失後的恢復方法:

1、創建實驗用的MyISAM表t1,並插入數據:

mysql> create table t1(id int) engine=myisam;

Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1),(2),(3),(4),(5),(6),(7),(8);

Query OK, 8 rows affected (0.00 sec)

Records: 8  Duplicates: 0  Warnings: 0

2、刪除t1表的.frm文件

[root@localhost gusha]# cd /var/lib/mysql/gusha 

[root@localhost gusha]# ls

db.opt     t1.MYI    t1.frm  t1.MYD  

[root@localhost gusha]# rm -rf t1.frm 

此時在gusha庫裏已經查詢不到t1表了:

mysql> show tables;

Empty set (0.00 sec)

還能查詢t1表裏的內容是因爲有緩存,清下緩存:

mysql> select * from t1;

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    7 |

|    8 |

+------+

8 rows in set (0.00 sec)

mysql> flush tables;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from t1;

ERROR 1146 (42S02): Table 'gusha.t1' doesn't exist

3、進行恢復,把gusha庫對應的文件夾裏的t1.MYD和t1.MYI文件移動到其它文件夾:

[root@localhost gusha]# mv t1.MY* /var/lib/backup/

[root@localhost gusha]# ls

db.opt

在gusha庫裏重新創建一個t1表,表結構和原來的t1表一樣:

mysql> create table t1(id int) engine=myisam;

Query OK, 0 rows affected (0.00 sec)

把t1.MYD和t1.MYI文件移動會gusha庫對應的文件夾:

[root@localhost gusha]# mv /var/lib/backup/t1.MY* .

mv: overwrite `./t1.MYD'? y

mv: overwrite `./t1.MYI'? y

此時MySQL會自動修復t1表

mysql> select * from t1;

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

|    6 |

|    7 |

|    8 |

+------+

8 rows in set (0.00 sec)

如果沒有自動修復,則執行下面命令進行修復:

mysql> repair table t1;

+----------+--------+----------+----------+

| Table    | Op     | Msg_type | Msg_text |

+----------+--------+----------+----------+

| gusha.t1 | repair | status   | OK       |

+----------+--------+----------+----------+

1 row in set (0.00 sec)

到此MyISAM表t1.frm丟失後又恢復回來了

更多精彩MySQL內容 請關注我:
api.php?bg=ffffff&fg=000000&gc=000000&el

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章