部分生產環境採用mysqldump --single-transaction的方式在夜間進行資料庫備份,而同事恰好在備份期間執行了alter table操作,操作部分成功部分失敗,為啥呢?
測試在MySQL 5.6.36上執行,問題有版本差異!
##======================================= =================================
##在mysqldump對single-transaction參數的解釋為:
Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. 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, as consistent snapshot is not isolated from them. Option automatically turns off --lock-tables.
紅色字體部分是重點,但是看得有些迷糊,還是動手測試下。
根據《mysqldump的幾個主要選項探究》的介紹,我們備份執行的命令mysqldump --single-transaction --master-data相當於執行下面程式碼:
FLUSH TABLES; FLUSH TABLES WITH READ LOCK;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; START TRANSACTION WITH CONSISTENT SNAPSHOT; SHOW MASTER STATUS; UNLOCK TABLES; SHOW TABLES LIKE 'xxx'SET OPTION SQL_QUOTE_SHOW_CREATE=1SHWO CREATE TABLE 'xxx'SHOW FIELDS FROM 'xxx'SHOW TABLE STATUS LIKE 'xxx'SELECT /*!40001 SQL_NO_CACHE */ * FROM xxx QUIT
場景1:mysqldump開始但尚未備份到表格tb001時,另外回話對錶tb001進行alter操作,然後mysqldump對錶tb001進行導出
#alter操作順利完成,但是mysqldump操作失敗。
場景2:mysqldump開始備份並完成tb001的匯出,在對其他表進行匯出過程中,其他回話對表進行alter操作
#alter table操作被阻塞直至mysqldump完成或失敗後退出。
使用mysqldump備份時,模擬場景2的環境,報錯資訊為:
mysqldump: Error 1412: Table definition has changed, please retry transaction when dumping table ` tb1002` at row: 0
查看匯出文件,最後內容為:
-- -- Dumping data for table `tb1002`--LOCK TABLES `tb1002` WRITE;/*!40000 ALTER TABLE `tb1002` DISABLE KEYS */;
總結:
以上是關於mysqldump的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!