本节描述了如何使用 mysqldump 创建分隔文本转储文件。有关重新加载此类转储文件的信息,请参见 第 9.4.4 节,“重新加载分隔文本格式备份”。This section describes how to use mysqldump to create delimited-text dump files. For information about reloading such dump files, see Section 9.4.4, “Reloading Delimited-Text Format Backups”.
如果你使用 mysqldump 并带有 --tab= 选项,它会使用 dir_namedir_name 作为输出目录,并在该目录中分别导出表,每个表使用两个文件。表名将作为这些文件的基本名称。对于名为 t1 的表,这些文件分别命名为 t1.sql 和 t1.txt。.sql 文件包含表的 CREATE TABLE 语句。.txt 文件包含表数据,每行对应表中的一行数据。If you invoke mysqldump with the
--tab=
option, it uses dir_namedir_name as the
output directory and dumps tables individually in that directory
using two files for each table. The table name is the base name
for these files. For a table named t1, the
files are named t1.sql and
t1.txt. The .sql file
contains a CREATE TABLE statement
for the table. The .txt file contains the
table data, one line per table row.
以下命令将 db1 数据库的内容导出到 /tmp 数据库中的文件:The following command dumps the contents of the
db1 database to files in the
/tmp database:
$> mysqldump --tab=/tmp db1
包含表数据的 .txt 文件由服务器写入,因此它们归用于运行服务器的系统账户所有。服务器使用 SELECT ... INTO
OUTFILE 来写入文件,因此您必须拥有 FILE 权限才能执行此操作,如果指定的 .txt 文件已存在,则会发生错误。The .txt files containing table data are
written by the server, so they are owned by the system account
used for running the server. The server uses
SELECT ... INTO
OUTFILE to write the files, so you must have the
FILE privilege to perform this
operation, and an error occurs if a given
.txt file already exists.
服务器将转储表的 CREATE 定义发送给 mysqldump,后者将它们写入 .sql 文件。因此,这些文件归执行 mysqldump 的用户所有。The server sends the CREATE definitions for
dumped tables to mysqldump, which writes them
to .sql files. These files therefore are
owned by the user who executes mysqldump.
最好仅将--tab用于转储本地服务器。如果在远程服务器上使用它,则--tab目录必须在本地主机和远程主机上都存在,.txt文件由服务器写入远程目录(在服务器主机上),而.sql文件由mysqldump写入本地目录(在客户端主机上)。It is best that --tab be used
only for dumping a local server. If you use it with a remote
server, the --tab directory
must exist on both the local and remote hosts, and the
.txt files are written by the server in the
remote directory (on the server host), whereas the
.sql files are written by
mysqldump in the local directory (on the
client host).
对于 mysqldump --tab,服务器默认将表数据写入 .txt 文件,每行一条记录,列值之间用制表符分隔,列值不加引号,换行符作为行终止符。(这些默认设置与 SELECT ... INTO
OUTFILE 相同。)For mysqldump --tab, the server by default
writes table data to .txt files one line
per row with tabs between column values, no quotation marks
around column values, and newline as the line terminator. (These
are the same defaults as for
SELECT ... INTO
OUTFILE.)
为了使数据文件以不同格式写入,mysqldump 支持以下选项:To enable data files to be written using a different format, mysqldump supports these options:
用于分隔列值的字符串(默认:制表符)。The string for separating column values (default: tab).
--fields-enclosed-by=char--fields-enclosed-by=char用于括住列值的字符(默认:无字符)。The character within which to enclose column values (default: no character).
--fields-optionally-enclosed-by=char用于括住非数字列值的字符(默认:无字符)。The character within which to enclose non-numeric column values (default: no character).
--fields-escaped-by=char--fields-escaped-by=char转义特殊字符的字符(默认:不转义)。The character for escaping special characters (default: no escaping).
行终止字符串(默认:换行符)。The line-termination string (default: newline).
根据您为这些选项中任何一个指定的值,可能需要在命令行中对该值进行适当的引用或转义以适应您的命令解释器。或者,也可以使用十六进制表示法来指定该值。假设您希望 mysqldump 使用双引号引用列值。要实现此目的,请将双引号指定为 --fields-enclosed-by 选项的值。但此字符对于命令解释器来说通常是特殊字符,必须特殊处理。例如,在 Unix 上,您可以这样引用双引号:Depending on the value you specify for any of these options, it
might be necessary on the command line to quote or escape the
value appropriately for your command interpreter. Alternatively,
specify the value using hex notation. Suppose that you want
mysqldump to quote column values within
double quotation marks. To do so, specify double quote as the
value for the
--fields-enclosed-by
option. But this character is often special to command
interpreters and must be treated specially. For example, on
Unix, you can quote the double quote like this:
--fields-enclosed-by='"'
在任何平台上,您都可以以十六进制指定数值:On any platform, you can specify the value in hex:
--fields-enclosed-by=0x22
通常会将几种数据格式选项一起使用。例如,要以逗号分隔值格式转储表格,并使用回车/换行对 (\r\n) 终止行,请使用以下命令(在一行中输入):It is common to use several of the data-formatting options
together. For example, to dump tables in comma-separated values
format with lines terminated by carriage-return/newline pairs
(\r\n), use this command (enter it on a
single line):
$>mysqldump --tab=/tmp --fields-terminated-by=,--fields-enclosed-by='"' --lines-terminated-by=0x0d0a db1
如果您使用任何数据格式选项来导出表数据,在稍后重新加载数据文件时,您需要指定相同的格式,以确保正确解释文件内容。Should you use any of the data-formatting options to dump table data, you need to specify the same format when you reload data files later, to ensure proper interpretation of the file contents.