> For the complete documentation index, see [llms.txt](https://hezhiqiang-book.gitbook.io/mysql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang-book.gitbook.io/mysql/di-ba-zhang/bei-fen-shu-ju-ku.md).

# 备份数据库

在本教程中，您将学习如何使用mysqldump工具备份MySQL数据库。

MySQL GUI工具(如phpMyAdmin，SQLyog等)通常为备份MySQL数据库提供了方便的功能。 但是，如果您的数据库很大，则备份过程可能非常慢，因为备份文件需要通过网络传输到客户端PC。因此，备份过程增加了MySQL数据库服务器的锁定和可用时间。

MySQL提供了非常有用的工具，用于在服务器上本地备份或转储MySQL数据库。 备份文件存储在服务器中的文件系统中，因此您只需在需要时下载即可。

备份MySQL数据库的工具是`mysqldump`。它位于MySQL安装文件夹的根`/bin`文件夹中。如本教程安装的位置为：`D:\software\mysql-5.7.18-winx64\bin\mysqldump.exe`

`mysqldump`是由MySQL提供的程序，可用于转储数据库以备数据库或将数据库传输到另一个数据库服务器。

转储文件包含一组用于创建数据库对象的SQL语句。 此外，`mysqldump`可用于生成CSV，分隔符或XML文件。 在本教程中，我们将仅关注如何使用`mysqldump`工具备份MySQL数据库。

在本教程中，我们将仅关注如何使用`mysqldump`工具备份MySQL数据库。

## 如何备份MySQL数据库

要备份MySQL数据库，数据库首先必须存在于数据库服务器中，并且您也可以访问该服务器。 如果没有远程桌面，可以使用*SSH*或*Telnet*登录到远程服务器。备份MySQL数据库的命令如下：

```sql
mysqldump -u [username] –p[password] [database_name] > [dump_file.sql]
```

上述命令的参数如下：

* `[username]`：有效的MySQL用户名。
* `[password]`：用户的有效密码。 请注意，`-p`和密码之间没有空格。
* `[database_name]`: 要备份的数据库名称
* `[dump_file.sql]`: 要生成的转储文件。

通过执行上述命令，所有数据库结构和数据将导出到一个`[dump_file.sql]`转储文件中。 例如，要备份示例数据库`yiibaidb`，可使用以下命令：

```sql
mysqldump -u root –p123456  yiibaidb > D:\worksp\bakup\yiibaidb001.sql
```

执行上面语句，如下所示 -

```sql
C:\Users\Administrator> mysqldump -u root -p123456  yiibaidb > D:\worksp\bakup\yiibaidb001.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

C:\Users\Administrator>
```

执行后，将创建一个文件：`D:\worksp\bakup\yiibaidb001.sql`

## 如何仅备份MySQL数据库结构

如果只想备份数据库结构而不需要备份数据，那么只需要添加一个选项`-no-data`来告诉`mysqldump`只需要导出数据库结构，如下：

```sql
mysqldump -u [username] –p[password] –no-data [database_name] > [dump_file.sql]
```

例如，仅使用结构来备份示例数据库，可以使用以下命令：

```sql
C:\Users\Administrator> mysqldump -u root –p123456  -no-data yiibaidb > D:\worksp\bakup\backup002.sql
```

## 如何仅备份MySQL数据库数据

有一种情况，您希望在分段和开发系统中刷新数据，因此这些系统中的数据与生产系统相同。

在这种情况下，只需要从生产系统导出数据，并将其导入到临时或开发系统中。要实现只备份数据，您可以使用`mysqldump`的选项`-no-create-info`，如下所示：

```sql
mysqldump -u [username] –p[password] –no-create-info [database_name] > [dump_file.sql]
```

例如，要仅使用数据来备份示例数据库(`yiibaidb`)，请使用以下命令：

```sql
mysqldump –u root –p123456 –no-create-info yiibaidb > D:\worksp\bakup\backup003.sql
```

## 如何将多个MySQL数据库备份到一个文件中

如果要通过`[database_name]`中的命令来备份多个数据库，只需单独的数据库名称即可。 如果要备份数据库服务器中的所有数据库，请使用选项`-all-database`。

```sql
mysqldump -u [username] –p[password]  [dbname1,dbname2,…] > [all_dbs_dump_file.sql]

mysqldump -u [username] –p[password] –all-database > [all_dbs_dump_file.sql]
```

在本教程中，您已经学习了如何使用`mysqldump`工具来备份具有多种选项的MySQL数据库。


---

# 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:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-ba-zhang/bei-fen-shu-ju-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
