7.4.1 Dumping Data in SQL Format with mysqldump使用mysqldump以SQL格式转储数据

This section describes how to use mysqldump to create SQL-format dump files. 本节介绍如何使用mysqldump创建SQL格式的转储文件。For information about reloading such dump files, see Section 7.4.2, “Reloading SQL-Format Backups”.有关重新加载此类转储文件的信息,请参阅第7.4.2节,“重新加载SQL格式备份”

By default, mysqldump writes information as SQL statements to the standard output. You can save the output in a file:默认情况下,mysqldump将信息作为SQL语句写入标准输出。您可以将输出保存在文件中:

shell> mysqldump [arguments] > file_name

To dump all databases, invoke mysqldump with the --all-databases option:要转储所有数据库,请使用--all-databases选项调用mysqldump

shell> mysqldump --all-databases > dump.sql

To dump only specific databases, name them on the command line and use the --databases option:要仅转储特定数据库,请在命令行上命名它们并使用--databases选项:

shell> mysqldump --databases db1 db2 db3 > dump.sql

The --databases option causes all names on the command line to be treated as database names. --databases选项使命令行上的所有名称都被视为数据库名称。Without this option, mysqldump treats the first name as a database name and those following as table names.如果没有此选项,mysqldump会将名字视为数据库名,将后面的名字视为表名。

With --all-databases or --databases, mysqldump writes CREATE DATABASE and USE statements prior to the dump output for each database. 对于--all-databases--databasesmysqldump在每个数据库的转储输出之前写入CREATE DATABASEUSE语句。This ensures that when the dump file is reloaded, it creates each database if it does not exist and makes it the default database so database contents are loaded into the same database from which they came. 这确保了在重新加载转储文件时,如果不存在,它会创建每个数据库,并将其设置为默认数据库,以便将数据库内容加载到它们来自的同一个数据库中。If you want to cause the dump file to force a drop of each database before recreating it, use the --add-drop-database option as well. 如果你想让转储文件在重新创建之前强制删除每个数据库,也可以使用--add-drop-database选项。In this case, mysqldump writes a DROP DATABASE statement preceding each CREATE DATABASE statement.在这种情况下,mysqldump在每个CREATE DATABASE语句之前写入一个DROP DATABASE语句。

To dump a single database, name it on the command line:要转储单个数据库,请在命令行上为其命名:

shell> mysqldump --databases test > dump.sql

In the single-database case, it is permissible to omit the --databases option:在单数据库的情况下,可以省略--databases选项:

shell> mysqldump test > dump.sql

The difference between the two preceding commands is that without --databases, the dump output contains no CREATE DATABASE or USE statements. This has several implications:前两个命令之间的区别在于,如果没有--databases,转储输出将不包含CREATE DATABASEUSE语句。这有几个含义:

To dump only specific tables from a database, name them on the command line following the database name:要仅转储数据库中的特定表,请在命令行中按照数据库名称命名它们:

shell> mysqldump test t1 t3 t7 > dump.sql

By default, if GTIDs are in use on the server where you create the dump file (gtid_mode=ON), mysqldump includes a SET @@GLOBAL.gtid_purged statement in the output to add the GTIDs from the gtid_executed set on the source server to the gtid_purged set on the target server. If you are dumping only specific databases or tables, it is important to note that the value that is included by mysqldump includes the GTIDs of all transactions in the gtid_executed set on the source server, even those that changed suppressed parts of the database, or other databases on the server that were not included in the partial dump. If you only replay one partial dump file on the target server, the extra GTIDs do not cause any problems with the future operation of that server. However, if you replay a second dump file on the target server that contains the same GTIDs (for example, another partial dump from the same source server), any SET @@GLOBAL.gtid_purged statement in the second dump file fails. To avoid this issue, either set the mysqldump option --set-gtid-purged to OFF or COMMENTED to output the second dump file without an active SET @@GLOBAL.gtid_purged statement, or remove the statement manually before replaying the dump file.