有没有一种方法可以主动获取最新的MariaDB GTID,而不必不断地查询服务器?

huangapple go评论57阅读模式
英文:

Is there a way to actively get the newest MariaDB GTID without constantly querying the server?

问题

我建立了基于GTID的主从复制。我想获取主服务器或从服务器的最新GTID。我知道可以使用像"show variables like '%gtid%'"这样的方式来获取GTID,但这种方式需要定期查询。是否有任何关于MariaDB服务器可以主动通知的方法,如果我提供一个脚本或一个函数的话,MariaDB服务器可以主动通知最新的GTID?

英文:

I establish the master-slave replication based on GTID. And i want to get the newest GTID of master or slave, I know the way of getting GTID by show variables like '%gtid%', but this way requires constant timed query. Is there any way about mariadb server can active notice if I provide a script or a function.

if I provide a script or a function,Mmariadb server can active notice the newest GTID

答案1

得分: 2

MariaDB和MySQL都不能在没有客户端先前请求的情况下向客户端发送数据。

跟踪GTID(全局事务标识符)受到多个客户端连接器(例如MariaDB Connector/C或MariaDB Connector/Python)的支持,其中GTID将从上一个提交的OK数据包的会话状态信息中获取。

以下是Python中的一个简单示例:

import mariadb

def status_tracker(connection, status):
    if 'last_gtid' in status:
        print("last_gtid changed: %s" % status['last_gtid'])

conn = mariadb.connect(status_callback=status_tracker, db="test")

cursor = conn.cursor()

# 告诉服务器在会话状态信息中提交last_gtid
cursor.execute("SET @@session.session_track_system_variables='last_gtid'")

cursor.execute("CREATE TEMPORARY TABLE t1 (a int)")
conn.commit()
cursor.execute("CREATE TEMPORARY TABLE t2 (a int)")
conn.commit()
cursor.execute("SET gtid_seq_no=1000")
cursor.execute("CREATE TEMPORARY TABLE t3 (a int)")
conn.commit()

结果/输出:

last_gtid changed: 0-1-40802
last_gtid changed: 0-1-40803
last_gtid changed: 0-1-1000
英文:

Neither MariaDB nor MySQL can send data to clients without a previous request from the client.

Tracking the GTID is supported by several client connectors (like MariaDB Connector/C or MariaDB Connector/Python) where the GTID will be obtainted from the session state information of the OK packet from previous commit.

Here is a simple example in Python:

import mariadb

def status_tracker(connection, status):
    if 'last_gtid' in status:
      print("last_gtid changed: %s" % status['last_gtid'])


conn= mariadb.connect(status_callback=status_tracker, db="test")

cursor= conn.cursor()

# tell the server to submit last_gtid in session_state_information
cursor.execute("SET @@session.session_track_system_variables='last_gtid'")

cursor.execute("CREATE TEMPORARY TABLE t1 (a int)")
conn.commit()
cursor.execute("CREATE TEMPORARY TABLE t2 (a int)")
conn.commit()
cursor.execute("SET gtid_seq_no=1000")
cursor.execute("CREATE TEMPORARY TABLE t3 (a int)")
conn.commit()

Result/Output:

last_gtid changed: 0-1-40802
last_gtid changed: 0-1-40803
last_gtid changed: 0-1-1000

huangapple
  • 本文由 发表于 2023年5月29日 10:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定