mysql - 如何在同时使用DDL时使用mysqldump和单事务获取正确的转储?

标签 mysql mysqldump corruption

我是 MySQL 的新手,我正在寻找使用 mysqldump 执行在线热逻辑备份的最佳方法。 This page建议使用此命令行:

mysqldump --single-transaction --flush-logs --master-data=2
          --all-databases > backup_sunday_1_PM.sql

但是......如果你仔细阅读文档you find that :

While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log position), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause the SELECT performed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.

那么,有什么方法可以防止这种可能的转储损坏情况吗? IE。可以暂时阻止这些语句的命令。

PS:关于这个主题的 MySQL 错误条目 http://bugs.mysql.com/bug.php?id=27850

最佳答案

打开 mysql 命令窗口并发出以下命令:

mysql> FLUSH TABLES WITH READ LOCK;

这将锁定此 MySQL 实例上 所有 数据库中的所有 表,直到您发出 UNLOCK TABLES(或终止持有这些表的客户端连接读锁)。

要确认这一点,您可以打开另一个命令窗口并尝试执行 ALTERDROPRENAMETRUNCATE。这些命令挂起,等待读锁被释放。按 Ctrl-C 终止等待。

但是当表有读锁时,您仍然可以执行 mysqldump 备份。

FLUSH TABLES WITH READ LOCK命令可能与使用--lock-all-tables选项相同mysqldump。还不是很清楚,但是this doc似乎支持它:

Another use for UNLOCK TABLES is to release the global read lock acquired with FLUSH TABLES WITH READ LOCK.

FLUSH TABLES WITH READ LOCK--lock-all-tables 都使用短语“global read lock”,所以我认为它们很可能做同样的事情事物。因此,您应该能够将该选项用于 mysqldump 并防止并发的 ALTER、DROP、RENAME 和 TRUNCATE。


回复。您的评论:以下内容来自您链接到的 MySQL 错误日志中的 Guilhem Bichot:

Hi. --lock-all-tables calls FLUSH TABLES WITH READ LOCK. Thus it is expected to block ALTER, DROP, RENAME, or TRUNCATE (unless there is a bug or I'm wrong). However, --lock-all-tables --single-transaction cannot work (mysqldump throws an error message): because lock-all-tables locks all tables of the server against writes for the duration of the backup, whereas single-transaction is intended to let writes happen during the backup (by using a consistent-read SELECT in a transaction), they are incompatible in nature.

由此看来,您似乎无法在备份期间获得并发访问,同时阻止 ALTER、DROP、RENAME 和 TRUNCATE。

关于mysql - 如何在同时使用DDL时使用mysqldump和单事务获取正确的转储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/451404/

相关文章:

MySQL 将时间戳添加到插入的值

php - 备份 .sql 文件为空

不锁定表的 MySQLDump

mysqldump 区分大小写的问题? Win->linux

javascript - Node.Js 使用 sequelize/body 解析器和 mysql 添加记录

php - 如何创建与我的 php 函数执行类似工作的程序(优化加速)

mysqldump 错误 : Got packet bigger than max_allowed_packet'

c# - Visual Studio中损坏的Windows窗体

eclipse - 如何修复/重新创建 Eclipse 工作区和/或 Maven 缓存/存储库?

java - 检查压缩文件是否损坏