侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

浅析mysql主从复制中的gtid

2024-05-08 星期三 / 0 评论 / 0 点赞 / 61 阅读 / 2500 字

gtid(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。它的官方定义如下:gtid= source_id :transaction_id每一个 gt

gtid(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。它的官方定义如下:
gtid= source_id :transaction_id

每一个 gtid代表一个数据库事务。在上面的定义中,source_id 表示执行事务的主库 uuid(server_uuid),transaction_id 是一个从 1 开始的自增计数,表示在这个主库上执行的第 n 个事务。MySQL 只要保证每台数据库的 server_uuid 全局唯一,以及每台数据库生成的 transaction_id 自身唯一,就能保证 gtid 的全局唯一性。
在开启gtid的主从复制的环境下,在slave上执行show slave status/G 可以看到下述信息:

mysql> show slave status/G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event...             Slave_IO_Running: Yes            Slave_SQL_Running: Yes...           Retrieved_Gtid_Set: b5e3d908-fa6d-11e7-b931-06f990000100:1-3            Executed_Gtid_Set: b5e3d908-fa6d-11e7-b931-06f990000100:1-3                Auto_Position: 11 row in set (0.00 sec)

Retrieved_Gtid_Set 表示slave从master接受的gtid set,使用 reset slave 命令可以清空此项;
Executed_Gtid_Set 表示slave已执行的gtid set,使用 reset master 命令可以清空此项。
Retrieved_Gtid_Set 和 Executed_Gtid_Set 必须为master 上 gtid set 的子集,否则会报以下错误:

mysql> show slave status/G*************************** 1. row ***************************...                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Slave has more GTIDs than the master has, using the master's SERVER_UUID. This may indicate that the end of the binary log was truncated or that the last binary log file was lost, e.g., after a power or disk failure when sync_binlog != 1. The master may or may not have rolled back transactions that were already replica'...1 row in set (0.00 sec)

slave 停掉后再次启动时,会进行以下操作:
1.读取master上的gtid set(假设为set A)
2.将set A和自身的 Retrieved_Gtid_Set(假设为set B) 对比,执行 A-B 部分的事务以保持和master的同步

这里本来是要贴上验证的操作的,但51cto博客的表格展示很不友好,所以验证的工作就交给大家啦~^o^

广告 广告

评论区