Actual combat: from the Mysql database frm file, extract the table structure to create SQL statements

demand

In some special scenarios, for example, your mysql database cannot be started, you need to copy the ibd file of the table to another database, restore the business database, and the premise of restoring business data is that you need to create it in another database The table structure is exactly the same. At this time, you need to extract the table structure from the frm file of the Mysql database to create the sql statement.

To read the table structure in the frm file, you need to install the mysql tool set rpm package first. I installed the mysql-utilities-1.6.5-1.el7.noarch.rpm version. This package can be obtained from the mysql official Website download, he relies on the mysql-connector-python package, so it also needs to be downloaded from the official website.

The installation process is as follows


[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

Some friends may have questions. Since your mysql database has been unable to start, can mysqlfrm extract the table structure to create SQL statements? Let’s test it in action.

Check if there is a mysqld service process


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

Use mysqlfrm to extract the table structure

[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.

As you can see, the table structure of test1 has been extracted to create the sql statement, let's see if the extracted statement is correct.


[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 test1\G;
*************************** 1. row ***************************
       Table: test1
Create 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_ci
1 row in set (0.03 sec)

ERROR:
No query specified

Compare the original table structure and the extracted table structure. Are there any differences? This is because mysqlfrm cannot know what character set to use when extracting the table structure SQL statement without connecting to the database. In this test table, the character set used is utf8mb4, and one character occupies a length of 4 bytes, so here, the length of char and varchar needs to be divided by 4 to be the correct table structure sql statement.

Guess you like

Origin blog.51cto.com/15061930/2642092