我是 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 theSELECT
performed bymysqldump
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
(或终止持有这些表的客户端连接读锁)。
要确认这一点,您可以打开另一个命令窗口并尝试执行 ALTER
、DROP
、RENAME
或 TRUNCATE
。这些命令挂起,等待读锁被释放。按 Ctrl-C 终止等待。
但是当表有读锁时,您仍然可以执行 mysqldump
备份。
FLUSH TABLES WITH READ LOCK
命令可能与使用的
。还不是很清楚,但是this doc似乎支持它:--lock-all-tables
选项相同mysqldump
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/