使用 mysqldump 进行完美备份

发布于 2024-07-26 05:40:23 字数 229 浏览 6 评论 0原文

如何使用mysqldump对mysql数据库进行完美备份? 当我进行备份时,只会备份指定数据库中的表。 过程和功能不是。

这是我正在使用的备份命令: (操作系统为Windows Vista。)

mysqldump -u username -p db1 > backup.sql

How can I make a perfect backup of mysql database using mysqldump?
When I am making a backup, my tables from specified database are only getting backed up. The procedures and functions are not.

Here's the backup command I am using:
(Operating system is Windows Vista.)

mysqldump -u username -p db1 > backup.sql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

菩提树下叶撕阳。 2024-08-02 05:40:24

如果您想在不中断任何连接的情况下进行完整备份,即所有数据库、过程、例程和事件:

mysqldump -u [username] -p -A -R -E --triggers --single-transaction > full_backup.sql
  1. -A 对于所有数据库(您也可以使用 --all-databases< /code>)
  2. -R 对于所有例程(存储过程和触发器)
  3. -E 对于所有事件
  4. --single-transaction 不锁定即,不中断任何连接(读/写)。

如果您只想备份指定的数据库:

mysqldump -u [username] -p [database_name] [other_database_name] -R -e --triggers --single-transaction > database_backup.sql

如果您只想备份数据库中的特定表:

mysqldump -u [username] -p [database_name] [table_name] > table_backup.sql

如果您只想备份数据库结构,只需添加 -- no-data 到前面的命令:

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

mysqldump 还有更多选项,这些选项都记录在 mysqldump 文档 或在命令行运行 man mysqldump

If you want to take a full backup i.e., all databases, procedures, routines, and events without interrupting any connections:

mysqldump -u [username] -p -A -R -E --triggers --single-transaction > full_backup.sql
  1. -A For all databases (you can also use --all-databases)
  2. -R For all routines (stored procedures & triggers)
  3. -E For all events
  4. --single-transaction Without locking the tables i.e., without interrupting any connection (R/W).

If you want to take a backup of only specified database(s):

mysqldump -u [username] -p [database_name] [other_database_name] -R -e --triggers --single-transaction > database_backup.sql

If you want to take a backup of only a specific table in a database:

mysqldump -u [username] -p [database_name] [table_name] > table_backup.sql

If you want to take a backup of the database structure only just add --no-data to the previous commands:

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

mysqldump has many more options, which are all documented in the mysqldump documentation or by running man mysqldump at the command line.

硪扪都還晓 2024-08-02 05:40:24

这有点取决于你的版本。 在 5.0.13 之前,这对于 mysqldump 是不可能的。

来自 mysqldump 手册页 (v 5.1.30)

 --routines, -R

      Dump stored routines (functions and procedures) from the dumped
      databases. Use of this option requires the SELECT privilege for the
      mysql.proc table. The output generated by using --routines contains
      CREATE PROCEDURE and CREATE FUNCTION statements to re-create the
      routines. However, these statements do not include attributes such
      as the routine creation and modification timestamps. This means that
      when the routines are reloaded, they will be created with the
      timestamps equal to the reload time.
      ...

      This option was added in MySQL 5.0.13. Before that, stored routines
      are not dumped. Routine DEFINER values are not dumped until MySQL
      5.0.20. This means that before 5.0.20, when routines are reloaded,
      they will be created with the definer set to the reloading user. If
      you require routines to be re-created with their original definer,
      dump and load the contents of the mysql.proc table directly as
      described earlier.

It depends a bit on your version. Before 5.0.13 this is not possible with mysqldump.

From the mysqldump man page (v 5.1.30)

 --routines, -R

      Dump stored routines (functions and procedures) from the dumped
      databases. Use of this option requires the SELECT privilege for the
      mysql.proc table. The output generated by using --routines contains
      CREATE PROCEDURE and CREATE FUNCTION statements to re-create the
      routines. However, these statements do not include attributes such
      as the routine creation and modification timestamps. This means that
      when the routines are reloaded, they will be created with the
      timestamps equal to the reload time.
      ...

      This option was added in MySQL 5.0.13. Before that, stored routines
      are not dumped. Routine DEFINER values are not dumped until MySQL
      5.0.20. This means that before 5.0.20, when routines are reloaded,
      they will be created with the definer set to the reloading user. If
      you require routines to be re-created with their original definer,
      dump and load the contents of the mysql.proc table directly as
      described earlier.
猫腻 2024-08-02 05:40:24

使用这些命令:-

mysqldump <other mysqldump options> --routines > outputfile.sql

如果我们只想备份存储过程和触发器而不是 mysql 表和数据,那么我们应该运行如下命令:

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt <database> > outputfile.sql

如果您需要将它们导入到另一个数据库/服务器,则必须运行如下命令:

mysql <database> < outputfile.sql

Use these commands :-

mysqldump <other mysqldump options> --routines > outputfile.sql

If we want to backup ONLY the stored procedures and triggers and not the mysql tables and data then we should run something like:

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt <database> > outputfile.sql

If you need to import them to another db/server you will have to run something like:

mysql <database> < outputfile.sql
橘寄 2024-08-02 05:40:24

除了 --routines 标志之外,您还需要授予备份用户读取存储过程的权限:

GRANT SELECT ON `mysql`.`proc` TO <backup user>@<backup host>;

我为备份用户授予的最小 GRANT 权限集是:

GRANT USAGE ON *.* TO ...
GRANT SELECT, LOCK TABLES ON <target_db>.* TO ...
GRANT SELECT ON `mysql`.`proc` TO ...

In addition to the --routines flag you will need to grant the backup user permissions to read the stored procedures:

GRANT SELECT ON `mysql`.`proc` TO <backup user>@<backup host>;

My minimal set of GRANT privileges for the backup user are:

GRANT USAGE ON *.* TO ...
GRANT SELECT, LOCK TABLES ON <target_db>.* TO ...
GRANT SELECT ON `mysql`.`proc` TO ...
岁月如刀 2024-08-02 05:40:24

我使用的是 MySQL 5.5.40。 此版本有选项--all-databases

mysqldump -u<username> -p<password> --all-databases --events > /tmp/all_databases__`date +%d_%b_%Y_%H_%M_%S`.sql

该命令将为 MySQL 服务器中的所有数据库创建一个完美的备份,并保存到以当前日期时间命名的文件中。

I am using MySQL 5.5.40. This version has the option --all-databases

mysqldump -u<username> -p<password> --all-databases --events > /tmp/all_databases__`date +%d_%b_%Y_%H_%M_%S`.sql

This command will create a perfect backup of all databases in MySQL server to file named to current date-time.

无戏配角 2024-08-02 05:40:24

使用“-R”来备份存储过程,但还要记住,如果您希望在修改数据库时对其进行一致的转储,则需要使用 --single-transaction (如果您只备份innodb) 或 --lock-all-tables (如果您还需要 myisam 表)

Use '-R' to backup stored procedures, but also keep in mind that if you want a consistent dump of your database while its being modified you need to use --single-transaction (if you only backup innodb) or --lock-all-tables (if you also need myisam tables)

另类 2024-08-02 05:40:24

在 MySQL 5.7 上它对我有用,我使用的是 CentOS7。

用于转储。

命令:

mysqldump -u user_name -p database_name -R -E > file_name.sql

示例:

mysqldump -u root -p mr_sbc_clean -R -E > mr_sbc_clean_dump.sql

用于部署转储。

命令:

mysql -u user_name -p database_name < file_name.sql

示例:

mysql -u root -p mr_sbc_clean_new < mr_sbc_clean_dump.sql

On MySQL 5.7 its work for me, I'm using CentOS7.

For taking Dump.

Command :

mysqldump -u user_name -p database_name -R -E > file_name.sql

Exemple :

mysqldump -u root -p mr_sbc_clean -R -E > mr_sbc_clean_dump.sql

For deploying Dump.

Command :

mysql -u user_name -p database_name < file_name.sql

Exemple :

mysql -u root -p mr_sbc_clean_new < mr_sbc_clean_dump.sql
对风讲故事 2024-08-02 05:40:24

要创建转储,请按照以下步骤操作:

  1. 打开 CMD 并转到安装 MySQL 的 bin 文件夹
    例如:C:\Program Files\MySQL\MySQL Server 8.0\bin。 如果你看到这里
    文件夹 mysqldump.exe 将在那里。 或者你已经设置了上面的文件夹
    在环境变量的 Path 变量中。

  2. 现在,如果你在 CMD 中点击 mysqldump,你可以看到 CMD 能够识别 dump 命令。

  3. 现在运行“mysqldump -h [主机] -P [端口] -u [用户名] -p --skip-triggers --no-create-info --single-transaction --quick --lock-tables= false ABC_databse > c:\xyz.sql"
  4. 上面的命令将提示输入密码,然后开始处理。

To create dump follow below steps:

  1. Open CMD and go to bin folder where you have installed your MySQL
    ex:C:\Program Files\MySQL\MySQL Server 8.0\bin. If you see in this
    folder mysqldump.exe will be there. Or you have setup above folder
    in your Path variable of Environment Variable.

  2. Now if you hit mysqldump in CMD you can see CMD is able to identify dump command.

  3. Now run "mysqldump -h [host] -P [port] -u [username] -p --skip-triggers --no-create-info --single-transaction --quick --lock-tables=false ABC_databse > c:\xyz.sql"
  4. Above command will prompt for password then it will start processing.
美人骨 2024-08-02 05:40:24

例如,使用 -E(--events)-R(--routines) 和默认情况下隐式使用 --triggers ,您可以导出 apple 数据库表的架构和数据及其 事件例程 (过程和函数)触发器backup.sql ,如下所示。 *--single-transaction 保证一致(可靠)转储,不可能仅导出过程或函数和 --skip-triggers 可以排除默认包含在隐式 --triggers 中的触发器和 我的回答解释了如何导出数据库表的架构和数据:

mysqldump -u john -p -E -R --single-transaction apple > backup.sql

或:

mysqldump -u john -p --events --routines --single-transaction apple > backup.sql

或。 *您可以显式使用 --triggers ,如下所示::

                           ↓↓ Here ↓↓
mysqldump -u john -p -E -R --triggers --single-transaction apple > backup.sql

并且,使用 -B(--databases),可以导出 appleorange 数据库的架构和数据及其事件backup.sql 的例程(过程和函数)和触发器,如下所示。 *我的回答解释了如何导出多个数据库:

mysqldump -u john -p -B -E -R --single-transaction apple orange > backup.sql

并且,使用 -A(--all -databases),您可以将所有数据库的架构和数据及其所有事件、例程(过程和函数)和触发器导出到 backup.sql,如下所示。 *我的回答解释了如何导出所有数据库:

mysqldump -u john -p -A -B -E -R --single-transaction > backup.sql

For example, using -E(--events), -R(--routines) and --triggers implicitly used by default, you can export the schema and data of the tables of apple database with its events, routines (procedures and functions) and triggers to backup.sql as shown below. *--single-transaction guarantees consistent(reliable) dump and it is impossible to export only procedures or functions and --skip-triggers can exclude the triggers which are included with implicit --triggers by default and my answer explains how to export the schema and data of the tables of a database:

mysqldump -u john -p -E -R --single-transaction apple > backup.sql

Or:

mysqldump -u john -p --events --routines --single-transaction apple > backup.sql

Or. *You can explicitly use --triggers as shown below::

                           ↓↓ Here ↓↓
mysqldump -u john -p -E -R --triggers --single-transaction apple > backup.sql

And, using -B(--databases), you can export the schema and data of apple and orange databases with their events, routines (procedures and functions) and triggers to backup.sql as shown below. *My answer explains how to export the schema and data of multiple databases:

mysqldump -u john -p -B -E -R --single-transaction apple orange > backup.sql

And, using -A(--all-databases), you can export the schema and data of all databases with all their events, routines (procedures and functions) and triggers to backup.sql as shown below. *My answer explains how to export the schema and data of all databases:

mysqldump -u john -p -A -B -E -R --single-transaction > backup.sql
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文