--lock-tables
Lock all tables for read.
(Defaults to on; use --skip-lock-tables to disable.)
14777 Query /*!40100 SET @@SQL_MODE='' */
14777 Init DB db1
14777 Query SHOW CREATE DATABASE IF NOT EXISTS `db1`
14777 Query show tables
14777 Query LOCK TABLES `t` READ /*!32311 LOCAL */
14777 Query show table status like 't'
14777 Query SET SQL_QUOTE_SHOW_CREATE=1
14777 Query SET SESSION character_set_results = 'binary'
14777 Query show create table `t`
14777 Query SET SESSION character_set_results = 'utf8'
14777 Query show fields from `t`
14777 Query show fields from `t`
14777 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t`
14777 Query SET SESSION character_set_results = 'binary'
14777 Query use `db1`
14777 Query select @@collation_database
14777 Query SHOW TRIGGERS LIKE 't'
14777 Query SET SESSION character_set_results = 'utf8'
14777 Query UNLOCK TABLES
14777 Quit
--lock-all-tables
发起一个全局的读锁,阻止对所有表的写入操作,以此来确保数据的一致性。备份完成后,该会话断开,会自动解锁。
Locks all tables across all databases. This is achieved
by taking a global read lock for the duration of the
whole dump. Automatically turns --single-transaction and
–lock-tables off.
14778 Query /*!40100 SET @@SQL_MODE='' */
14778 Query /*!40103 SET TIME_ZONE='+00:00' */
14778 Query FLUSH TABLES
14778 Query FLUSH TABLES WITH READ LOCK
14778 Query SHOW VARIABLES LIKE 'gtid\_mode'
14778 Query SELECT @@GLOBAL.GTID_EXECUTED
14778 Init DB db1
14778 Query SHOW TABLES LIKE 't'
14778 Query show table status like 't'
14778 Query SET SQL_QUOTE_SHOW_CREATE=1
14778 Query SET SESSION character_set_results = 'binary'
14778 Query show create table `t`
14778 Query SET SESSION character_set_results = 'utf8'
14778 Query show fields from `t`
14778 Query show fields from `t`
14778 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t`
14778 Query SET SESSION character_set_results = 'binary'
14778 Query use `db1`
14778 Query select @@collation_database
14778 Query SHOW TRIGGERS LIKE 't'
14778 Query SET SESSION character_set_results = 'utf8'
14778 Quit
--master-data=2
与选项--lock-all-tables
的输出相比,除了多一个SHOW MASTER STATUS
语句之外,没有别的变化。
该选项会将二进制日志文件名和位置追加到备份文件中。若
--master-data=1
,将以CHANGE MASTER
命令的形式输出,若--master-data=2
,该命令将会被注释掉。--master-data
选项将导致--lock-all-tables
选项默认打开,除非也指定了选项--single-transaction
,无论在哪种情况下,备份时肯定会刷新日志文件。选项--lock-tables
自动关闭。
14781 Query /*!40100 SET @@SQL_MODE='' */
14781 Query /*!40103 SET TIME_ZONE='+00:00' */
14781 Query FLUSH /*!40101 LOCAL */ TABLES
14781 Query FLUSH TABLES WITH READ LOCK
14781 Query SHOW VARIABLES LIKE 'gtid\_mode'
14781 Query SELECT @@GLOBAL.GTID_EXECUTED
14781 Query SHOW MASTER STATUS
14781 Init DB db1
14781 Query SHOW TABLES LIKE 't'
14781 Query show table status like 't'
14781 Query SET SQL_QUOTE_SHOW_CREATE=1
14781 Query SET SESSION character_set_results = 'binary'
14781 Query show create table `t`
14781 Query SET SESSION character_set_results = 'utf8'
14781 Query show fields from `t`
14781 Query show fields from `t`
14781 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t`
14781 Query SET SESSION character_set_results = 'binary'
14781 Query use `db1`
14781 Query select @@collation_database
14781 Query SHOW TRIGGERS LIKE 't'
14781 Query SET SESSION character_set_results = 'utf8'
14781 Quit
--single-transaction
InnoDB
表在备份时,通常启用选项 --single-transaction
来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:REPEATABLE READ
,以确保本次会话(dump
)时,不会看到其他会话已经提交了的数据。
在一个单独的事务中通过创建事务一致性快照来备份所有表,只对支持多版本读的存储引擎的表有效(当前仅
InnoDB
支持);此备份不能够保证其他存储引擎表中数据的一致性,当备份正在运行时,需确保备份是有效的(正确的表数据和日志位置),且没有其他连接使用以下语句:ALTER TABLE, DROP TABLE, RENAME TABLE,TRUNCATE TABLE
,因为一致性快照与此相互关联。启动该选项时自动关闭--lock-tables
选项
14782 Query /*!40100 SET @@SQL_MODE='' */
14782 Query /*!40103 SET TIME_ZONE='+00:00' */
14782 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
14782 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
14782 Query SHOW VARIABLES LIKE 'gtid\_mode'
14782 Query SELECT @@GLOBAL.GTID_EXECUTED
14782 Query UNLOCK TABLES
14782 Init DB db1
14782 Query SHOW TABLES LIKE 't'
14782 Query SAVEPOINT sp
14782 Query show table status like 't'
14782 Query SET SQL_QUOTE_SHOW_CREATE=1
14782 Query SET SESSION character_set_results = 'binary'
14782 Query show create table `t`
14782 Query SET SESSION character_set_results = 'utf8'
14782 Query show fields from `t`
14782 Query show fields from `t`
14782 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t`
14782 Query SET SESSION character_set_results = 'binary'
14782 Query use `db1`
14782 Query select @@collation_database
14782 Query SHOW TRIGGERS LIKE 't'
14782 Query SET SESSION character_set_results = 'utf8'
14782 Query ROLLBACK TO SAVEPOINT sp
14782 Query RELEASE SAVEPOINT sp
14782 Quit
--single-transaction
and --master-data=2
如果只使用选项 --single-transaction
开启一个快照事务来进行数据备份,其他连接在执行大事务的情况下,一致性快照事务获取的状态可能不一致。通过开启选项 --master-data=2
,执行 flush tables; flush table with read lock
来保证当前实例处于相对静止的无大事务状态。
14783 Connect root@localhost on using Socket
14783 Query /*!40100 SET @@SQL_MODE='' */
14783 Query /*!40103 SET TIME_ZONE='+00:00' */
14783 Query FLUSH /*!40101 LOCAL */ TABLES
14783 Query FLUSH TABLES WITH READ LOCK
14783 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
14783 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
14783 Query SHOW VARIABLES LIKE 'gtid\_mode'
14783 Query SELECT @@GLOBAL.GTID_EXECUTED
14783 Query SHOW MASTER STATUS
14783 Query UNLOCK TABLES
14783 Init DB db1
14783 Query SHOW TABLES LIKE 't'
14783 Query SAVEPOINT sp
14783 Query show table status like 't'
14783 Query SET SQL_QUOTE_SHOW_CREATE=1
14783 Query SET SESSION character_set_results = 'binary'
14783 Query show create table `t`
14783 Query SET SESSION character_set_results = 'utf8'
14783 Query show fields from `t`
14783 Query show fields from `t`
14783 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t`
14783 Query SET SESSION character_set_results = 'binary'
14783 Query use `db1`
14783 Query select @@collation_database
14783 Query SHOW TRIGGERS LIKE 't'
14783 Query SET SESSION character_set_results = 'utf8'
14783 Query ROLLBACK TO SAVEPOINT sp
14783 Query RELEASE SAVEPOINT sp
14783 Quit
/*!40100 SET @@SQL_MODE='' */
/*!40103 SET TIME_ZONE='+00:00' */
FLUSH /*!40101 LOCAL */ TABLES
上述代码中/*!40100 xxx*/
的含义:当 mysql 的版本号大于等于 40100 时,该参数可加上或该语句可执行
测试
当前 mysql
版本号
mysql> select @@version;
+------------+
| @@version |
+------------+
| 5.7.23-log |
+------------+
带有注释的语句
/*!50722 select 1*/
>/*!50722 select 1*/;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)
/*!50723 select 1*/
mysql> /*!50723 select 1*/;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)
/*!50724 select 1*/
>/*!50724 select 1*/;
Query OK, 0 rows affected (0.00 sec)
flush tables
会产生一个事务, 而 flush local tables
并不会产生一个事务
mysqldump 的几个主要选项探究