There are different reasons for creating
tables externally; that is, creating tables outside of the data
directory. Those reasons might include space management, I/O
optimization, or placing tables on a storage device with
particular performance or capacity characteristics, for example.
InnoDB supports the following methods for
creating tables externally:
You can create an
InnoDB table in an external
directory by specifying a
clause in the
CREATE TABLE statement.
CREATE TABLE t1 (c1 INT PRIMARY KEY) DATA DIRECTORY = '/external/directory';
DATA DIRECTORY clause is supported for
tables created in file-per-table tablespaces. Tables are
implicitly created in file-per-table tablespaces when the
is enabled, which it is by default.
mysql> SELECT @@innodb_file_per_table; +-------------------------+ | @@innodb_file_per_table | +-------------------------+ | 1 | +-------------------------+
For more information about file-per-table tablespaces, see Section 126.96.36.199, “File-Per-Table Tablespaces”.
When you specify a
DATA DIRECTORY clause in a
CREATE TABLE statement, the table's data file
is created in a schema directory under the specified directory.
The following example demonstrates creating a table in an
external directory using the
clause. It is assumed that the
mysql> USE test; Database changed mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY) DATA DIRECTORY = '/external/directory'; # MySQL creates the table's data file in a schema directory # under the external directory shell> cd /external/directory/test shell> ls t1.ibd
When creating a table in an external directory, ensure that the directory is known to
InnoDB. Otherwise, if the server halts unexpectedly before data file pages are fully flushed, startup fails when the data file is not found during the pre-recovery discovery phase that searches known directories for data files (see Tablespace Discovery During Crash Recovery). To make a directory known, add it to the
innodb_directoriesis a read-only startup option that defines directories to scan at startup for data files. Configuring it requires restarting the server.
MySQL initially holds the tablespace data file open, preventing you from dismounting the device, but might eventually close the file if the server is busy. Be careful not to accidentally dismount an external device while MySQL is running, or start MySQL while the device is disconnected. Attempting to access a table when the associated data file is missing causes a serious error that requires a server restart.
A server restart might fail if the data file is not found at the expected path. In this case, you can restore the tablespace data file from a backup or drop the table to remove the information about it from the data dictionary.
Before placing a table on an NFS-mounted volume, review potential issues outlined in Using NFS with MySQL.
If using an LVM snapshot, file copy, or other file-based mechanism to back up the table's data file, always use the
FLUSH TABLES ... FOR EXPORTstatement first to ensure that all changes buffered in memory are flushed to disk before the backup occurs.
DATA DIRECTORYclause to create a table in an external directory is an alternative to using symbolic links, which
InnoDBdoes not support.
CREATE TABLE ...
TABLESPACE syntax can be used in combination with the
DATA DIRECTORY clause to create a table in an
external directory. To do so, specify
innodb_file_per_table as the tablespace name.
mysql> CREATE TABLE t2 (c1 INT PRIMARY KEY) TABLESPACE = innodb_file_per_table DATA DIRECTORY = '/external/directory';
This method is supported only for tables created in
file-per-table tablespaces, but does not require the
to be enabled. In all other respects, this method is equivalent
CREATE TABLE ... DATA DIRECTORY method
described above. The same usage notes apply.
You can create a table in a general tablespace that resides in an external directory.