db2查询表结构语句_实战:从Mysql数据库frm文件,提取表结构创建SQL语句

在某些特殊的场景下,例如你的mysql数据库无法启动,需要你将表的ibd文件拷贝到另一个数据库中,恢复业务数据库,恢复业务数据的前提,是你需要在另一个数据库中,创建好一模一样的表结构。这时你就需要从Mysql数据库的frm文件中,提取表结构创建sql语句。

5cac44a6009a4f8c5ef62921bdd955b0.png

安装需求

要想读取frm文件中的表结构,你需要先安装mysql的工具集rpm包,我安装的是mysql-utilities-1.6.5-1.el7.noarch.rpm这个版本,这个包可以从mysql官方网站下载,他依赖mysql-connector-python包,所以也需要从官网下载

安装过程如下所示

[root@mysql ~]# yum install mysql-connector-python-2.1.8-1.el7.x86_64.rpm[root@mysql ~]# yum install mysql-utilities-1.6.5-1.el7.noarch.rpm

有的朋友可能有疑问,既然你的mysql数据库已经无法启动了,那mysqlfrm能否提取到表结构创建sql语句呢,下面就来实战测试一下

检查是否有mysqld服务进程

[mysql@mysql ~]$ ps -ef|grep -i mysqldmysql     74835  70672  0 16:45 pts/1    00:00:00 grep --color=auto -i mysqld
fc3f89aac60cc3a3c935580c09693a6b.png

使用mysqlfrm提取表结构

[mysql@mysql testdb]$ mysqlfrm --diagnostic /data/mysql/data/3306/testdb/test1.frm# WARNING: Cannot generate character set or collation names without the --server option.# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.# Reading .frm file for /data/mysql/data/3306/testdb/test1.frm:# The .frm file is a TABLE.# CREATE TABLE Statement:CREATE TABLE `testdb`.`test1` (  `id` int(11) NOT NULL,   `name1` char(40) NOT NULL,   `name2` char(80) NOT NULL, PRIMARY KEY `PRIMARY` (`id`)) ENGINE=InnoDB;#...done.

可以看到,已经提取到test1的表结构创建sql语句了,那来看看,提取到的语句是否是正确的。

[root@localhost] 16:53:24 [testdb]>show tables;+------------------+| Tables_in_testdb |+------------------+| test1            |+------------------+1 row in set (0.00 sec)[root@localhost] 16:53:32 [testdb]>show create table test1G;*************************** 1. row ***************************       Table: test1Create Table: CREATE TABLE `test1` (  `id` int(11) NOT NULL,  `name1` char(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',  `name2` char(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci1 row in set (0.03 sec)ERROR: No query specified
2d802bdcbac587be94bc10878b8f2312.png

对比一下原始表结构和提取出来的表结构,是不是发现有不一样的地方,这是因为mysqlfrm在不连接数据库提取表结构sql语句时,是无法知道用什么字符集的。在这个测试表,用的字符集是utf8mb4,一个字符占用4个字节长度,所以在这里,还需要将char,varchar的长度除4,才是正确的表结构sql语句。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值