13.2.5 IMPORT TABLE Statement语句

IMPORT TABLE FROM sdi_file [, sdi_file] ...

The IMPORT TABLE statement imports MyISAM tables based on information contained in .sdi (serialized dictionary information) metadata files. IMPORT TABLE语句根据.sdi(序列化字典信息)元数据文件中包含的信息导入MyISAM表。IMPORT TABLE requires the FILE privilege to read the .sdi and table content files, and the CREATE privilege for the table to be created.IMPORT TABLE需要FILE权限来读取.sdi和表内容文件,并且需要创建表的创建权限。

Tables can be exported from one server using mysqldump to write a file of SQL statements and imported into another server using mysql to process the dump file. 可以使用mysqldump从一台服务器导出表来编写SQL语句文件,然后使用mysql将表导入另一台服务器来处理转储文件。IMPORT TABLE provides a faster alternative using the raw table files.IMPORT TABLE使用“原始”表文件提供了更快的替代方法。

Prior to import, the files that provide the table content must be placed in the appropriate schema directory for the import server, and the .sdi file must be located in a directory accessible to the server. 导入之前,提供表内容的文件必须放置在导入服务器的相应架构目录中,.sdi文件必须位于服务器可以访问的目录中。For example, the .sdi file can be placed in the directory named by the secure_file_priv system variable, or (if secure_file_priv is empty) in a directory under the server data directory.例如,可以将.sdi文件放在由secure_file_priv系统变量命名的目录中,或者(如果secure_file_priv为空)放在服务器数据目录下的目录中。

The following example describes how to export MyISAM tables named employees and managers from the hr schema of one server and import them into the hr schema of another server. 下面的示例描述如何从一台服务器的hr架构导出名为employeesmanagersMyISAM表,并将它们导入另一台服务器的hr架构。The example uses these assumptions (to perform a similar operation on your own system, modify the path names as appropriate):该示例使用这些假设(要在自己的系统上执行类似的操作,请根据需要修改路径名):

To export tables from the export server, use this procedure:要从导出服务器导出表,请使用以下步骤:

  1. Ensure a consistent snapshot by executing this statement to lock the tables so that they cannot be modified during export:通过执行以下语句锁定表以确保快照的一致性,以便在导出期间无法修改这些表:

    mysql> FLUSH TABLES hr.employees, hr.managers WITH READ LOCK;

    While the lock is in effect, the tables can still be used, but only for read access.当锁生效时,表仍然可以使用,但只能用于读取访问。

  2. At the file system level, copy the .sdi and table content files from the hr schema directory to the secure export directory:在文件系统级别,将.sdi和表内容文件从hr架构目录复制到安全导出目录:

    • The .sdi file is located in the hr schema directory, but might not have exactly the same basename as the table name. .sdi文件位于hr架构目录中,但可能没有与表名完全相同的基本名。For example, the .sdi files for the employees and managers tables might be named employees_125.sdi and managers_238.sdi.例如,employees表和managers表的.sdi文件可能命名为employees_125.sdimanagers_238.sdi

    • For a MyISAM table, the content files are its .MYD data file and .MYI index file.对于MyISAM表,内容文件是其.MYD数据文件和.MYI索引文件。

    Given those file names, the copy commands look like this:给定这些文件名,copy命令如下所示:

    shell> cd export_basedir/data/hr
    shell> cp employees_125.sdi /tmp/export
    shell> cp managers_238.sdi /tmp/export
    shell> cp employees.{MYD,MYI} /tmp/export
    shell> cp managers.{MYD,MYI} /tmp/export
  3. Unlock the tables:解锁表:

    mysql> UNLOCK TABLES;

To import tables into the import server, use this procedure:要将表导入导入导入服务器,请使用以下过程:

  1. The import schema must exist. 导入架构必须存在。If necessary, execute this statement to create it:如有必要,请执行以下语句来创建它:

    mysql> CREATE SCHEMA hr;
  2. At the file system level, copy the .sdi files to the import server secure_file_priv directory, /tmp/mysql-files. 在文件系统级别,将.sdi文件复制到import server secure_file_priv目录/tmp/mysql-filesAlso, copy the table content files to the hr schema directory:另外,将表内容文件复制到hr架构目录:

    shell> cd /tmp/export
    shell> cp employees_125.sdi /tmp/mysql-files
    shell> cp managers_238.sdi /tmp/mysql-files
    shell> cp employees.{MYD,MYI} import_basedir/data/hr
    shell> cp managers.{MYD,MYI} import_basedir/data/hr
  3. Import the tables by executing an IMPORT TABLE statement that names the .sdi files:通过执行Import TABLE语句来导入表,该语句将.sdi文件命名为:

    mysql> IMPORT TABLE FROM
    '/tmp/mysql-files/employees.sdi',
    '/tmp/mysql-files/managers.sdi';

The .sdi file need not be placed in the import server directory named by the secure_file_priv system variable if that variable is empty; it can be in any directory accessible to the server, including the schema directory for the imported table. 如果secure_file_priv系统变量指定的导入服务器目录为空,则不需要将.sdi文件放在该目录中;它可以位于服务器可访问的任何目录中,包括导入表的架构目录。If the .sdi file is placed in that directory, however, it may be rewritten; the import operation creates a new .sdi file for the table, which overwrites the old .sdi file if the operation uses the same file name for the new file.但是,如果.sdi文件放在该目录中,它可能会被重写;导入操作为表创建一个新的.sdi文件,如果操作对新文件使用相同的文件名,则会覆盖旧的.sdi文件。

Each sdi_file value must be a string literal that names the .sdi file for a table or is a pattern that matches .sdi files. 每个sdi_file值必须是一个为表命名.sdi文件的字符串文字,或者是一个与.sdi文件匹配的模式。If the string is a pattern, any leading directory path and the .sdi file name suffix must be given literally. 如果字符串是一个模式,则任何前导目录路径和.sdi文件名后缀必须按字面给出。Pattern characters are permitted only in the base name part of the file name:仅允许在文件名的基名称部分使用模式字符:

Using a pattern, the previous IMPORT TABLE statement could have been written like this (assuming that the /tmp/mysql-files directory contains no other .sdi files matching the pattern):使用模式,前面的IMPORT TABLE语句可以这样编写(假设/tmp/mysql-files目录不包含与模式匹配的其他.sdi文件):

IMPORT TABLE FROM '/tmp/mysql-files/*.sdi';

To interpret the location of .sdi file path names, the server uses the same rules for IMPORT TABLE as the server-side rules for LOAD DATA (that is, the non-LOCAL rules). 为了解释.sdi文件路径名的位置,服务器使用与加载数据的服务器端规则(即非本地规则)相同的IMPORT TABLE规则。See Section 13.2.7, “LOAD DATA Statement”, paying particular attention to the rules used to interpret relative path names.请参阅第13.2.7节,“加载数据语句”,特别注意用于解释相对路径名的规则。

IMPORT TABLE fails if the .sdi or table files cannot be located. 如果找不到.sdi或表文件,则IMPORT TABLE失败。After importing a table, the server attempts to open it and reports as warnings any problems detected. 导入表后,服务器将尝试打开它,并将检测到的任何问题报告为警告。To attempt a repair to correct any reported issues, use REPAIR TABLE.要尝试修复以更正任何报告的问题,请使用REPAIR TABLE

IMPORT TABLE is not written to the binary log.IMPORT TABLE未写入二进制日志。

Restrictions and Limitations限制和限制

IMPORT TABLE applies only to non-TEMPORARY MyISAM tables. IMPORT TABLE仅适用于非临时MyISAM表。It does not apply to tables created with a transactional storage engine, tables created with CREATE TEMPORARY TABLE, or views.它不适用于使用事务存储引擎创建的表、使用CREATE TEMPORARY TABLE创建的表或视图。

An .sdi file used in an import operation must be generated on a server with the same data dictionary version and sdi version as the import server. 导入操作中使用的.sdi文件必须在与导入服务器具有相同数据字典版本和sdi版本的服务器上生成。The version information of the generating server is found in the .sdi file:生成服务器的版本信息位于.sdi文件中:

{
   "mysqld_version_id":80019,
   "dd_version":80017,
   "sdi_version":80016,
   ...
}

To determine the data dictionary and sdi version of the import server, you can check the .sdi file of a recently created table on the import server.要确定导入服务器的数据字典和sdi版本,可以检查导入服务器上最近创建的表的.sdi文件。

The table data and index files must be placed in the schema directory for the import server prior to the import operation, unless the table as defined on the export server uses the DATA DIRECTORY or INDEX DIRECTORY table options. 在导入操作之前,表数据和索引文件必须放置在导入服务器的架构目录中,除非导出服务器上定义的表使用DATA DIRECTORYINDEX DIRECTORY表选项。In that case, modify the import procedure using one of these alternatives before executing the IMPORT TABLE statement:在这种情况下,在执行import TABLE语句之前,请使用以下替代方法之一修改导入过程:

  • Put the data and index files into the same directory on the import server host as on the export server host, and create symlinks in the import server schema directory to those files.将数据和索引文件放入导入服务器主机上与导出服务器主机上相同的目录中,并在导入服务器架构目录中创建指向这些文件的符号链接。

  • Put the data and index files into an import server host directory different from that on the export server host, and create symlinks in the import server schema directory to those files. 将数据和索引文件放入不同于导出服务器主机的导入服务器主机目录中,并在导入服务器架构目录中创建指向这些文件的符号链接。In addition, modify the .sdi file to reflect the different file locations.另外,修改.sdi文件以反映不同的文件位置。

  • Put the data and index files into the schema directory on the import server host, and modify the .sdi file to remove the data and index directory table options.将数据和索引文件放入导入服务器主机上的schema目录,并修改.sdi文件以删除数据和索引目录表选项。

Any collation IDs stored in the .sdi file must refer to the same collations on the export and import servers.存储在.sdi文件中的任何排序规则ID都必须引用导出和导入服务器上的相同排序规则。

Trigger information for a table is not serialized into the table .sdi file, so triggers are not restored by the import operation.表的触发器信息没有序列化到表.sdi文件中,因此导入操作不会还原触发器。

Some edits to an .sdi file are permissible prior to executing the IMPORT TABLE statement, whereas others are problematic or may even cause the import operation to fail:在执行IMPORT TABLE语句之前,允许对.sdi文件进行某些编辑,而其他编辑则有问题,甚至可能导致导入操作失败:

  • Changing the data directory and index directory table options is required if the locations of the data and index files differ between the export and import servers.如果数据和索引文件的位置在导出服务器和导入服务器之间不同,则需要更改数据目录和索引目录表选项。

  • Changing the schema name is required to import the table into a different schema on the import server than on the export server.需要更改架构名称才能将表导入导入导入服务器上与导出服务器上不同的架构。

  • Changing schema and table names may be required to accommodate differences between file system case-sensitivity semantics on the export and import servers or differences in lower_case_table_names settings. 可能需要更改架构和表名以适应导出和导入服务器上的文件系统区分大小写语义之间的差异,或者在lower_case_table_names设置中的差异。Changing the table names in the .sdi file may require renaming the table files as well.更改.sdi文件中的表名可能还需要重命名表文件。

  • In some cases, changes to column definitions are permitted. 在某些情况下,允许更改列定义。Changing data types is likely to cause problems.更改数据类型可能会导致问题。