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 执行flush tables操作,并加一个全局读锁,很多童鞋可能会好奇,这两个命令貌似是重复的,为什么不在第一次执行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 / 获取当前数据库的快照,这个是由mysqldump中--single-transaction决定的。我们对比下加了该参数和没加的区别,日志如下: 有--single-transaction 参数:
无--single-transaction 参数:
可以看到,当我们不加参数--single-transaction 参数时,将会少了对隔级别设置,少了开启事物一致性快照,少了unlock tables; SHOW MASTER STATUS 这个是由--master-data决定的,记录了开始备份时,binlog的状态信息,包括MASTER_LOG_FILE和MASTER_LOG_POS UNLOCK TABLES 释放锁。 在备份过程中还有一个操作,设置保存点, 其实,这样做不会因为元数据锁阻塞在备份期间对已经备份表的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.* /