This section describes how to import tables using the Transportable Tablespaces feature, which permits importing tables, partitioned tables, or individual table partitions that reside in file-per-table tablespaces. There are many reasons why you might want to import tables:
To run reports on a non-production MySQL server instance to avoid placing extra load on a production server.
To copy data to a new replica server.
To restore a table from a backed-up tablespace file.
As a faster way of moving data than importing a dump file, which requires reinserting data and rebuilding indexes.
To move a data to a server with storage media that is better suited to your storage requirements. For example, you might move busy tables to an SSD device, or move large tables to a high-capacity HDD device.
The Transportable Tablespaces feature is described under the following topics in this section:
The
innodb_file_per_tablevariable must be enabled, which it is by default.The page size of the tablespace must match the page size of the destination MySQL server instance.
InnoDBpage size is defined by theinnodb_page_sizevariable, which is configured when initializing a MySQL server instance.If the table has a foreign key relationship,
foreign_key_checksmust be disabled before executingDISCARD TABLESPACE. Also, you should export all foreign key related tables at the same logical point in time, asALTER TABLE ... IMPORT TABLESPACEdoes not enforce foreign key constraints on imported data. To do so, stop updating the related tables, commit all transactions, acquire shared locks on the tables, and perform the export operations.When importing a table from another MySQL server instance, both MySQL server instances must have General Availability (GA) status and must be the same version. Otherwise, the table must be created on the same MySQL server instance into which it is being imported.
If the table was created in an external directory by specifying the
DATA DIRECTORYclause in theCREATE TABLEstatement, the table that you replace on the destination instance must be defined with the sameDATA DIRECTORYclause. A schema mismatch error is reported if the clauses do not match. To determine if the source table was defined with aDATA DIRECTORYclause, useSHOW CREATE TABLEto view the table definition. For information about using theDATA DIRECTORYclause, see Section 17.6.1.2, “Creating Tables Externally”.If a
ROW_FORMAToption is not defined explicitly in the table definition orROW_FORMAT=DEFAULTis used, theinnodb_default_row_formatsetting must be the same on the source and destination instances. Otherwise, a schema mismatch error is reported when you attempt the import operation. UseSHOW CREATE TABLEto check the table definition. UseSHOW VARIABLESto check theinnodb_default_row_formatsetting. For related information, see Defining the Row Format of a Table.
This example demonstrates how to import a regular non-partitioned table that resides in a file-per-table tablespace.
On the destination instance, create a table with the same definition as the table you intend to import. (You can obtain the table definition using
SHOW CREATE TABLEsyntax.) If the table definition does not match, a schema mismatch error is reported when you attempt the import operation.mysql> USE test; mysql> CREATE TABLE t1 (c1 INT) ENGINE=INNODB;On the destination instance, discard the tablespace of the table that you just created. (Before importing, you must discard the tablespace of the receiving table.)
mysql> ALTER TABLE t1 DISCARD TABLESPACE;On the source instance, run
FLUSH TABLES ... FOR EXPORTto quiesce the table you intend to import. When a table is quiesced, only read-only transactions are permitted on the table.mysql> USE test; mysql> FLUSH TABLES t1 FOR EXPORT;FLUSH TABLES ... FOR EXPORTensures that changes to the named table are flushed to disk so that a binary table copy can be made while the server is running. WhenFLUSH TABLES ... FOR EXPORTis run,InnoDBgenerates a.cfgmetadata file in the schema directory of the table. The.cfgfile contains metadata that is used for schema verification during the import operation.NoteThe connection executing
FLUSH TABLES ... FOR EXPORTmust remain open while the operation is running; otherwise, the.cfgfile is removed as locks are released upon connection closure.Copy the
.ibdfile and.cfgmetadata file from the source instance to the destination instance. For example:$> scp /path/to/datadir/test/t1.{ibd,cfg} destination-server:/path/to/datadir/testThe
.ibdfile and.cfgfile must be copied before releasing the shared locks, as described in the next step.NoteIf you are importing a table from an encrypted tablespace,
InnoDBgenerates a.cfpfile in addition to a.cfgmetadata file. The.cfpfile must be copied to the destination instance together with the.cfgfile. The.cfpfile contains a transfer key and an encrypted tablespace key. On import,InnoDBuses the transfer key to decrypt the tablespace key. For related information, see Section 17.13, “InnoDB Data-at-Rest Encryption”.On the source instance, use
UNLOCK TABLESto release the locks acquired by theFLUSH TABLES ... FOR EXPORTstatement:mysql> USE test; mysql> UNLOCK TABLES;The
UNLOCK TABLESoperation also removes the.cfgfile.On the destination instance, import the tablespace:
mysql> USE test; mysql> ALTER TABLE t1 IMPORT TABLESPACE;
This example demonstrates how to import a partitioned table, where each table partition resides in a file-per-table tablespace.
On the destination instance, create a partitioned table with the same definition as the partitioned table that you want to import. (You can obtain the table definition using
SHOW CREATE TABLEsyntax.) If the table definition does not match, a schema mismatch error is reported when you attempt the import operation.mysql> USE test; mysql> CREATE TABLE t1 (i int) ENGINE = InnoDB PARTITION BY KEY (i) PARTITIONS 3;In the
/directory, there is a tablespacedatadir/test.ibdfile for each of the three partitions.mysql> \! ls /path/to/datadir/test/ t1#p#p0.ibd t1#p#p1.ibd t1#p#p2.ibdOn the destination instance, discard the tablespace for the partitioned table. (Before the import operation, you must discard the tablespace of the receiving table.)
mysql> ALTER TABLE t1 DISCARD TABLESPACE;The three tablespace
.ibdfiles of the partitioned table are discarded from the/directory.datadir/testOn the source instance, run
FLUSH TABLES ... FOR EXPORTto quiesce the partitioned table that you intend to import. When a table is quiesced, only read-only transactions are permitted on the table.mysql> USE test; mysql> FLUSH TABLES t1 FOR EXPORT;FLUSH TABLES ... FOR EXPORTensures that changes to the named table are flushed to disk so that binary table copy can be made while the server is running. WhenFLUSH TABLES ... FOR EXPORTis run,InnoDBgenerates.cfgmetadata files in the schema directory of the table for each of the table's tablespace files.mysql> \! ls /path/to/datadir/test/ t1#p#p0.ibd t1#p#p1.ibd t1#p#p2.ibd t1#p#p0.cfg t1#p#p1.cfg t1#p#p2.cfgThe
.cfgfiles contain metadata that is used for schema verification when importing the tablespace.FLUSH TABLES ... FOR EXPORTcan only be run on the table, not on individual table partitions.Copy the
.ibdand.cfgfiles from the source instance schema directory to the destination instance schema directory. For example:$>scp /path/to/datadir/test/t1*.{ibd,cfg} destination-server:/path/to/datadir/testThe
.ibdand.cfgfiles must be copied before releasing the shared locks, as described in the next step.NoteIf you are importing a table from an encrypted tablespace,
InnoDBgenerates a.cfpfiles in addition to a.cfgmetadata files. The.cfpfiles must be copied to the destination instance together with the.cfgfiles. The.cfpfiles contain a transfer key and an encrypted tablespace key. On import,InnoDBuses the transfer key to decrypt the tablespace key. For related information, see Section 17.13, “InnoDB Data-at-Rest Encryption”.On the source instance, use
UNLOCK TABLESto release the locks acquired byFLUSH TABLES ... FOR EXPORT:mysql> USE test; mysql> UNLOCK TABLES;On the destination instance, import the tablespace of the partitioned table:
mysql> USE test; mysql> ALTER TABLE t1 IMPORT TABLESPACE;
This example demonstrates how to import individual table partitions, where each partition resides in a file-per-table tablespace file.
In the following example, two partitions (p2
and p3) of a four-partition table are
imported.
On the destination instance, create a partitioned table with the same definition as the partitioned table that you want to import partitions from. (You can obtain the table definition using
SHOW CREATE TABLEsyntax.) If the table definition does not match, a schema mismatch error is reported when you attempt the import operation.mysql> USE test; mysql> CREATE TABLE t1 (i int) ENGINE = InnoDB PARTITION BY KEY (i) PARTITIONS 4;In the
/directory, there is a tablespacedatadir/test.ibdfile for each of the four partitions.mysql> \! ls /path/to/datadir/test/ t1#p#p0.ibd t1#p#p1.ibd t1#p#p2.ibd t1#p#p3.ibdOn the destination instance, discard the partitions that you intend to import from the source instance. (Before importing partitions, you must discard the corresponding partitions from the receiving partitioned table.)
mysql> ALTER TABLE t1 DISCARD PARTITION p2, p3 TABLESPACE;The tablespace
.ibdfiles for the two discarded partitions are removed from the/directory on the destination instance, leaving the following files:datadir/testmysql> \! ls /path/to/datadir/test/ t1#p#p0.ibd t1#p#p1.ibdNoteWhen
ALTER TABLE ... DISCARD PARTITION ... TABLESPACEis run on subpartitioned tables, both partition and subpartition table names are permitted. When a partition name is specified, subpartitions of that partition are included in the operation.On the source instance, run
FLUSH TABLES ... FOR EXPORTto quiesce the partitioned table. When a table is quiesced, only read-only transactions are permitted on the table.mysql> USE test; mysql> FLUSH TABLES t1 FOR EXPORT;FLUSH TABLES ... FOR EXPORTensures that changes to the named table are flushed to disk so that binary table copy can be made while the instance is running. WhenFLUSH TABLES ... FOR EXPORTis run,InnoDBgenerates a.cfgmetadata file for each of the table's tablespace files in the schema directory of the table.mysql> \! ls /path/to/datadir/test/ t1#p#p0.ibd t1#p#p1.ibd t1#p#p2.ibd t1#p#p3.ibd t1#p#p0.cfg t1#p#p1.cfg t1#p#p2.cfg t1#p#p3.cfgThe
.cfgfiles contain metadata that used for schema verification during the import operation.FLUSH TABLES ... FOR EXPORTcan only be run on the table, not on individual table partitions.Copy the
.ibdand.cfgfiles for partitionp2and partitionp3from the source instance schema directory to the destination instance schema directory.$> scp t1#p#p2.ibd t1#p#p2.cfg t1#p#p3.ibd t1#p#p3.cfg destination-server:/path/to/datadir/testThe
.ibdand.cfgfiles must be copied before releasing the shared locks, as described in the next step.NoteIf you are importing partitions from an encrypted tablespace,
InnoDBgenerates a.cfpfiles in addition to a.cfgmetadata files. The.cfpfiles must be copied to the destination instance together with the.cfgfiles. The.cfpfiles contain a transfer key and an encrypted tablespace key. On import,InnoDBuses the transfer key to decrypt the tablespace key. For related information, see Section 17.13, “InnoDB Data-at-Rest Encryption”.On the source instance, use
UNLOCK TABLESto release the locks acquired byFLUSH TABLES ... FOR EXPORT:mysql> USE test; mysql> UNLOCK TABLES;On the destination instance, import table partitions
p2andp3:mysql> USE test; mysql> ALTER TABLE t1 IMPORT PARTITION p2, p3 TABLESPACE;NoteWhen
ALTER TABLE ... IMPORT PARTITION ... TABLESPACEis run on subpartitioned tables, both partition and subpartition table names are permitted. When a partition name is specified, subpartitions of that partition are included in the operation.
The Transportable Tablespaces feature is only supported for tables that reside in file-per-table tablespaces. It is not supported for the tables that reside in the system tablespace or general tablespaces. Tables in shared tablespaces cannot be quiesced.
FLUSH TABLES ... FOR EXPORTis not supported on tables with aFULLTEXTindex, as full-text search auxiliary tables cannot be flushed. After importing a table with aFULLTEXTindex, runOPTIMIZE TABLEto rebuild theFULLTEXTindexes. Alternatively, dropFULLTEXTindexes before the export operation and recreate the indexes after importing the table on the destination instance.Due to a
.cfgmetadata file limitation, schema mismatches are not reported for partition type or partition definition differences when importing a partitioned table. Column differences are reported.
With the exception of tables that contain instantly added or dropped columns,
ALTER TABLE ... IMPORT TABLESPACEdoes not require a.cfgmetadata file to import a table. However, metadata checks are not performed when importing without a.cfgfile, and a warning similar to the following is issued:Message: InnoDB: IO Read error: (2, No such file or directory) Error opening '.\ test\t.cfg', will attempt to import without schema verification 1 row in set (0.00 sec)Importing a table without a
.cfgmetadata file should only be considered if no schema mismatches are expected and the table does not contain any instantly added or dropped columns. The ability to import without a.cfgfile could be useful in crash recovery scenarios where metadata is not accessible.Attempting to import a table with columns that were added or dropped using
ALGORITHM=INSTANTwithout using a.cfgfile can result in undefined behavior.On Windows,
InnoDBstores database, tablespace, and table names internally in lowercase. To avoid import problems on case-sensitive operating systems such as Linux and Unix, create all databases, tablespaces, and tables using lowercase names. A convenient way to ensure that names are created in lowercase is to setlower_case_table_namesto 1 before initializing the server. (It is prohibited to start the server with alower_case_table_namessetting that is different from the setting used when the server was initialized.)[mysqld] lower_case_table_names=1When running
ALTER TABLE ... DISCARD PARTITION ... TABLESPACEandALTER TABLE ... IMPORT PARTITION ... TABLESPACEon subpartitioned tables, both partition and subpartition table names are permitted. When a partition name is specified, subpartitions of that partition are included in the operation.
The following information describes internals and messages written to the error log during a table import procedure.
When ALTER TABLE
... DISCARD TABLESPACE is run on the destination
instance:
The table is locked in X mode.
The tablespace is detached from the table.
When
FLUSH
TABLES ... FOR EXPORT is run on the source instance:
The table being flushed for export is locked in shared mode.
The purge coordinator thread is stopped.
Dirty pages are synchronized to disk.
Table metadata is written to the binary
.cfgfile.
Expected error log messages for this operation:
[Note] InnoDB: Sync to disk of '"test"."t1"' started.
[Note] InnoDB: Stopping purge
[Note] InnoDB: Writing table metadata to './test/t1.cfg'
[Note] InnoDB: Table '"test"."t1"' flushed to disk
When UNLOCK
TABLES is run on the source instance:
The binary
.cfgfile is deleted.The shared lock on the table or tables being imported is released and the purge coordinator thread is restarted.
Expected error log messages for this operation:
[Note] InnoDB: Deleting the meta-data file './test/t1.cfg'
[Note] InnoDB: Resuming purge
When ALTER TABLE
... IMPORT TABLESPACE is run on the destination
instance, the import algorithm performs the following operations
for each tablespace being imported:
Each tablespace page is checked for corruption.
The space ID and log sequence numbers (LSNs) on each page are updated.
Flags are validated and LSN updated for the header page.
Btree pages are updated.
The page state is set to dirty so that it is written to disk.
Expected error log messages for this operation:
[Note] InnoDB: Importing tablespace for table 'test/t1' that was exported
from host 'host_name'
[Note] InnoDB: Phase I - Update all pages
[Note] InnoDB: Sync to disk
[Note] InnoDB: Sync to disk - done!
[Note] InnoDB: Phase III - Flush changes to disk
[Note] InnoDB: Phase IV - Flush complete
You may also receive a warning that a tablespace is discarded
(if you discarded the tablespace for the destination table)
and a message stating that statistics could not be calculated
due to a missing .ibd file:
[Warning] InnoDB: Table "test"."t1" tablespace is set as discarded.
7f34d9a37700 InnoDB: cannot calculate statistics for table
"test"."t1" because the .ibd file is missing. For help, please refer to
http://dev.mysql.com/doc/refman/en/innodb-troubleshooting.html