MySQL is one of the most popular open-source relational database management systems in the world. It is widely used for web applications, e-commerce, data warehousing, and more. However, MySQL is not immune to data corruption or disasters that can cause the server to fail or lose data. In such cases, it is crucial to have a backup and recovery strategy that can restore the database to a consistent state.
One of the challenges of recovering a MySQL database is to retrieve the table structure or definition, also known as Data Definition Language (DDL). In previous versions of MySQL, the table DDL was stored in external files with the .frm extension, along with the data files with the .ibd or .myd and .myi extensions. These files could be copied or moved to another server or directory, and MySQL could read them and recognize the tables. However, this approach had some drawbacks, such as:
In MySQL 8, the .frm files have been dropped and replaced by a new data dictionary that stores all the metadata information inside InnoDB tablespaces. The data dictionary is transactional, platform-independent, resilient, and accessible by SQL queries or commands. This change brings many benefits, such as:
However, this change also implies that the old methods of recovering table DDL from .frm files are no longer valid or applicable. In this article, we will explore how to recover table DDL in MySQL 8 without .frm files, using different tools and techniques.
One of the new features introduced in MySQL 8 is the Spatial Data Infrastructures (SDI) file. The SDI file is a JSON file that contains a copy of the table DDL and some additional information, such as tablespace name, schema name, table name, etc. The SDI file is stored inside the InnoDB tablespace that contains the table data. The SDI file is updated or modified by DDL operations on tables that reside in that tablespace.
The SDI file can be used as a source of table DDL recovery in MySQL 8. However, there are some limitations and caveats to consider:
To recover table DDL from SDI files, we need to use a tool called mysqlpump, which is a logical backup utility that can dump database objects and data into a SQL script. The mysqlpump tool has an option called --include-sdi that allows us to include the SDI information in the output. For example, we can run the following command to dump the table DDL from a schema named windmills:
mysqlpump --user=root --password --include-sdi --skip-dump-rows --exclude-databases=mysql windmills > windmills_sdi.sql
This command will create a file called windmills_sdi.sql that contains the table DDL along with the SDI information for each table in the windmills schema. The output will look something like this:
-- Dump created by MySQL pump utility
We can see that each table has a comment with a JSON object that contains the SDI information. We can ignore this comment and use only the CREATE TABLE statements to restore the table DDL on another server or directory.
If we do not have access to the SDI files or we want to use another method to recover table DDL in MySQL 8 without .frm files, we can use some tools that can read and parse the binary format of InnoDB tablespaces and extract the metadata information from them. Two of these tools are DBSake and mysqlfrm.
DBSake is a collection of command-line tools for MySQL administration and development. One of its features is frmdump, which can dump table DDL from .frm files or InnoDB tablespaces. To use DBSake frmdump on an InnoDB tablespace file (.ibd), we need to specify the --innodb option and provide some additional information about the server version and page size. For example:
dbsake frmdump --innodb --server-version=8.0 wmillAUTOINC1.ibd
This command will output something like this:
-- Table structure for table `wmillAUTOINC1`
MySQL Utilities is a package of tools that provide various functionalities for MySQL administration and development. One of these tools is mysqlfrm, which can read and parse the binary format of .frm files or InnoDB tablespaces and extract the table structure information from them. The mysqlfrm tool can also spawn a local server instance and execute the table DDL on it, so that we can inspect the table structure using SQL commands.
To recover table DDL from mysqlfrm, we need to have access to the .frm files or the InnoDB tablespace files that contain the table structure information. We also need to have the mysqlfrm tool installed and configured on our system. For example, if we have a .frm file called wmillAUTOINC1.frm in the windmills schema, we can run the following command to recover its table DDL:
mysqlfrm --server=root:rootpassword@localhost windmills/wmillAUTOINC1.frm --port=3307
This command will spawn a local server instance on port 3307 and execute the CREATE TABLE statement for the wmillAUTOINC1 table on it. The output will look something like this:
# Source on localhost: ... connected.
We can see that this command generates a CREATE TABLE statement that matches the original table structure. However, this command does not include any indexes or constraints that may exist on the table. To include them, we need to use the --diagnostic option and provide an InnoDB tablespace file (.ibd) instead of a .frm file.
As we have seen, recovering table DDL in MySQL 8 without .frm files is possible, but not always easy or straightforward. Depending on the situation, we may need to use different tools and techniques, and deal with various challenges and limitations. Therefore, it is advisable to follow some best practices that can help us avoid or minimize the need for table DDL recovery in MySQL 8, such as:
By following these best practices, you can ensure that your MySQL database is safe and secure, and that you can recover your table DDL in MySQL 8 without .frm files if needed.
In this article, we have explored how to recover table DDL in MySQL 8 without .frm files, using different tools and techniques. We have seen that MySQL 8 has introduced a new data dictionary that stores all the metadata information inside InnoDB tablespaces, replacing the old .frm files. This change has many advantages, but also some challenges for backup and recovery scenarios. We have learned how to use SDI files, DBSake or mysqlfrm tools, backup files, and information schema views to extract and restore table structure information in MySQL 8. We have also discussed some best practices that can help us avoid or minimize the need for table DDL recovery in MySQL 8.
a8ba361960