Dump a database in MySQL

This section describes how to use mysqldumparrow-up-right to create SQL-format dump files. For information about reloading such dump files, see Section 7.4.2, ā€œReloading SQL-Format Backupsā€arrow-up-right.

By default, mysqldumparrow-up-right writes information as SQL statements to the standard output. You can save the output in a file:

$> mysqldump [arguments] > file_name

To dump all databases, invoke mysqldumparrow-up-right with the --all-databasesarrow-up-right option:

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

To dump only specific databases, name them on the command line and use the --databasesarrow-up-right option:

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

The --databasesarrow-up-right option causes all names on the command line to be treated as database names. Without this option, mysqldumparrow-up-right treats the first name as a database name and those following as table names.

With --all-databasesarrow-up-right or --databasesarrow-up-right, mysqldumparrow-up-right writes CREATE DATABASEarrow-up-right and USEarrow-up-right statements prior to the dump output for each database. 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-databasearrow-up-right option as well. In this case, mysqldumparrow-up-right writes a DROP DATABASEarrow-up-right statement preceding each CREATE DATABASEarrow-up-right statement.

To dump a single database, name it on the command line:

$> mysqldump --databases test > dump.sql

In the single-database case, it is permissible to omit the --databasesarrow-up-right option:

$> mysqldump test > dump.sql

The difference between the two preceding commands is that without --databasesarrow-up-right, the dump output contains no CREATE DATABASEarrow-up-right or USEarrow-up-right statements. This has several implications:

  • When you reload the dump file, you must specify a default database name so that the server knows which database to reload.

  • For reloading, you can specify a database name different from the original name, which enables you to reload the data into a different database.

  • If the database to be reloaded does not exist, you must create it first.

  • Because the output contains no CREATE DATABASEarrow-up-right statement, the --add-drop-databasearrow-up-right option has no effect. If you use it, it produces no DROP DATABASEarrow-up-right statement.

To dump only specific tables from a database, name them on the command line following the database name:

Last updated