使用mysqldump进行完美备份

问题描述 投票:0回答:9

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

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

mysqldump -u username -p db1 > backup.sql
mysql database backup dump database-dump
9个回答
116
投票

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

mysqldump -u [username] -p -A -R -E --triggers --single-transaction > full_backup.sql
  1. -A
    适用于所有数据库(您也可以使用
    --all-databases
  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
进行记录。


20
投票

这有点取决于您的版本。在 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.

16
投票

使用这些命令:-

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

5
投票

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

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

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

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

3
投票

我使用的是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 服务器中的所有数据库创建一个完美的备份,并保存到以当前日期时间命名的文件中。


2
投票

使用“-R”来备份存储过程,但还要记住,如果您希望在修改数据库时对其进行一致的转储,则需要使用

--single-transaction
(如果您只备份 innodb)或
--lock-all-tables
(如果你还需要 myisam 表)


1
投票

在 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

0
投票

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

  1. 打开CMD并转到安装MySQL的bin文件夹
    例如:C:\Program Files\MySQL\MySQL Server 8.0 中。如果你看到这里
    文件夹 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. 以上命令将提示输入密码,然后开始处理。

0
投票

例如,您可以使用其

events
-E--events)、例程(过程和函数)-R)导出apple数据库表的架构和数据或 --routines)和 triggers(默认情况下隐式使用 --triggers)到
backup.sql
,如下所示。 *--单事务保证一致(可靠)转储,并且不可能仅导出过程或函数,并且--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),您可以将

apple
orange
数据库的架构和数据及其事件、例程(过程和函数)和触发器导出到
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
© www.soinside.com 2019 - 2024. All rights reserved.