通过替换frm文件方式修改表结构

版本:5.6.16

在自己的虚拟环境中,测试创建一个表,表结构如下:
mysql> drop table yoon_temp;
Query OK, 0 rows affected (0.09 sec)


mysql> show create table yoon\G
*************************** 1. row ***************************
       Table: yoon
Create Table: CREATE TABLE `yoon` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


再创建一个相同的表结构,name varchar(30) 为30,并通过替换.frm来实现yoon表的字段修改:
mysql> show create table yoon_temp\G
*************************** 1. row ***************************
       Table: yoon_temp
Create Table: CREATE TABLE `yoon_temp` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


不小心直接删除yoon_temp.frm文件,无法替换,在数据库上删除yoon_temp提示表不存在:
[root@hank-yoon yoon]# rm -rf yoon_temp.frm

mysql> show tables;
+----------------+
| Tables_in_yoon |
+----------------+
| yoon           |
+----------------+
1 row in set (0.00 sec)


删除表:
mysql> drop table yoon_temp;
ERROR 1051 (42S02): Unknown table 'yoon.yoon_temp'

重新创建表:
mysql> create table yoon_temp (id int,name varchar(30));
ERROR 1813 (HY000): Tablespace for table '`yoon`.`yoon_temp`' exists. Please DISCARD the tablespace before IMPORT.

mysql> alter table yoon_temp DISCARD tablespace;
ERROR 1146 (42S02): Table 'yoon.yoon_temp' doesn't exist


在目录下通过yoon.frm拷贝创建yoon_temp.frm
[root@hank-yoon yoon]# cp  yoon.frm  yoon_temp.frm

[root@hank-yoon yoon]# chown mysql.mysql yoon_temp.frm

查看表:
mysql> show tables;
+----------------+
| Tables_in_yoon |
+----------------+
| yoon           |
| yoon_temp      |
+----------------+
2 rows in set (0.00 sec)


mysql> show create table yoon_temp\G
*************************** 1. row ***************************
       Table: yoon_temp
Create Table: CREATE TABLE `yoon_temp` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


mysql> drop table yoon_temp;
Query OK, 0 rows affected (0.03 sec)

重新创建再测试:
mysql> create table yoon_temp (id int,name varchar(30));
Query OK, 0 rows affected (0.02 sec)

当表结构文件不小心删除时,可通过其他表结构来创建进行修复删除,表结构要相同,字段varcahr(xxx) xxx大小不同无所谓,也间接实现了通过替换frm的方式修改表结构。

posted @ 2016-01-29 17:40  __Yoon  阅读(233)  评论(0编辑  收藏  举报