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.mzph.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表中具体代码…