通过替换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的方式修改表结构。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」