mysql xa crash_分布式事务:MySQL中的XA事务崩溃了如何恢复?

在测试MySQL XA事务时,服务器强制断电导致数据库报错。重启后,通过查看日志发现3个未提交或回滚的XA事务。通过执行`xa recover`和`xa rollback`命令成功恢复了这些事务。本文介绍了MySQL XA事务的恢复过程及其限制。
摘要由CSDN通过智能技术生成

前段时间搭建了一套MySQL分布式数据库集群,数据库节点有12个,用来测试各种分布式事务方案的性能和优缺点。测试MySQL XA事务时,正当测试脚本向数据库中批量插入数据时,强制服务器断电!注意:是直接拔电源,使其瞬间断电,再次重启服务器后,MySQL数据库报错了。特此记录MySQL XA事务的恢复。

MySQL XA事务问题

服务器强制断电后重启,此时MySQL报错,查看MySQL启动日志时,发现如下所示的错误信息。

InnoDB: The log sequence number in ibdata files does not match

InnoDB: the log sequence number in the ib_logfiles!

100224 23:24:20 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Restoring possible half-written data pages from the doublewrite

InnoDB: buffer...

InnoDB: Transaction 0 4497755 was in the XA prepared state.

InnoDB: Transaction 0 4468551 was in the XA prepared state.

InnoDB: Transaction 0 4468140 was in the XA prepared state.

InnoDB: 3 transaction(s) which must be rolled back or cleaned up

InnoDB: in total 0 row operations to undo

InnoDB: Trx id counter is 0 5312768

InnoDB: Starting in background the rollback of uncommitted transactions

100224 23:24:20 InnoDB: Rollback of non-prepared transactions completed

100224 23:24:20 InnoDB: Started; log sequence number 0 3805002509

100224 23:24:20 InnoDB: Starting recovery for XA transactions...

100224 23:24:20 InnoDB: Transaction 0 4497755 in prepared state after recovery

100224 23:24:20 InnoDB: Transaction contains changes to 8 rows

100224 23:24:20 InnoDB: Transaction 0 4468551 in prepared state after recovery

100224 23:24:20 InnoDB: Transaction contains changes to 1 rows

100224 23:24:20 InnoDB: Transaction 0 4468140 in prepared state after recovery

100224 23:24:20 InnoDB: Transaction contains changes to 1 rows

100224 23:24:20 InnoDB: 3 transactions in prepared state after recovery

100224 23:24:20 [Note] Found 3 prepared transaction(s) in InnoDB

100224 23:24:20 [Warning] Found 3 prepared XA transactions

100224 23:24:20 [Note] Event Scheduler: Loaded 0 events

100224 23:24:20 [Note] /opt/mysql/bin/mysqld: ready for connections.

Version: '8.0.18' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)

从上面的日志信息中,可以看出有三个XA的事务没有提交或回滚。那该如何恢复MySQL的XA事务呢?

恢复MySQL XA事务

首先,登录到MySQL,执行如下命令。

mysql> xa recover;

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

| formatID | gtrid_length | bqual_length | data |

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

| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f06397f000001:bae5:4b6928eb:f0650 |

| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:fb5c37f000001:bae5:4b6928eb:fb5cd |

| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f03ea7f000001:bae5:4b6928eb:f0400 |

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

数据表示信息如下:

formatIDis the formatIDpart of the transaction xid

gtrid_lengthis the length in bytes of the gtridpart of the xid

bqual_lengthis the length in bytes of the bqualpart of the xid

datais the concatenation of the gtridand bqualparts of the xid

这是三个XA事务的信息,准备直接回滚。

mysql> xa rollback '1-7f000001:bae5:4b6928eb:fb5c3','7f000001:bae5:4b6928eb:fb5cd',131075;

Query OK, 0 rows affected (0.41 sec)

MySQL XA事务补充

XA事务支持限于InnoDB存储引擎。

MySQL  XA实施是针对外部XA的,其中,MySQL服务器作为资源管理器,而客户端程序作为事务管理器。未实施“内部XA”。这样,就允许MySQL服务器内的单独存储引擎作为RM(资源管理器),而服务器本身作为TM(事务管理器)。处理包含1个以上存储引擎的XA事务时,需要内部XA。内部XA的实施是不完整的,这是因为,它要求存储引擎在表处理程序层面上支持两阶段提交,目前仅对InnoDB实现了该特性。

对于XA START,不支持JOIN和RESUME子句。

对于XA END,不支持SUSPEND [FOR MIGRATE]子句。

在全局事务内,对于每个XA事务,xid值的bqual部分应是不同的,该要求是对当前MySQL XA实施的限制。它不是XA规范的组成部分。

如果XA事务达到PREPARED状态而且MySQL服务器宕机,当服务器重启后,能够继续处理事务。就像原本应当的那样。但是,如果客户端连接中止而服务器继续运行,服务器将回滚任何未完成的XA事务,即使该事务已达到PREPARED状态也同样。它应能提交或回滚PREPARED XA事务,但在不更改二进制日志机制的情况下不能这样。

特别推荐一个分享架构+算法的优质内容,还没关注的小伙伴,可以长按关注一下:

927266a39a9c5af73480433b735f15a5.png

b7bca969a3441ea8500d9bed50c91285.png

2b438611814856cd64a3f1eb723759ba.png

长按订阅更多精彩▼

229f9370837dba26b04050064f4f94c4.png

如有收获,点个在看,诚挚感谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值