首页 > 数据库 > MySQL > 正文

MySQL的GTID复制如何应用

2024-07-24 12:33:09
字体:
来源:转载
供稿:网友
  从MySQL 5.6.5开始新增了一种基于GTID的复制方式。通过GTID保证了每个在主库上提交的事务在集群中有一个唯一的ID。这种方式强化了数据库的主备一致性,故障恢复以及容错能力。
 
  GTID是什么
  GTID (Global Transaction ID) 是对于一个已提交事务的编号,并且是一个全局唯一的编号。 GTID实际上是由UUID+TID 组成的。其中UUID是一个 MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
 
  GTID的工作原理
  当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。
  binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值。
  sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。
  如果有记录,说明该GTID的事务已经执行,slave会忽略。
  如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。
  一主一从GTID复制的搭建
  主机规划:
 
  master:docker,端口3312
  slave:docker,端口3313
  master的配置
  配置文件my.cnf内容如下:
 
  $ cat /home/mysql/docker-data/3313/conf/my.cnf
  # For advice on how to change settings please see
  # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
 
  [mysqld]
  #
  # Remove leading # and set to the amount of RAM for the most important data
  # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  # innodb_buffer_pool_size = 128M
  #
  # Remove leading # to turn on a very important data integrity option: logging
  # changes to the binary log between backups.
  # log_bin
  #
  # Remove leading # to set options mainly useful for reporting servers.
  # The server defaults are faster for transactions and fast SELECTs.
  # Adjust sizes as needed, experiment to find the optimal values.
  # join_buffer_size = 128M
  # sort_buffer_size = 2M
  # read_rnd_buffer_size = 2M
  #datadir=/home/mysql/docker-data/3307/data
  #socket=/home/mysql/docker-data/3307/mysql.sock
 
  character_set_server=utf8
  init_connect='SET NAMES utf8'
 
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
 
  #log-error=/home/mysql/docker-data/3307/logs/mysqld.log
  #pid-file=/home/mysql/docker-data/3307/mysqld.pid
  lower_case_table_names=1
  server-id=1403311
  log-bin=mysql-bin
  binlog-format=ROW
  auto_increment_increment=1
  auto_increment_offset=1
  # 开启gtid
  gtid_mode=ON
  enforce-gtid-consistency=true
 
  #rpl_semi_sync_master_enabled=1
  #rpl_semi_sync_master_timeout=10000
  创建docker实例:
 
  $ docker run --name mysql3312 -p 3312:3306 --privileged=true -ti -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=order -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -v /home/mysql/docker-data/3312/conf:/etc/mysql/conf.d -v /home/mysql/docker-data/3312/data/:/var/lib/mysql -v /home/mysql/docker-data/3312/logs/:/var/log/mysql -d mysql:5.7
  添加用于复制的用户并授权:
 
  mysql> GRANT REPLICATION SLAVE,FILE,REPLICATION CLIENT ON *.* TO 'repluser'@'%' IDENTIFIED BY '123456';
  Query OK, 0 rows affected, 1 warning (0.01 sec)
 
  mysql> FLUSH PRIVILEGES;
  Query OK, 0 rows affected (0.01 sec)
  slave的配置
  配置文件my.cnf内容与master一致,注意修改server-id,保持唯一。
 
  *************************** 1. row ***************************
                 Slave_IO_State: Waiting for master to send event
                    Master_Host: 172.23.252.98
                    Master_User: repluser
                    Master_Port: 3312
                  Connect_Retry: 60
                Master_Log_File: mysql-bin.000006
            Read_Master_Log_Pos: 419
                 Relay_Log_File: 5dfbef024732-relay-bin.000003
                  Relay_Log_Pos: 632
          Relay_Master_Log_File: mysql-bin.000006
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes
                Replicate_Do_DB:
            Replicate_Ignore_DB:
             Replicate_Do_Table:
         Replicate_Ignore_Table:
        Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:
                     Last_Errno: 0
                     Last_Error:
                   Skip_Counter: 0
            Exec_Master_Log_Pos: 419
                Relay_Log_Space: 846
                Until_Condition: None
                 Until_Log_File:
                  Until_Log_Pos: 0
             Master_SSL_Allowed: No
             Master_SSL_CA_File:
             Master_SSL_CA_Path:
                Master_SSL_Cert:
              Master_SSL_Cipher:
                 Master_SSL_Key:
          Seconds_Behind_Master: 0
  Master_SSL_Verify_Server_Cert: No
                  Last_IO_Errno: 0
                  Last_IO_Error:
                 Last_SQL_Errno: 0
                 Last_SQL_Error:
    Replicate_Ignore_Server_Ids:
               Master_Server_Id: 1403311
                    Master_UUID: cd2eaa0a-7a59-11ec-b3b4-0242ac110002
               Master_Info_File: /var/lib/mysql/master.info
                      SQL_Delay: 0
            SQL_Remaining_Delay: NULL
        Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
             Master_Retry_Count: 86400
                    Master_Bind:
        Last_IO_Error_Timestamp:
       Last_SQL_Error_Timestamp:
                 Master_SSL_Crl:
             Master_SSL_Crlpath:
             Retrieved_Gtid_Set: cd2eaa0a-7a59-11ec-b3b4-0242ac110002:1
              Executed_Gtid_Set: cd2eaa0a-7a59-11ec-b3b4-0242ac110002:1
                  Auto_Position: 1
           Replicate_Rewrite_DB:
                   Channel_Name:
             Master_TLS_Version:
  1 row in set (0.00 sec)
  在master.order表插入数据:
 
  mysql> insert into t_order values(4,"V");
  发现数据已经同步至slave:
 
  mysql> select * from order.t_order;
  +------+------+
  | id   | name |
  +------+------+
  |    4 | V    |
  +------+------+
  3 rows in set (0.00 sec)
  先停止slave,再在master.order表插入数据:
  +------+------+
  |    4 | V    |
  |    5 | X    |
  +------+------+
  4 rows in set (0.00 sec)
  遇到的问题
  在slave服务器show slave status:
 
  Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
  首先检查master和slave的server_id是否一致,如果一致去修改my.cnf文件中的server_id字段:
 
  mysql> show variables like 'server_id';
  然后排查master和slave的uuid是否一致:
 
  mysql> show variables like '%uuid%';
  如果uuid一致去修改data目录下的auto.cnf文件,拷贝整个data目录,把auto.cnf文件也拷贝过来了,里面记录了数据库的uuid,每个库的uuid应该是不一样的。

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表