8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

15.2.6 IMPORT TABLE 语句15.2.6 IMPORT TABLE Statement

IMPORT TABLE FROM sdi_file [, sdi_file] ...

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

表可以通过在一台服务器上使用 mysqldump 导出为 SQL 语句文件,并在另一台服务器上使用 mysql 处理该 dump 文件导入。IMPORT TABLE 提供了一种使用表文件的 “raw” 方式更快速的替代方法。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. IMPORT TABLE provides a faster alternative using the “raw” table files.

在导入之前,提供表内容的文件必须放置在导入服务器的相应模式目录中,且 .sdi 文件必须位于服务器可访问的目录中。例如,.sdi 文件可以放置在由系统变量 secure_file_priv 指定的目录中,或者(如果 secure_file_priv 为空)放在服务器数据目录下的某个目录中。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. 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.

下面的示例描述了如何将一个服务器的 hr 模式中名为 employees 和 managers 的 MyISAM 表导出,并导入到另一服务器的 hr 模式中。该示例使用了以下假设(要在您自己的系统上执行类似操作,请根据需要修改路径名):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. The example uses these assumptions (to perform a similar operation on your own system, modify the path names as appropriate):

  • 对于导出服务器,export_basedir 表示其基目录,其数据目录为 export_basedir/data。For the export server, export_basedir represents its base directory, and its data directory is export_basedir/data.

  • 对于导入服务器,import_basedir 表示其基目录,其数据目录为 import_basedir/data。For the import server, import_basedir represents its base directory, and its data directory is import_basedir/data.

  • 表文件从导出服务器导出到 /tmp/export 目录,该目录是安全的(其他用户无法访问)。Table files are exported from the export server into the /tmp/export directory and this directory is secure (not accessible to other users).

  • 导入服务器使用 /tmp/mysql-files 作为其系统变量 secure_file_priv 指定的目录。The import server uses /tmp/mysql-files as the directory named by its secure_file_priv system variable.

要从导出服务器导出表,请使用以下步骤: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. 在文件系统级别,将 .sdi 文件和表内容文件从 hr 模式目录复制到安全导出目录:At the file system level, copy the .sdi and table content files from the hr schema directory to the secure export directory:

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

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

    根据这些文件名,复制命令如下:Given those file names, the copy commands look like this:

    $> cd export_basedir/data/hr
    $> cp employees_125.sdi /tmp/export
    $> cp managers_238.sdi /tmp/export
    $> cp employees.{MYD,MYI} /tmp/export
    $> 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. 在文件系统级别,将 .sdi 文件复制到导入服务器的 secure_file_priv 目录,即 /tmp/mysql-files。同时,将表内容文件复制到 hr 模式目录:At the file system level, copy the .sdi files to the import server secure_file_priv directory, /tmp/mysql-files. Also, copy the table content files to the hr schema directory:

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

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

如果 secure_file_priv 系统变量为空,则 .sdi 文件不必放在导入服务器目录中;它可以放在服务器可以访问的任何目录,包括导入表的模式目录。然而,如果 .sdi 文件放在该目录中,它可能会被重写;导入操作会为表创建一个新的 .sdi 文件,如果操作为新文件使用相同的文件名,将会覆盖旧的 .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. 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_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. If the string is a pattern, any leading directory path and the .sdi file name suffix must be given literally. Pattern characters are permitted only in the base name part of the file name:

  • ? 匹配任意单个字符? matches any single character

  • * 匹配任意字符序列,包括没有字符* matches any sequence of characters, including no characters

使用模式时,前面的 IMPORT TABLE 语句可以这样编写(假设 /tmp/mysql-files 目录中没有其他匹配该模式的 .sdi 文件):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 FROM '/tmp/mysql-files/*.sdi';

为了解释 .sdi 文件路径名的位置,服务器使用与 IMPORT TABLE 相同的规则,作为服务器端 LOAD DATA 的规则(即非 LOCAL 规则)。参见 第 15.2.9 章,“LOAD DATA 语句”,特别注意用于解释相对路径名的规则。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). See Section 15.2.9, “LOAD DATA Statement”, paying particular attention to the rules used to interpret relative path names.

IMPORT TABLE 如果无法找到 .sdi 或表文件则会失败。导入表后,服务器会尝试打开它,并报告任何检测到的问题作为警告。要尝试修复以纠正任何报告的问题,请使用 REPAIR TABLE。IMPORT TABLE fails if the .sdi or table files cannot be located. 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.

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

限制和局限性Restrictions and Limitations

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

用于导入操作的 .sdi 文件必须在与导入服务器具有相同数据字典版本和 sdi 版本的服务器上生成。生成服务器的版本信息可以在 .sdi 文件中找到: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. The version information of the generating server is found in the .sdi file:

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

要确定导入服务器的数据字典和 sdi 版本,可以检查导入服务器上最近创建表的 .sdi 文件。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.

在导入操作之前,表的数据和索引文件必须放置在导入服务器的模式目录中,除非导出服务器上定义的表使用了 DATA DIRECTORY 或 INDEX DIRECTORY 表选项。在这种情况下,在执行 IMPORT TABLE 语句之前,请使用以下替代方案修改导入过程: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. In that case, modify the import procedure using one of these alternatives before executing the IMPORT TABLE statement:

  • 将数据和索引文件放入与导出服务器主机相同的导入服务器主机目录中,并在导入服务器模式目录中为这些文件创建符号链接。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.

  • 将数据和索引文件放入与导出服务器主机不同的导入服务器主机目录中,并在导入服务器模式目录中为这些文件创建符号链接。此外,修改 .sdi 文件以反映不同的文件位置。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.

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

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

在执行 .sdi 语句之前,对 IMPORT TABLE 文件进行某些编辑是允许的,而其他编辑可能存在问题,甚至可能导致导入操作失败: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:

  • 如果导出和导入服务器之间的数据和索引文件位置不同,则必须更改数据目录和索引目录表选项。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.

  • 在某些情况下,可能需要更改模式和表名,以适应导出和导入服务器之间文件系统大小写敏感语义的差异,或 lower_case_table_names 设置的差异。更改 .sdi 文件中的表名可能还需要重命名表文件。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. Changing the table names in the .sdi file may require renaming the table files as well.

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