【发布时间】:2010-11-07 20:53:27
【问题描述】:
如何使用 mysqldump 对 mysql 数据库进行完整备份? 当我进行备份时,我来自指定数据库的表只会得到备份。程序和函数都没有。
这是我正在使用的备份命令:
(操作系统为 Windows Vista。)
mysqldump -u username -p db1 > backup.sql
【问题讨论】:
如何使用 mysqldump 对 mysql 数据库进行完整备份? 当我进行备份时,我来自指定数据库的表只会得到备份。程序和函数都没有。
这是我正在使用的备份命令:
(操作系统为 Windows Vista。)
mysqldump -u username -p db1 > backup.sql
【问题讨论】:
如果您想在不中断任何连接的情况下进行完整备份,即所有数据库、过程、例程和事件:
mysqldump -u [username] -p -A -R -E --triggers --single-transaction > full_backup.sql
-A 适用于所有数据库(也可以使用--all-databases)-R 适用于所有例程(存储过程和触发器)-E所有活动--single-transaction 不锁定表,即不中断任何连接 (R/W)。如果您只想备份指定的数据库:
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 documentation 中或在命令行中运行man mysqldump。
【讨论】:
-e 可能有错字。我用-E。但两者都有,正如您在mysqldump --help 中看到的那样。
$ mysql -u [username] -p [new_database] << database_backup.sql
这取决于您的版本。在 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.
【讨论】:
使用这些命令:-
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
【讨论】:
除了 --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 ...
【讨论】:
FLUSH PRIVILEGES;。
我使用的是 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 服务器中所有数据库的完整备份到以当前日期时间命名的文件。
【讨论】:
使用'-R'来备份存储过程,但也要记住,如果你想在修改数据库时一致地转储你的数据库,你需要使用--single-transaction(如果你只备份innodb)或--lock-all-tables (如果您还需要 myisam 表)
【讨论】:
在 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
用于部署 Dump。
命令:
mysql -u user_name -p database_name < file_name.sql
示例:
mysql -u root -p mr_sbc_clean_new < mr_sbc_clean_dump.sql
【讨论】:
要创建转储,请按照以下步骤操作:
打开 CMD 并转到安装 MySQL 的 bin 文件夹
例如:C:\Program Files\MySQL\MySQL Server 8.0\bin。如果你看到这个
文件夹 mysqldump.exe 将在那里。或者你已经设置了上面的文件夹
在环境变量的路径变量中。
现在,如果您在 CMD 中点击 mysqldump,您可以看到 CMD 能够识别转储命令。
【讨论】: