--single-transaction 的含義是,在開(kāi)始 dump 前,設(shè)置隔離級(jí)別為 RR ,并且 start transaction. 通過(guò)將導(dǎo)出操作封裝在一個(gè)事務(wù) (Repeatable Read) 內(nèi)來(lái)使得導(dǎo)出的數(shù)據(jù)是一個(gè)一致性快照。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的蓬溪網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
測(cè)試數(shù)據(jù)庫(kù)版本:
MySQL> select version();
+------------+
| version() |
+------------+
| 5.7.26-log |
+------------+
1 row in set (0.00 sec)
測(cè)試 1 :
sesseion A |
session B |
mysql> set tx_isolation='repeatable-read'; Query OK, 0 rows affected (0.00 sec)
|
mysql> set tx_isolation='repeatable-read'; Query OK, 0 rows affected (0.00 sec)
|
mysql> begin; Query OK, 0 rows affected (0.01 sec) |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec)
mysql> insert into test01 select 4,4; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.00 sec) |
|
mysql> insert into test01 select 5,5; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.00 sec) |
可以看到, RR 隔離級(jí)別下的一致性讀,不是以事務(wù) begin 的時(shí)間點(diǎn)建立 snapshot 的。單純 begin 后,到第一次讀取前之間其他會(huì)話(huà)的事務(wù)還是可以讀取的,但是在第一次讀取數(shù)據(jù)完成后,讀取到的事務(wù)就不再變化了。
測(cè)試 2 :
session A |
session B |
mysql> set tx_isolation='repeatable-read'; |
mysql> set tx_isolation='repeatable-read'; |
mysql> select * from t1; Empty set (0.00 sec) |
|
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +----+------+ 5 rows in set (0.00 sec) |
|
mysql> insert into test01 select 6,6; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +----+------+ 5 rows in set (0.00 sec) |
RR 隔離級(jí)別下的一致性讀,發(fā)生在事務(wù)里面第一次 select 的時(shí)候。我試了下用 delete 語(yǔ)句替換 session A 的 select 語(yǔ)句,結(jié)果發(fā)現(xiàn)還是會(huì)讀取到 session B 的事務(wù)。
session A |
session B |
mysql> set tx_isolation='repeatable-read'; |
mysql> set tx_isolation='repeatable-read'; mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec) |
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec) |
|
mysql> insert into test01 select 4,4; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.00 sec) |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec) |
|
mysql> update test01 set c2=5 where c1=4; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 5 | +----+------+ 4 rows in set (0.00 sec) |
Session A 第一次 select 建立一致性讀后, session B 插入數(shù)據(jù), session A 的 select 仍然讀不到,但是 update 因?yàn)槭钱?dāng)前讀,所以更新到 session B 插入的數(shù)據(jù)。
所以 begin 和 start transaction 是事務(wù)開(kāi)始的標(biāo)志,但不是事務(wù)開(kāi)始的起點(diǎn)。如果要將 start transaction 作為事務(wù)開(kāi)始的時(shí)間點(diǎn),那么必須使用:
START TRANSACTION WITH consistent snapshot ###mysqldump 中的快照就是用這個(gè)實(shí)現(xiàn)的
mysql> set tx_isolation='repeatable-read'; |
mysql> set tx_isolation='repeatable-read'; |
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec) |
|
mysql> start transaction with consistent snapshot; Query OK, 0 rows affected (0.00 sec) |
|
mysql> insert into test01 select 4,4; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 |
|
mysql> select * from test01; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec) |
發(fā)起備份
mysqldump -uroot -poracle --single-transaction --master-data=1 -R -E --triggers -B ming --ignore_table=ming.test02 > /tmp/ming_st.sql
查看備份過(guò)程的 general log :
[root@oradb-2062 binlog]# more /u01/mysql/3306/data/oradb-2062.log /u01/mysql_57/bin/mysqld, Version: 5.7.26-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /u01/mysql/3306/data/mysqld.sock Time Id Command Argument 2019-08-01T08:30:50.718358Z 12 Query show variables like 'log_output' 2019-08-01T08:31:33.211254Z 14 Connect root@localhost on using Socket 2019-08-01T08:31:33.211413Z 14 Query /*!40100 SET @@SQL_MODE='' */ 2019-08-01T08:31:33.211474Z 14 Query /*!40103 SET TIME_ZONE='+00:00' */ 2019-08-01T08:31:33.211565Z 14 Query FLUSH /*!40101 LOCAL */ TABLES 2019-08-01T08:31:33.212009Z 14 Query FLUSH TABLES WITH READ LOCK 2019-08-01T08:31:33.212047Z 14 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 2019-08-01T08:31:33.212070Z 14 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */ 2019-08-01T08:31:33.212115Z 14 Query SHOW VARIABLES LIKE 'gtid\_mode' 2019-08-01T08:31:33.216296Z 14 Query SHOW MASTER STATUS 2019-08-01T08:31:33.216472Z 14 Query UNLOCK TABLES 2019-08-01T08:31:33.219582Z 14 Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG ' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AN D TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('ming'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, I NITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME 2019-08-01T08:31:33.223184Z 14 Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE _TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('ming')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME 2019-08-01T08:31:33.223727Z 14 Query SHOW VARIABLES LIKE 'ndbinfo\_version' 2019-08-01T08:31:33.225502Z 14 Init DB ming 2019-08-01T08:31:33.225545Z 14 Query SHOW CREATE DATABASE IF NOT EXISTS `ming` 2019-08-01T08:31:33.225683Z 14 Query SAVEPOINT sp 2019-08-01T08:31:33.225750Z 14 Query show tables 2019-08-01T08:31:33.225957Z 14 Query show table status like 'mytest01' 2019-08-01T08:31:33.226083Z 14 Query SET SQL_QUOTE_SHOW_CREATE=1 2019-08-01T08:31:33.226114Z 14 Query SET SESSION character_set_results = 'binary' 2019-08-01T08:31:33.226145Z 14 Query show create table `mytest01` 2019-08-01T08:31:33.226190Z 14 Query SET SESSION character_set_results = 'utf8' 2019-08-01T08:31:33.226226Z 14 Query show fields from `mytest01` 2019-08-01T08:31:33.226468Z 14 Query show fields from `mytest01` 2019-08-01T08:31:33.226687Z 14 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `mytest01` 2019-08-01T08:31:33.226810Z 14 Query SET SESSION character_set_results = 'binary' 2019-08-01T08:31:33.226844Z 14 Query use `ming` 2019-08-01T08:31:33.226877Z 14 Query select @@collation_database 2019-08-01T08:31:33.226920Z 14 Query SHOW TRIGGERS LIKE 'mytest01' 2019-08-01T08:31:33.227098Z 14 Query SET SESSION character_set_results = 'utf8' 2019-08-01T08:31:33.227130Z 14 Query ROLLBACK TO SAVEPOINT sp 2019-08-01T08:31:33.227162Z 14 Query show table status like 'test01' 2019-08-01T08:31:33.227262Z 14 Query SET SQL_QUOTE_SHOW_CREATE=1 2019-08-01T08:31:33.227289Z 14 Query SET SESSION character_set_results = 'binary' 2019-08-01T08:31:33.227316Z 14 Query show create table `test01` 2019-08-01T08:31:33.227356Z 14 Query SET SESSION character_set_results = 'utf8' 2019-08-01T08:31:33.227389Z 14 Query show fields from `test01` 2019-08-01T08:31:33.227730Z 14 Query show fields from `test01` 2019-08-01T08:31:33.227911Z 14 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test01` 2019-08-01T08:31:33.228005Z 14 Query SET SESSION character_set_results = 'binary' 2019-08-01T08:31:33.228053Z 14 Query use `ming` 2019-08-01T08:31:33.228084Z 14 Query select @@collation_database 2019-08-01T08:31:33.228143Z 14 Query SHOW TRIGGERS LIKE 'test01' 2019-08-01T08:31:33.228336Z 14 Query SET SESSION character_set_results = 'utf8' 2019-08-01T08:31:33.228369Z 14 Query ROLLBACK TO SAVEPOINT sp 2019-08-01T08:31:33.228399Z 14 Query show table status like 'test03' 2019-08-01T08:31:33.228501Z 14 Query SET SQL_QUOTE_SHOW_CREATE=1 2019-08-01T08:31:33.228686Z 14 Query SET SESSION character_set_results = 'binary' 2019-08-01T08:31:33.228726Z 14 Query show create table `test03` 。。。。。。 |
可以看到, mysqldump 的大致實(shí)現(xiàn)過(guò)程是:連接 -> 初始化信息 -> 刷新表(鎖表) -> 開(kāi)啟事務(wù)(一致性快照) -> 記錄偏移量 -> 解鎖表
參考:https://yq.aliyun.com/articles/552972?spm=a2c4e.11153940.0.0.18a12415csEBjM
分享題目:mysqldump--single-transaction一致性的研究
文章網(wǎng)址:http://m.rwnh.cn/article42/jcgjhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、微信公眾號(hào)、外貿(mào)建站、網(wǎng)站收錄、電子商務(wù)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)