mysqldump的几个主要选项探究
1、mysqldump
首先来看下mysqldump的几个主要参数的实际工作方式。
mysqldump 几个主要选项
1. -q
很简单,什么都不做,只是导出时加了一个SQL_NO_CACHE来确保不会读取缓存里的数据。081022 17:39:33 7 Connect root@localhost on
7 Query /*!40100 SET @@SQL_MODE='' */
7 Init DB yejr
7 Query SHOW TABLES LIKE 'yejr'
7 Query LOCK TABLES `yejr` READ /*!32311 LOCAL */
7 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
7 Query show create table `yejr`
7 Query show fields from `yejr`
7 Query show table status like 'yejr'
7 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
7 Query UNLOCK TABLES
7 Quit
7 Query /*!40100 SET @@SQL_MODE='' */
7 Init DB yejr
7 Query SHOW TABLES LIKE 'yejr'
7 Query LOCK TABLES `yejr` READ /*!32311 LOCAL */
7 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
7 Query show create table `yejr`
7 Query show fields from `yejr`
7 Query show table status like 'yejr'
7 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
7 Query UNLOCK TABLES
7 Quit
2. --lock-tables
跟上面类似,不过多加了一个READ LOCAL LOCK,该锁不会阻止读,也不会阻止新的数据插入。
081022 17:36:21 5 Connect root@localhost on
5 Query /*!40100 SET @@SQL_MODE='' */
5 Init DB yejr
5 Query SHOW TABLES LIKE 'yejr'
5 Query LOCK TABLES `yejr` READ /*!32311 LOCAL */
5 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
5 Query show create table `yejr`
5 Query show fields from `yejr`
5 Query show table status like 'yejr'
5 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
5 Query UNLOCK TABLES
5 Quit
3. --lock-all-tables
这个就有点不太一样了,它请求发起一个全局的读锁,会阻止对所有表的写入操作,以此来确保数据的一致性。备份完成后,该会话断开,会自动解锁。
081022 17:36:55 6 Connect root@localhost on
6 Query /*!40100 SET @@SQL_MODE='' */
6 Query FLUSH TABLES
6 Query FLUSH TABLES WITH READ LOCK
6 Init DB yejr
6 Query SHOW TABLES LIKE 'yejr'
6 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
6 Query show create table `yejr`
6 Query show fields from `yejr`
6 Query show table status like 'yejr'
6 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
6 Quit
4. --master-data
除了和刚才的 --lock-all-tables 多了个SHOW MASTER STATUS之外,没有别的变化。
081022 17:59:02 1 Connect root@localhost on
1 Query /*!40100 SET @@SQL_MODE='' */
1 Query FLUSH TABLES
1 Query FLUSH TABLES WITH READ LOCK
1 Query SHOW MASTER STATUS
1 Init DB yejr
1 Query SHOW TABLES LIKE 'yejr'
1 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
1 Query show create table `yejr`
1 Query show fields from `yejr`
1 Query show table status like 'yejr'
1 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
1 Quit
5. --single-transaction
InnoDB 表在备份时,通常启用选项--single-transaction来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:REPEATABLE READ,以确保本次会话(dump)时,不会看到其他会话已经提交了的数据。
081022 17:23:35 1 Connect root@localhost on
1 Query /*!40100 SET @@SQL_MODE='' */
1 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
1 Query BEGIN
1 Query UNLOCK TABLES
1 Init DB yejr
1 Query SHOW TABLES LIKE 'yejr'
1 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
1 Query show create table `yejr`
1 Query show fields from `yejr`
1 Query show table status like 'yejr'
1 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
1 Quit
6. --single-transaction and --master-data
本例中,由于增加了选项--master-data,因此还需要提交一个快速的全局读锁。在这里,可以看到和上面的不同之处在于少了发起BEGIN来显式声明事务的开始。这里采用START TRANSACTION WITH CONSISTENT SNAPSHOT来代替BEGIN的做法的缘故不是太了解,可以看看源代码来分析下。
081022 17:27:07 2 Connect root@localhost on
2 Query /*!40100 SET @@SQL_MODE='' */
2 Query FLUSH TABLES
2 Query FLUSH TABLES WITH READ LOCK
2 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
2 Query START TRANSACTION WITH CONSISTENT SNAPSHOT
2 Query SHOW MASTER STATUS
2 Query UNLOCK TABLES
2 Init DB yejr
2 Query SHOW TABLES LIKE 'yejr'
2 Query SET OPTION SQL_QUOTE_SHOW_CREATE=1
2 Query show create table `yejr`
2 Query show fields from `yejr`
2 Query show table status like 'yejr'
2 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `yejr`
2 Quit
关于隔离级别可以看手册13.2.10.3. InnoDB and TRANSACTION ISOLATION LEVEL,或者本站之前的文章:[InnoDB系列] - 实例解析Innodb的隔离级别以及锁模式。
关于START TRANSACTION WITH CONSISTENT SNAPSHOT的说明可以看下手册描述:
The WITH CONSISTENT SNAPSHOT clause starts a consistent read for storage engines that are capable of it. This applies only to InnoDB. The effect is the same as issuing a START TRANSACTION followed by a SELECT from any InnoDB table. See Section 13.2.10.4, “Consistent Non-Locking Read”. The WITH CONSISTENT SNAPSHOT clause does not change the current transaction isolation level, so it provides a consistent snapshot only if the current isolation level is one that allows consistent read (REPEATABLE READ or SERIALIZABLE).
12.4.1. START TRANSACTION, COMMIT, and ROLLBACK Syntax
原文http://imysql.cn/2008_10_24_deep_into_mysqldump_options
更多相关文章
- 入门 --ubuntu下面mysql数据库安装以及相关操作
- 使用python抓取csdn博客访问量并保存在sqlite3数据库中
- 世界国家 的数据库sql
- 在通过ResultSet反向检索数据时出现NullPointerException
- Oracle数据库导入导出程序
- 在VS2008中使用MySQL数据库
- 将下面语句插入到SQLSERVER数据库中出现“将字符串转换为 unique
- MySQL数据库的基本操作
- 将Excel表格多个Sheet表的数据转换为SQL存储语句