> For the complete documentation index, see [llms.txt](https://dojo.lkmx.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dojo.lkmx.io/ingles/mysql/dump-a-database-in-mysql.md).

# Dump a database in MySQL

This section describes how to use [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) to create SQL-format dump files. For information about reloading such dump files, see [Section 7.4.2, “Reloading SQL-Format Backups”](https://dev.mysql.com/doc/refman/5.7/en/reloading-sql-format-dumps.html).

By default, [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) writes information as SQL statements to the standard output. You can save the output in a file:

```terminal
$> mysqldump [arguments] > file_name
```

To dump all databases, invoke [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) with the [`--all-databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_all-databases) option:

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

To dump only specific databases, name them on the command line and use the [`--databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_databases) option:

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

The [`--databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_databases) option causes all names on the command line to be treated as database names. Without this option, [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) treats the first name as a database name and those following as table names.

With [`--all-databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_all-databases) or [`--databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_databases), [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) writes [`CREATE DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/create-database.html) and [`USE`](https://dev.mysql.com/doc/refman/5.7/en/use.html) 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-database`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database) option as well. In this case, [**mysqldump**](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) writes a [`DROP DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/drop-database.html) statement preceding each [`CREATE DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/create-database.html) statement.

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

```terminal
$> mysqldump --databases test > dump.sql
```

In the single-database case, it is permissible to omit the [`--databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_databases) option:

```terminal
$> mysqldump test > dump.sql
```

The difference between the two preceding commands is that without [`--databases`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_databases), the dump output contains no [`CREATE DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/create-database.html) or [`USE`](https://dev.mysql.com/doc/refman/5.7/en/use.html) 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 DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/create-database.html) statement, the [`--add-drop-database`](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database) option has no effect. If you use it, it produces no [`DROP DATABASE`](https://dev.mysql.com/doc/refman/5.7/en/drop-database.html) statement.

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

```terminal
$> mysqldump test t1 t3 t7 > dump.sql
```

## Reference Links

* <https://dev.mysql.com/doc/refman/5.7/en/mysqldump-sql-format.html>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dojo.lkmx.io/ingles/mysql/dump-a-database-in-mysql.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
