当前位置:   article > 正文

MySql GTID主从复制

gtid主从复制

目录

1.GTID介绍

1.1GTID是什么

1.2 GTID Replication介绍

1.3 GTID的组成

1.4 GTID的作用

1.5 为什么要用GTID

1.6 GTID比传统复制的优势

1.7GTID复制实现的工作原理

2 主从复制

2.1 一主一从

2.1.1部署数据库可查看 2.1.2 授权一个用户,用于主从复制主从>

2.1.3 修改master主配文件

2.1.4 slave主配文件

2.1.5 配置slave主机,授权从数据库

2.1.6 验证主从复制

2.2 一主多从

2.2.1 第二台从数据库slave主配文件

2.2.2 配置第二台slave主机

2.3 多主一从

2.3.1 两台主数据库配置和上述主数据库相同,从数据库配置也相同2.3.2 查看主库信息

2.3.3 配置slave主机

2.3.4 在从库上配置主库信息

2.3.5 验证测试


1.GTID介绍


1.1GTID是什么


GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识,保存在mysql数据目录下的auto.cnf文件里。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。下面是一个GTID的具体形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23。

  1. 全称为: Global Transaction ID ,可简化MySQL(5.6版本之后)的主从切换以及Failover。
  2. GTID是一个字符串类似 `3E11FA47-71CA-11E1-9E33-C80AA9429562:23`
  3. 由UUID+TID组成,
  4. UUID代表MySQL实例,
  5. TID代表这个实例提交的事务数量。


1.2 GTID Replication介绍


从MySQL5.6开始增加了强大的GTID(Global Transaction ID,全局事务ID)这个特性,用来强化数据库的主备一致性, 故障恢复, 以及容错能力。用于取代过去传统的主从复制(即:基于binlog和position的异步复制)。借助GTID,在发生主备切换的情况下,MySQL的其他slave可以自动在新主上找到正确的复制位置,这大大简化了复杂复制拓扑下集群的维护,也减少了人为设置复制position发生误操作的风险。另外,基于GTID的复制可以忽略已经执行过的事务,减少了数据发生不一致的风险。

1.3 GTID的组成


GTID是由server_uuid和事务id组成的,即GTID=server_uuid:transaction_id。
server_uuid,是在MySQL第一次启动时自动生成并持久化到auto.cnf文件(存放在数据目录下,每台机器的server_uuid都不一样。
transaction_id,是一个从1开始的自增计数,表示在这个主库上执行的第n个事务。MySQL会保证事务与GTID之间的1:1映射,如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1
表示在以6ba9a76d-606b-11ea-b3ce-000c29cb3421为唯一标识的MySQL实例上执行的第1个数据库事务。
一组连续的事务可以用 “-” 连接的事务序号范围表示。例如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1-15

1.4 GTID的作用


根据GTID可以知道事务最初是在哪个实例上提交的
GTID的存在方便了Replication的Failover


1.5 为什么要用GTID


在主从复制中,尤其是半同步复制中, 由于Master 的dump进程一边要发送binlog给Slave,一边要等待Slave的ACK消息,这个过程是串行的,即前一个事物的ACK没有收到消息,那么后一个事物只能排队候着; 这样将会极大地影响性能;有了GTID后,SLAVE就直接可以通过数据流获得GTID信息,而且可以同步

主从故障切换中,如果一台MASTER down,需要提取拥有最新日志的SLAVE做MASTER,这个是很好判断,而有了GTID,就只要以GTID为准即可方便判断;而有了GTID后,SLAVE就不需要一直保存这bin-log 的文件名和Position了;只要启用MASTER_AUTO_POSITION 即可

当MASTER crash的时候,GTID有助于保证数据一致性,因为每个事物都对应唯一GTID,如果在恢复的时候某事物被重复提交,SLAVE会直接忽略

1.6 GTID比传统复制的优势


更简单的实现failover,不用以前那样在需要找log_file和log_Pos
更简单的搭建主从复制
比传统复制更加安全
GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过


1.7GTID复制实现的工作原理


主节点更新数据时,会在事务前产生GTID,一起记录到binlog日志中
从节点的I/O线程将变更的bin log,写入到本地的relay log中
SQL线程从relay log中获取GTID,然后对比本地binlog是否有记录(所以MySQL从节点必须要开启binary log)
如果有记录,说明该GTID的事务已经执行,从节点会忽略
如果没有记录,从节点就会从relay log中执行该GTID的事务,并记录到bin log
在解析过程中会判断是否有主键,如果没有就用二级索引,如果有就用全部扫描


2 主从复制


2.1 一主一从

数据库    IP
master   192.268.25.140
slave   192.168.25.142


2.1.1部署数据库可查看 <MYSQL主从>
2.1.2 授权一个用户,用于主从复制

  1. mysql> show variables like '%gtid%';
  2. +----------------------------------+-----------+
  3. | Variable_name | Value |
  4. +----------------------------------+-----------+
  5. | binlog_gtid_simple_recovery | ON |
  6. | enforce_gtid_consistency | OFF |
  7. | gtid_executed_compression_period | 1000 |
  8. | gtid_mode | OFF |
  9. | gtid_next | AUTOMATIC |
  10. | gtid_owned | |
  11. | gtid_purged | |
  12. | session_track_gtids | OFF |
  13. +----------------------------------+-----------+
  14. 8 rows in set (0.01 sec)
  15. mysql> create user 'zj'@'192.168.25.142' identified by "1";
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> grant replication slave on *.* to 'zj'@'192.168.25.142' ;
  18. Query OK, 0 rows affected (0.00 sec)
  19. mysql> flush privileges;
  20. Query OK, 0 rows affected (0.00 sec)


2.1.3 修改master主配文件

  1. [root@localhost ~]# vim /etc/my.cnf
  2. [mysqld]
  3. basedir = /usr/local/mysql
  4. datadir = /opt/data
  5. socket = /tmp/mysql.sock
  6. port = 3306
  7. pid-file = /opt/data/mysql.pid
  8. skip-name-resolve
  9. log-bin = mysql-bin #开启二进制日志
  10. server-id = 1
  11. gtid-mode = on #开启gtid模式
  12. enforce-gtid-consistency = on #强制gtid一致性,开启后对特定的create table不被支持
  13. binlog-format = row #默认为mixed混合模式,更改成row复制,为了数据一致性
  14. log-slave-updates = 1 #从库binlog才会记录主库同步的操作日志
  15. skip-slave-start = 1 #跳过slave复制线程
  16. // 重启数据库服务
  17. [root@localhost ~]# service mysqld restart
  18. Shutting down MySQL.. SUCCESS!
  19. Starting MySQL. SUCCESS!
  20. // 查看master数据库状态
  21. mysql> show master status;
  22. +------------------+----------+--------------+------------------+------------------------------------------+
  23. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  24. +------------------+----------+--------------+------------------+------------------------------------------+
  25. | mysql-bin.000014 | 1951 | | | b736875b-097e-11ec-b557-000c29810dc2:1-9 |
  26. +------------------+----------+--------------+------------------+------------------------------------------+
  27. 1 row in set (0.00 sec)


2.1.4 slave主配文件

  1. [root@localhost ~]# vim /etc/my.cnf
  2. [mysqld]
  3. basedir = /usr/local/mysql
  4. datadir = /opt/data
  5. socket = /tmp/mysql.sock
  6. port = 3306
  7. pid-file = /opt/data/mysql.pid
  8. skip-name-resolve
  9. server-id = 2
  10. log-bin = mysql-bin
  11. binlog-format = row
  12. skip-slave-start = 1
  13. log-slave-updates = 1
  14. gtid-mode = on
  15. enforce-gtid-consistency = on
  16. [root@localhost ~]# service mysqld restart
  17. Shutting down MySQL.. SUCCESS!
  18. Starting MySQL. SUCCESS!

2.1.5 配置slave主机,授权从数据库

  1. // 检查gtid模式状态
  2. mysql> show variables like '%gtid%';
  3. +----------------------------------+-----------+
  4. | Variable_name | Value |
  5. +----------------------------------+-----------+
  6. | binlog_gtid_simple_recovery | ON |
  7. | enforce_gtid_consistency | ON |
  8. | gtid_executed_compression_period | 1000 |
  9. | gtid_mode | ON |
  10. | gtid_next | AUTOMATIC |
  11. | gtid_owned | |
  12. | gtid_purged | |
  13. | session_track_gtids | OFF |
  14. +----------------------------------+-----------+
  15. mysql> reset slave;
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> change master to
  18. -> master_host='192.168.25.140' ,
  19. -> master_user='zj' ,
  20. -> master_password='1' ,
  21. -> master_auto_position=1;
  22. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  23. mysql> start slave ;
  24. Query OK, 0 rows affected (0.00 sec)
  25. mysql> show slave status\G
  26. *************************** 1. row ***************************
  27. Slave_IO_State: Waiting for master to send event
  28. Master_Host: 192.168.25.140
  29. Master_User: zj
  30. Master_Port: 3306
  31. Connect_Retry: 60
  32. Master_Log_File: mysql-bin.000014
  33. Read_Master_Log_Pos: 154
  34. Relay_Log_File: localhost-relay-bin.000003
  35. Relay_Log_Pos: 367
  36. Relay_Master_Log_File: mysql-bin.000014
  37. Slave_IO_Running: Yes
  38. Slave_SQL_Running: Yes

2.1.6 验证主从复制

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | JJ |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> show master status;
  13. +------------------+----------+--------------+------------------+-------------------------------------------+
  14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  15. +------------------+----------+--------------+------------------+-------------------------------------------+
  16. | mysql_bin.000006 | 1389 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |
  17. +------------------+----------+--------------+------------------+-------------------------------------------+
  18. 1 row in set (0.00 sec)

2.2 一主多从

数据库 IP
master   192.168.25.1403
slave  192.168.25.142
slave  192.268.25.144

2.2.1 第二台从数据库slave主配文件

  1. [root@localhost ~]# vim /etc/my.cnf
  2. [mysqld]
  3. basedir = /usr/local/mysql
  4. datadir = /opt/data
  5. socket = /tmp/mysql.sock
  6. port = 3306
  7. pid-file = /opt/data/mysql.pid
  8. skip-name-resolve
  9. server-id = 3 //第三台从服务器id 不能与前面使用过的一样
  10. log-bin = mysql-bin
  11. binlog-format = row
  12. skip-slave-start = 1
  13. log-slave-updates = 1
  14. gtid-mode = on
  15. enforce-gtid-consistency = on
  16. [root@localhost ~]# service mysqld restart
  17. Shutting down MySQL.. SUCCESS!
  18. Starting MySQL. SUCCESS

2.2.2 配置第二台slave主机

  1. mysql> show variables like '%gtid%';
  2. +----------------------------------+-----------+
  3. | Variable_name | Value |
  4. +----------------------------------+-----------+
  5. | binlog_gtid_simple_recovery | ON |
  6. | enforce_gtid_consistency | ON |
  7. | gtid_executed_compression_period | 1000 |
  8. | gtid_mode | ON |
  9. | gtid_next | AUTOMATIC |
  10. | gtid_owned | |
  11. | gtid_purged | |
  12. | session_track_gtids | OFF |
  13. +----------------------------------+-----------+
  14. mysql> reset slave;
  15. Query OK, 0 rows affected (0.01 sec)
  16. mysql> change master to
  17. -> master_host='192.168.25.140' ,
  18. -> master_user='zj' ,
  19. -> master_password='1' ,
  20. -> master_auto_position=1;
  21. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  22. mysql> start slave ;
  23. Query OK, 0 rows affected (0.00 sec)
  24. mysql> show slave status\G
  25. *************************** 1. row ***************************
  26. Slave_IO_State: Waiting for master to send event
  27. Master_Host: 192.168.25.140
  28. Master_User: zj
  29. Master_Port: 3306
  30. Connect_Retry: 60
  31. Master_Log_File: mysql-bin.000014
  32. Read_Master_Log_Pos: 1776
  33. Relay_Log_File: localhost-relay-bin.000004
  34. Relay_Log_Pos: 454
  35. Relay_Master_Log_File: mysql-bin.000014
  36. Slave_IO_Running: Yes
  37. Slave_SQL_Running: Yes
  38. mysql> show databases;
  39. +--------------------+
  40. | Database |
  41. +--------------------+
  42. | information_schema |
  43. | JJ |
  44. | mysql |
  45. | performance_schema |
  46. | sys |
  47. +--------------------+
  48. 5 rows in set (0.00 sec)
  49. mysql> show master status;
  50. +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+
  51. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  52. +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+
  53. | mysql_bin.000008 | 750 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10,
  54. ba11a7ff-097f-11ec-9272-000c29959565:1,
  55. ddd6f885-0a26-11ec-81cc-000c29f2278e:1 |
  56. +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+

2.3 多主一从

数据库    IP
master    192.168.25.140
master    192.168.25.142
slave192.168.25.144

2.3.1 两台主数据库配置和上述主数据库相同,从数据库配置也相同
2.3.2 查看主库信息

  1. //主库1
  2. mysql> show master status;
  3. +------------------+----------+--------------+------------------+-------------------------------------------+
  4. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  5. +------------------+----------+--------------+------------------+-------------------------------------------+
  6. | mysql-bin.000014 | 2128 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |
  7. +------------------+----------+--------------+------------------+-------------------------------------------+
  8. 1 row in set (0.00 sec)
  9. 主库2
  10. mysql> show master status;
  11. +------------------+----------+--------------+------------------+-------------------------------------------+
  12. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  13. +------------------+----------+--------------+------------------+-------------------------------------------+
  14. | mysql-bin.000002 | 154 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |
  15. +------------------+----------+--------------+------------------+-------------------------------------------+
  16. 1 row in set (0.00 sec)

2.3.3 配置slave主机

  1. //检查gtid模式状态
  2. mysql> show variables like '%gtid%';
  3. +----------------------------------+-----------+
  4. | Variable_name | Value |
  5. +----------------------------------+-----------+
  6. | binlog_gtid_simple_recovery | ON |
  7. | enforce_gtid_consistency | ON |
  8. | gtid_executed_compression_period | 1000 |
  9. | gtid_mode | ON |
  10. | gtid_next | AUTOMATIC |
  11. | gtid_owned | |
  12. | gtid_purged | |
  13. | session_track_gtids | OFF |
  14. +----------------------------------+-----------+
  15. mysql> reset slave;
  16. Query OK, 0 rows affected (0.01 sec)

2.3.4 在从库上配置主库信息

  1. //配置主库1
  2. //下面两条命令不执行,后续会报错
  3. mysql> set global master_info_repository='table';
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> set global relay_log_info_repository='table';
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> change master to
  8. -> master_host='192.168.25.140' , //这里是主库IP
  9. -> master_user='zj' ,
  10. -> master_password='1' ,
  11. -> master_log_file='mysql-bin.000014'
  12. -> master_log_pos=2128
  13. -> for channel 'master-1'; //创建隧道
  14. 配置主库2
  15. mysql> change master to
  16. -> master_host='192.168.25.142' ,
  17. -> master_user='zj' ,
  18. -> master_password='1' ,
  19. -> master_log_file='mysql-bin.000002'
  20. -> master_log_pos=154
  21. -> for channel 'master-2'; //创建隧道
  22. mysql> START SLAVE;
  23. Query OK, 0 rows affected (0.00 sec)
  24. //查看发现都已经开启
  25. mysql> SHOW SLAVE STATUS\G
  26. *************************** 1. row ***************************
  27. Slave_IO_State: Waiting for master to send event
  28. Master_Host: 192.168.25.140
  29. Master_User: zj
  30. Master_Port: 3306
  31. Connect_Retry: 60
  32. Master_Log_File: mysql-bin.000014
  33. Read_Master_Log_Pos: 2128
  34. Relay_Log_File: localhost-relay-bin-master@002d1.000002
  35. Relay_Log_Pos: 320
  36. Relay_Master_Log_File: mysql-bin.000014
  37. Slave_IO_Running: Yes
  38. Slave_SQL_Running: Yes
  39. Replicate_Do_DB:
  40. Replicate_Ignore_DB:
  41. Replicate_Do_Table:
  42. Replicate_Ignore_Table:
  43. Replicate_Wild_Do_Table:
  44. Replicate_Wild_Ignore_Table:
  45. Last_Errno: 0
  46. Last_Error:
  47. Skip_Counter: 0
  48. Exec_Master_Log_Pos: 2128
  49. Relay_Log_Space: 544
  50. Until_Condition: None
  51. Until_Log_File:
  52. Until_Log_Pos: 0
  53. Master_SSL_Allowed: No
  54. Master_SSL_CA_File:
  55. Master_SSL_CA_Path:
  56. Master_SSL_Cert:
  57. Master_SSL_Cipher:
  58. Master_SSL_Key:
  59. Seconds_Behind_Master: 0
  60. Master_SSL_Verify_Server_Cert: No
  61. Last_IO_Errno: 0
  62. Last_IO_Error:
  63. Last_SQL_Errno: 0
  64. Last_SQL_Error:
  65. Replicate_Ignore_Server_Ids:
  66. Master_Server_Id: 1
  67. Master_UUID: b736875b-097e-11ec-b557-000c29810dc2
  68. Master_Info_File: mysql.slave_master_info
  69. SQL_Delay: 0
  70. SQL_Remaining_Delay: NULL
  71. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  72. Master_Retry_Count: 86400
  73. Master_Bind:
  74. Last_IO_Error_Timestamp:
  75. Last_SQL_Error_Timestamp:
  76. Master_SSL_Crl:
  77. Master_SSL_Crlpath:
  78. Retrieved_Gtid_Set:
  79. Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1
  80. Auto_Position: 0
  81. Replicate_Rewrite_DB:
  82. Channel_Name: master-1
  83. Master_TLS_Version:
  84. *************************** 2. row ***************************
  85. Slave_IO_State: Waiting for master to send event
  86. Master_Host: 192.168.25.142
  87. Master_User: zj
  88. Master_Port: 3306
  89. Connect_Retry: 60
  90. Master_Log_File: mysql-bin.000002
  91. Read_Master_Log_Pos: 154
  92. Relay_Log_File: localhost-relay-bin-master@002d2.000002
  93. Relay_Log_Pos: 320
  94. Relay_Master_Log_File: mysql-bin.000002
  95. Slave_IO_Running: Yes
  96. Slave_SQL_Running: Yes
  97. Replicate_Do_DB:
  98. Replicate_Ignore_DB:
  99. Replicate_Do_Table:
  100. Replicate_Ignore_Table:
  101. Replicate_Wild_Do_Table:
  102. Replicate_Wild_Ignore_Table:
  103. Last_Errno: 0
  104. Last_Error:
  105. Skip_Counter: 0
  106. Exec_Master_Log_Pos: 154
  107. Relay_Log_Space: 544
  108. Until_Condition: None
  109. Until_Log_File:
  110. Until_Log_Pos: 0
  111. Master_SSL_Allowed: No
  112. Master_SSL_CA_File:
  113. Master_SSL_CA_Path:
  114. Master_SSL_Cert:
  115. Master_SSL_Cipher:
  116. Master_SSL_Key:
  117. Seconds_Behind_Master: 0
  118. Master_SSL_Verify_Server_Cert: No
  119. Last_IO_Errno: 0
  120. Last_IO_Error:
  121. Last_SQL_Errno: 0
  122. Last_SQL_Error:
  123. Replicate_Ignore_Server_Ids:
  124. Master_Server_Id: 2
  125. Master_UUID: ba11a7ff-097f-11ec-9272-000c29959565
  126. Master_Info_File: mysql.slave_master_info
  127. SQL_Delay: 0
  128. SQL_Remaining_Delay: NULL
  129. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  130. Master_Retry_Count: 86400
  131. Master_Bind:
  132. Last_IO_Error_Timestamp:
  133. Last_SQL_Error_Timestamp:
  134. Master_SSL_Crl:
  135. Master_SSL_Crlpath:
  136. Retrieved_Gtid_Set:
  137. Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1
  138. Auto_Position: 0
  139. Replicate_Rewrite_DB:
  140. Channel_Name: master-2
  141. Master_TLS_Version:
  142. 2 rows in set (0.00 sec)

2.3.5 验证测试

  1. 主库1上创建数据库ll
  2. mysql> create database ll;
  3. Query OK, 1 row affected (0.00 sec)
  4. mysql> show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | ll |
  10. | mysql |
  11. | performance_schema |
  12. | sys |
  13. +--------------------+
  14. 5 rows in set (0.00 sec)
  15. 主库2上创建数据库mm
  16. mysql> create database mm;
  17. Query OK, 1 row affected (0.00 sec)
  18. mysql> show databases;
  19. +--------------------+
  20. | Database |
  21. +--------------------+
  22. | information_schema |
  23. | mysql |
  24. | performance_schema |
  25. | sys |
  26. | mm |
  27. +--------------------+
  28. 5 rows in set (0.00 sec)
  29. // 从库上开启从服务后查看
  30. mysql> start slave;
  31. Query OK, 0 rows affected (0.00 sec)
  32. mysql> show databases;
  33. +--------------------+
  34. | Database |
  35. +--------------------+
  36. | information_schema |
  37. | ll |
  38. | mysql |
  39. | performance_schema |
  40. | sys |
  41. | mm |
  42. +--------------------+
  43. 6 rows in set (0.00 sec)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/959310
推荐阅读
相关标签
  

闽ICP备14008679号