简体   繁体   English

提交事务后,为什么MySQL`gtid_owned`会话变量总是为空?

[英]Why is MySQL `gtid_owned` session variable always empty after committing a transaction?

I'm testing some work with MySQL Global Transaction IDs (GTIDs) and I'm having a hard time getting the most-recent session GTID. 我正在使用MySQL全局事务ID(GTID)测试一些工作,并且我很难获得最近的会话GTID。 I've enabled GTIDs (global gtid_mode is set to ON_PERMISSIVE ). 我启用了GTID(全局gtid_mode设置为ON_PERMISSIVE )。 According to the documentation for gtid_owned : 根据gtid_owned的文档:

This read-only variable holds a list whose contents depend on its scope. 此只读变量包含一个列表,其内容取决于其范围。 When used with session scope, the list holds all GTIDs that are owned by this client; 当与会话范围一起使用时,该列表包含此客户端拥有的所有GTID; ... ...

So, I expect, after committing a transaction, that this session variable would contain GTIDs; 所以,在提交事务之后,我希望这个会话变量包含GTID; but it is always empty, no matter what I do. 但无论我做什么,它总是空着的。 However, the global gtid_executed changes every time I commit a transaction, so I know that GTIDs are working. 但是,每次提交事务时全局gtid_executed更改,所以我知道GTID正在运行。

Here's a session that demonstrates this issue: 这是一个演示此问题的会话:

mysql> SELECT @@global.gtid_executed;
| a1c32161-89c4-11e8-856e-0242ac12001d:1-4 |

mysql> SELECT @@session.gtid_next;
| AUTOMATIC           |

mysql> SELECT @@session.gtid_owned;
|                      |

mysql> START TRANSACTION;

mysql> SELECT @@global.gtid_executed;
| a1c32161-89c4-11e8-856e-0242ac12001d:1-4 |

mysql> SELECT @@session.gtid_next;
| AUTOMATIC           |

mysql> SELECT @@session.gtid_owned;
|                      |

mysql> INSERT INTO ........

mysql> SELECT @@global.gtid_executed;
| a1c32161-89c4-11e8-856e-0242ac12001d:1-4 |

mysql> SELECT @@session.gtid_next;
| AUTOMATIC           |

mysql> SELECT @@session.gtid_owned;
|                      |

mysql> COMMIT;

mysql> SELECT @@global.gtid_executed;
| a1c32161-89c4-11e8-856e-0242ac12001d:1-5 |

mysql> SELECT @@session.gtid_next;
| AUTOMATIC           |

mysql> SELECT @@session.gtid_owned;
|                      |

mysql> INSERT INTO ........

mysql> SELECT @@global.gtid_executed;
| a1c32161-89c4-11e8-856e-0242ac12001d:1-6 |

mysql> SELECT @@session.gtid_next;
| AUTOMATIC           |

mysql> SELECT @@session.gtid_owned;
|                      |

Notice that each time a transaction is committed (explicitly or implicitly/autocommit), the global list of executed GTIDs is incremented, but the session gtid_owned is always empty. 请注意,每次提交事务(显式或隐式/自动提交)时,已执行GTID的全局列表都会递增,但会话gtid_owned始终为空。

What am I doing wrong/missing? 我做错了什么/错过了什么? Or have I found a bug? 或者我发现了一个错误?

A client only owns a transaction that is still open, so gtid_owned is cleared once the transaction commits. 客户端只拥有一个仍处于打开状态的事务,因此一旦事务提交,就会清除gtid_owned It also only shows a value if you have explicitly set one for gtid_next ; 如果您为gtid_next明确设置了一个值,它也只显示一个值; using gtid_next=automatic will not populate gtid_owned . 使用gtid_next=automatic不会填充gtid_owned The use of gtid_owned is therefore limited to some internal operations and to cases like replication where the GTID is explicitly set in the binary log. 因此, gtid_owned的使用仅限于某些内部操作以及复制等情况,其中GTID在二进制日志中明确设置。

To get the list of gtids set by the current session, you could enable session_track_gtids (see https://dev.mysql.com/doc/en/server-system-variables.html#sysvar_session_track_gtids ). 要获取当前会话设置的gtid列表,可以启用session_track_gtids (请参阅https://dev.mysql.com/doc/en/server-system-variables.html#sysvar_session_track_gtids )。

Note however that the 'mysql' command line client does not report the values returned by the session tracker; 但请注意,'mysql'命令行客户端不报告会话跟踪器返回的值; you'd need a client which lets you call the C API functions mysql_session_track_get_first() and mysql_session_track_get_next() (see https://dev.mysql.com/doc/en/mysql-session-track-get-first.html ). 你需要一个允许你调用C API函数mysql_session_track_get_first()mysql_session_track_get_next()的客户端(参见https://dev.mysql.com/doc/en/mysql-session-track-get-first.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM