详解MySQL故障切换设计参数-session_track_transaction_info

2019-09-10     波波说运维

概述

在数据库中间件读写分离应用场景中,如何保证底层数据库出现故障节点的时,中间件可以快速断开或迁移数据库连接,让用户没有感觉呢?在MySQL数据库中,提供了一个session_track_transaction_info参数来提供解决方案。

A system and method for linking data objects and physical objects of various kinds to an identified user is described. The system provides to the identified user information relating to the delivery status of mail pieces directed to or sent by the user, and the user then tracks and traces the mail pieces.


2、session_track_transaction_info参数

2.1 参数介绍

MySQL5.7中,可以通过设置session_track_transaction_info变量来跟踪事务的状态。

该参数存在global以及session两个级别,可以动态修改。

该参数可以设置的值为0(默认OFF),1,2

/**
Transaction tracking level
*/
enum enum_session_track_transaction_info {
TX_TRACK_NONE = 0, ///< do not send tracker items on transaction info
TX_TRACK_STATE = 1, ///< track transaction status
TX_TRACK_CHISTICS = 2 ///< track status and characteristics
};

该参数允许设置的值为0,1,2

  • 设置为0的时候,show variables like '%session_track_transaction_info%'显示为OFF,表示不开启事务状态跟踪
  • 设置为1的时候,show variables like '%session_track_transaction_info%'显示为STATE,表示跟踪事务状态
  • 设置为2的时候,show variables like '%session_track_transaction_info%'显示为CHARACTERISTICS,表示跟踪事务状态和语句

2.2 参数设置影响

开启session_track_transaction_info参数的时候,在数据库中无法直接查询到事务状态记录。 根据[WL#4797],MySQL是将事务状态跟踪的信息记录到了每一个Query请求返回的OK packet中。 可以通过抓包的方式查看事务状态信息。

For a client to safely relocate a transaction to another session, it must track not only transaction state but also transaction characteristics. In addition, the client must track the transaction_isolation and transaction_read_only system variables to correctly determine the session defaults.

2.2.1 原生MySQL OK packet格式

OK Packet的数据包格式定义

其中int和string中的lenenc表示的是LengthEcode。

MySQL-5.7.19代码中封装OK packet的代码部分在protocol_classic.cc文件中的net_send_ok()函数中。

2.2.3 session_track_transaction_info 额外补充信息

session_track_transaction_info使用8个字符位来表示事务的信息,并且这8个字符信息是保存在COM_QUERY请求语句的返回数据包中的(客户端执行一条语句,都会被封装成MySQL协议中的COM_QUERY请求发送给server端,server端解析执行之后将结果封装在数据包中返回)。


3、总结

在设置session_track_transaction_info参数之后,在MySQL的返回数据包中可以获取到当前连接的事务状态信息。 在数据库中间件上,利用这一特性,使得MySQL故障的情况下,能够自动迁移连接,减少对用户影响。 在部分场景下能够达到底层MySQL节点故障切换了,对应用来说可以无感知的切换过去。

参考:

https://www.wandouip.com/t5i279719/

https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html


后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~

文章来源: https://twgreatdaily.com/zh-hans/b5t1HW0BJleJMoPMbEHM.html