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丢失后又恢复回来了