FLUSH /!40101 LOCAL / TABLES Closes all open tables, forces all tables in use to be closed, and flushes the query cache. FLUSH TABLES WITH READ LOCK 執(zhí)行flush tables操作,并加一個全局讀鎖,很多童鞋可能會好奇,這兩個命令貌似是重復的,為什么不在第一次執(zhí)行flush tables操作的時候加上鎖呢? 下面看看源碼中的解釋: / We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES will wait but will not stall the whole mysqld, and when the long update is done the FLUSH TABLES WITH READ LOCK will start and succeed quickly. So, FLUSH TABLES is to lower the probability of a stage where both mysqldump and most client connections are stalled. Of course, if a second long update starts between the two FLUSHes, we have that bad stall. / 簡而言之,是為了避免較長的事務操作造成FLUSH TABLES WITH READ LOCK操作遲遲得不到鎖,但同時又阻塞了其它客戶端操作。 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 設置當前會話的事務隔離等級為RR,RR可避免不可重復讀和幻讀。 START TRANSACTION /!40100 WITH CONSISTENT SNAPSHOT / 獲取當前數(shù)據(jù)庫的快照,這個是由mysqldump中--single-transaction決定的。我們對比下加了該參數(shù)和沒加的區(qū)別,日志如下: 有--single-transaction 參數(shù):
無--single-transaction 參數(shù):
可以看到,當我們不加參數(shù)--single-transaction 參數(shù)時,將會少了對隔級別設置,少了開啟事物一致性快照,少了unlock tables; SHOW MASTER STATUS 這個是由--master-data決定的,記錄了開始備份時,binlog的狀態(tài)信息,包括MASTER_LOG_FILE和MASTER_LOG_POS UNLOCK TABLES 釋放鎖。 在備份過程中還有一個操作,設置保存點, 其實,這樣做不會因為元數(shù)據(jù)鎖阻塞在備份期間對已經(jīng)備份表的ddl操作。 / ROLLBACK TO SAVEPOINT in --single-transaction mode to release metadata lock on table which was already dumped. This allows to avoid blocking concurrent DDL on this table without sacrificing correctness, as we won't access table second time and dumps created by --single-transaction mode have validity point at the start of transaction anyway. Note that this doesn't make --single-transaction mode with concurrent DDL safe in general case. It just improves situation for people for whom it might be working.* /