dbv mysql_MariaDB與MySQL對比 --- 對分布式事務的支持

本文最初于2016年底發表在我的個人微信公眾號里面,現略有修訂后發布在這里。本文的技術信息針對的是mysql-5.7.x和mariadb-10.1.9。

MariaDB和MySQL兩者對分布式事務的支持有所不同,總的來說MySQL的支持更好,是完備的和可靠的(盡管后來也陸續發現了不少bug),而MariaDB的支持還有諸多問題,先舉例說明。

本文測試用例使用MySQL-5.7.16和MariaDB-10.1.9 進行測試。

MySQL/MariaDB對分布式事務的支持是根據 X/Open CAE document Distributed Transaction Processing: The XA Specification (http://www.opengroup.org/public/pubs/catalog/c193.htm) ,主要包括下面這幾個語句:

xa start 'gtid';

xa end 'gtid';

xa prepare 'gtid';

xa commit 'gtid';

xa rollback 'gtid';

xa recover;

外部的分布式事務管理器(transaction manager, TM) 可以使用這套XA命令來操作mysql 數據庫,按照XA標準的兩階段提交算法(2PC) 完成分布式事務提交。各個語句的意義見官方文檔。

其中mariadb的問題在于 xa prepare 'gtid' 之后,這個事務分支(transaction branch) 可能會丟失。詳見下文。

事先創建1個簡單的表并且插入4行數據:

create table t1(a int primary key);

insert into t1 values(1),(2),(3),(4);

一。兩者的公共行為(相同點)

本節的查詢是要說明,xa prepare之前(無論是xa end 之前還是之后),xa事務的改動對外部不可見,也不持久化,退出連接就會丟失修改。xa事務信息本身也會在退出連接后消失,重新連接后xa recover不會顯示它。

這些行為mysql與mariadb相同,都是正確的。

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> xa start '124';

Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(5);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values(6);

Query OK, 1 row affected (0.00 sec)

mysql> quit;

Bye

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.16-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> xa recover;

Empty set (0.00 sec)

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> xa start '124';

Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(5);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values(6);

Query OK, 1 row affected (0.00 sec)

mysql> xa end '124';

Query OK, 0 rows affected (0.00 sec)

mysql> quit;

Bye

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.7.16-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> xa recover;

Empty set (0.00 sec)

mysql> select*from t1;

ERROR 1046 (3D000): No database selected

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select*from t1;

+------+

| a |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

+------+

4 rows in set (0.00 sec)

二。MySQL的XA PREPARE 的行為

1. 對MySQL來說,在一個事務中執行xa prepare之后退出連接,xa事務不丟失,它的更新也不會丟失。

mysql> create table t1(a int);

Query OK, 0 rows affected (0.05 sec)

mysql> xa start '123';

Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(1),(2);

Query OK, 2 rows affected (0.00 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> insert into t1 values(3),(4);

Query OK, 2 rows affected (0.00 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> xa end '123';

Query OK, 0 rows affected (0.00 sec)

mysql> xa prepare '123';

Query OK, 0 rows affected (0.07 sec)

mysql> quit;

Bye

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.16-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> xa recover;

+----------+--------------+--------------+------+

| formatID | gtrid_length | bqual_length | data |

+----------+--------------+--------------+------+

| 1 | 3 | 0 | 123 |

+----------+--------------+--------------+------+

1 row in set (0.00 sec)

mysql> xa commit '123';

Query OK, 0 rows affected (0.01 sec)

mysql> select*From test.t1;

+------+

| a |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

+------+

4 rows in set (0.00 sec)

2. 對mysql來說,在一個事務中執行xa prepare后,kill掉 mysqld ,xa 事務的改動及其元數據不丟失,mysql binlog系統與innodb做了協調一致的事務恢復,非常完美。重啟mysqld后,客戶端連接上去,執行xa recover可以看到這個prepared事務,執行 xa commit 可以完成該事務的提交。

mysql> xa start '124';

Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(5);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values(6);

Query OK, 1 row affected (0.00 sec)

mysql> xa end '124';

Query OK, 0 rows affected (0.00 sec)

mysql> xa prepare '124';

Query OK, 0 rows affected (0.00 sec)

mysql> quit;

Bye

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.7.16-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> xa recover;

+----------+--------------+--------------+------+

| formatID | gtrid_length | bqual_length | data |

+----------+--------------+--------------+------+

| 1 | 3 | 0 | 124 |

+----------+--------------+--------------+------+

1 row in set (0.00 sec)

mysql> quit

Bye

[1]+ Killed ./bin/mysqld_safe --defaults-file=../../mysql-instances/a/my.cnf

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysqld_safe --defaults-file=../../mysql-instances/a/my.cnf &

[1] 22109

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ 2016-12-02T06:53:49.336023Z mysqld_safe Logging to '/home/david/mysql-instances/a/datadir/david-VirtualBox.err'.

2016-12-02T06:53:49.350561Z mysqld_safe Starting mysqld daemon with databases from /home/david/mysql-instances/a/datadir

david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.16-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> xa recover;

+----------+--------------+--------------+------+

| formatID | gtrid_length | bqual_length | data |

+----------+--------------+--------------+------+

| 1 | 3 | 0 | 124 |

+----------+--------------+--------------+------+

1 row in set (0.00 sec)

mysql> select*from t1;

ERROR 1046 (3D000): No database selected

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select*from t1;

+------+

| a |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

+------+

4 rows in set (0.00 sec)

mysql> xa commit '124';

Query OK, 0 rows affected (0.00 sec)

mysql> select*from t1;

+------+

| a |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

| 6 |

+------+

6 rows in set (0.00 sec)

mysql>

3. mysql之所以能做到上述行為,是因為mysql在xa prepare 做了binlog刷盤,并且允許其后的xa commit被其他事務的binlog隔開。

本例中,新啟動一個xa事務 T1,在xa prepare 之后,另啟動一個連接在其中執行2個普通本地事務,然后再做xa commit T1,可以看到這個xa commit 的binlog與xa prepare被那兩個新事務的binlog隔開了。對MySQL來說這不是問題。

三。MariaDB的XA支持

mariadb沒有把xa start到xa prepare這一塊操作作為一個單獨的binlog事務并且在xa prepare時刻做binlog刷盤,因此xa prepare之后一旦數據庫連接斷開或者mysqld重啟,那么binlog子系統中prepared狀態的事務的binlog就消失了,但是innodb當中這個prepared事務是存在的也就是說binlog與innodb數據不一致了。

1. xa prepare 后連接斷開,那么這個prepared事務就消失了,包括事務修改數據和元數據。binlog上面沒有它的binlog,xa recover也不能列出這個事務。這就錯的更加離譜了,可能是連接斷開后mariadb把innodb中prepared的事務也回滾了。

MariaDB [(none)]> xa start '124';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> insert into t1 values(5);

ERROR 1046 (3D000): No database selected

MariaDB [(none)]> use test;

Database changed

MariaDB [test]> insert into t1 values(5);

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into t1 values(6);

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> xa end '124';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> xa prepare '124';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> xa recover;

+----------+--------------+--------------+------+

| formatID | gtrid_length | bqual_length | data |

+----------+--------------+--------------+------+

| 1 | 3 | 0 | 124 |

+----------+--------------+--------------+------+

1 row in set (0.00 sec)

MariaDB [test]> quit;

Bye

[tdengine@TENCENT64 ~/davie/mysql_installs/tdsql-mariadb-10.1.9-bin-release3/install]$./jmysql.sh 6678

cmd: e

/data/6678/prod/mysql.sock

/data/home/tdengine/davie/mysql_installs/tdsql-mariadb-10.1.9-bin-release3

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.1.9-MariaDBV1.0R030D002-20161123-1511 Source distribution

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> xa recover;

Empty set (0.00 sec)

MariaDB [(none)]> use test;

Database changed

MariaDB [test]> select*from t1;

Empty set (0.00 sec)

MariaDB [test]>

2. xa prepare 后kill掉mysqld,那么這個prepared事務在binlog上面不存在,不過xa recover能列出這個事務,并且它的改動也存在。

在innodb當中,執行了XA PREPARE之后,這個事務已經prepare了,但是它的binlog沒有寫到binlog文件中。mysqld被kill掉重新拉起后,innodb做了事務恢復,所以可以看到它之前的改動;但是binlog上面卻沒有這個事務。不過做xa commit 卻又可以提交這個事務。之后,這個事務的改動就可見了。但是,innodb中的數據與binlog已經不一致了。備機上面沒有事務內容

MariaDB [test]> xa start '125';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> insert into t1 values(9);

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into t1 values(10);

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> xa end '125';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> xa prepare '125';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> quit

Bye

[tdengine@TENCENT64 ~/davie/mysql_installs/tdsql-mariadb-10.1.9-bin-release3/install]$./jmysql.sh 6678

cmd: e

/data/6678/prod/mysql.sock

/data/home/tdengine/davie/mysql_installs/tdsql-mariadb-10.1.9-bin-release3

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 3

Server version: 10.1.9-MariaDBV1.0R030D002-20161123-1511 Source distribution

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> xa recover;

+----------+--------------+--------------+------+

| formatID | gtrid_length | bqual_length | data |

+----------+--------------+--------------+------+

| 1 | 3 | 0 | 125 |

+----------+--------------+--------------+------+

1 row in set (0.00 sec)

MariaDB [(none)]> select*from t1;

ERROR 1046 (3D000): No database selected

MariaDB [(none)]> use test;

Database changed

MariaDB [test]> select*from t1;

+---+

| a |

+---+

| 5 |

| 6 |

| 7 |

| 8 |

+---+

4 rows in set (0.01 sec)

MariaDB [test]> xa commit '125';

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> select*from t1;

+----+

| a |

+----+

| 5 |

| 6 |

| 7 |

| 8 |

| 9 |

| 10 |

+----+

6 rows in set (0.00 sec)

MariaDB [test]>

從最初的測例開始,一直沒有任何插入數據行的binlog(write_row)寫入。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/533247.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/533247.shtml
英文地址,請注明出處:http://en.pswp.cn/news/533247.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

centos7下載安裝mysql步驟_Linux-centos7安裝mysql步驟

Centos7.3 yum安裝MySQL5.7.25擴展:在CentOS中默認安裝有MariaDB,這個是MySQL的分支,但為了需要,還是要在系統中安裝MySQL,而且安裝完成之后可以直接覆蓋掉MariaDB。1 下載并安裝MySQL官方的 Yum Repository[rootlocal…

mysql 常用命令的使用_MySQL基本命令

基操操作命令創建數據庫CREATE DATABASE 數據庫名;指定要操作的數據庫USE 數據庫名;創建數據表CREATE TABLE 數據表名;查看數據表SHOW CREATE TABLE 數據表名;使用DESCRIBE語句查看數據表DESCRIBE 數據表名;為數據表重…

織夢數據庫支持mysql5.7_最新織夢DEDECMS5.7數據庫說明文檔

最新織夢DEDECMS5.7數據庫說明文檔:dede_arctype 欄目管理表ID int(10) 欄目編號(自動編號)reID int(10) 父欄目編號topID int(10)sortrank smallint(6) 排序編號typename varchar(30) 欄目名稱typedir varchar(100) 欄目目錄isdefault smallint(6) 欄目列表選項(1鏈…

mysql ddl dql_MySQL的DDL和DML及其DQL數據庫操作

數據庫的基本概念1. 數據庫的英文單詞: DataBase 簡稱 : DB2. 什么數據庫?* 用于存儲和管理數據的倉庫。3. 數據庫的特點:1. 持久化存儲數據的。其實數據庫就是一個文件系統2. 方便存儲和管理數據3. 使用了統一的方式操作數據庫 -…

python模糊圖像清晰化_視頻模糊圖像處理

隨著科學技術的不斷發展和進步以及人們的安防意識不斷加強,人們對于安防技術的要求越來越高。電子監控在許多領域中都得到了廣泛的應用,如交通監控、軍事偵查、公共場所安全防范等。清晰的圖像能夠準確地鎖定犯罪證據和犯罪嫌疑人,能夠清晰地…

mysql分頁 disti_MySql查詢性能優化

慢查詢判定1.開啟慢查詢日志記錄執行時間超過long_query_time 秒的sql語句2.通過show processlist命令查看線程執行狀態3.通過explain解析sql了解執行狀態慢查詢優化是否向服務器請求列不必要的數據查詢不需要的記錄(limit),多表關聯返回全部列,總是取出…

java atlas mysql_使用Atlas實現MySQL讀寫分離+MySQL-(Master-Slave)配置

參考博文:MySQL-(Master-Slave)配置 本人按照博友北在北方的配置已成功 我使用的是 mysql5.6.27版本。配置中 又進一步對mysql5.6的日志進行了了解 :mysql日志詳細解析1.安裝注意:只能安裝在64位的Linux操作系統上,CentOS官方建…

mysql dts_云樹·DTS - 產品系列 - 分布式數據庫系統_MySQL數據庫性能優化-愛可生...

災備復制實現本地數據中心MySQL數據庫高效復制及異地數據中心MySQL數據庫容災轉移,從而確保在主數據中心故障或災難時,備用數據中心數據的最大完整性。該服務通過對MySQL二進制日志進行解析、過濾、合并、壓縮、并行回放等技術,準實時的在主備…

python利用matplotlib做餅圖_python利用matplotlib庫繪制餅圖的方法示例

介紹matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地進行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應用程序中。它的文檔相當完備,并且 Gallery頁面 中有上百幅縮略圖&#xff0…

react同步請求_React中setState同步更新策略

setState 同步更新我們在上文中提及,為了提高性能React將setState設置為批次更新,即是異步操作函數,并不能以順序控制流的方式設置某些事件,我們也不能依賴于this.state來計算未來狀態。典型的譬如我們希望在從服務端抓取數據并且…

DVWA設置mysql_dvwa安裝、配置、使用教程(Linux)

一、搭建LAMP環境二、安裝DVWA2.1 下載dvwa2.2 解壓安裝將下載的應用解壓到apache默認的主目錄/var/www/html:unzip DVWA-master.zip -d /usr/www/html2.3 啟用功能dvwa上的漏洞,需要些刻意的配置才能被利用。訪問:http://172.0.0.1/dvwa如下…

eclipse mysql jndi_Java開發網 - tomcat5配置jndi的問題 (jdbc:comp is not bound in this Context)...

Posted by:returnerPosted on:2004-11-09 22:42tomcat5配置jndi的問題;斑竹高手們來看看啊,情況緊急!!!!!!!!10萬分感謝這個問題我是搜索出來的,我也遇到了同…

java正則效率_善用Pattern提高你的應用處理正則表達式的效率(Java)

舉個簡單了例子,在一個需要用于注冊登錄的b/s模式的應用中,在瀏覽器驗證用戶注冊表單的合法性是必須的,但你為了防止hacker,在服務器再驗證一次肯定也是必須的。題目:在服務器端驗證郵箱是否合法:通常你可能…

java jwindow 鍵盤_各位老哥求救,JWINDOW無法接收到鍵盤監聽

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓下面po代碼啊。這是我的類。class DragWindow extends JWindow{int positionX;int positionY;public DragWindow() {try {jbInit();}catch(Exception e) {e.printStackTrace();}}private void jbInit() throws Exception {this.add…

java jdbc rowset_JAVA基礎知識之JDBC——RowSet

RowSet概念在C#中,提供了一個DataSet,可以把數據庫的數據放在內存中進行離線操作(讀寫),操作完成之后再同步到數據庫中去,Java中則提供了類似的功能RowSet.RowSet接口繼承自ResultSet接口。與ResultSet相比,RowSet默認…

java廚房_JAVA環境搭建,廚房安裝圖文教程!

在“系統變量”欄下執行三項操作:①新建“Java_Home”,設置其值為 JDK所在的絕對路徑,如果你的事剛才的默認路徑,那值為:C:Program FilesJavajdk1.7.0_02②新建“Classpath”(如果已有,則直接編輯)&#xf…

java post get 請求_java get post 請求

packagewzh.Http;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.URL;importjava.net.URLConnection;importjava.util.List;importjava.util.Map;public classHttpRequest {/*** 向指定UR…

用java做日記本系統_jsp+servlet開發java web個人日記本系統

項目描述Jsp_Servlet技術使用個人日記本系統,主要有日記分類,添加日記,刪除日記和一些個人資料的修改。運行環境jdk8tomcat7mysql5.6IntelliJ IDEA(eclipse)項目技術(必填)Jsp Servletbootstrapjqueryckeditor數據庫文件(可選)鏈接&#xff1…

java類加載過程_java類的加載過程

在這本書里面,在講到類初始化的五種情況時,提及了一個比較有趣的事情。先來看看下面的代碼public class SubClass {static{System.err.println("I m your son");}public static final int name 111;}這個時候如果調用SubClass.name&#xff0…

java mvc 導出excel_Java springMVC POI 導出 EXCEL

思路 :將需要導出的數據存放在一個List中創建一個EXCEL表 注意 XSSFWorkbook 只能操作2007以上的版本,XSSFWorkbook 只能操作2003一下的版本,所以需要的時候可以使用 Workbook創建對象處理兼容性遍歷List 并將每條數據 寫入 EXCEL表中具體代碼…