什么是GTID?
GTID (Global Transaction ID) 是对于一个已提交事务的编号,并且是一个全局唯一的编号。 GTID 实际上 是由UUID+TID 组成的。
其中 UUID 是一个 MySQL 实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
下面是一个GTID的具体形式:03a1eb63-c21a-11ec-b07f-000c2987bea6:1-25,冒号分割前边为UUID,后边为TID。
GTID 集合可以包含来自多个 MySQL 实例的事务,它们之间用逗号分隔。
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
数据库角色 IP 系统与MySQL版本 有无数据
主数据库 192.168.121.164 RHEL7 MySQL5.7 无数据
从数据库 192.168.121.133 RHEL7 MySQL5.7 无数据
主库增加备份用户
mysql> grant replication slave on \*.* to 'slave'@'192.168.121.%' identified by 'MySql@123'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
配置主库文件
注释记得清掉,我这里只是为了说明,配置的时候最好去掉
[root@localhost ~]# vim /etc/my.cnf [mysqld] port=3306 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-name-resolve symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid skip-name-resolve log-bin=mysql-bin #开启二进制日志 server-id=1 #服务器ID,必须唯一 gtid-mode=on #开启gtid模式 enforce-gtid-consistency=on #强制gtid一致性,开启后对特定的create table不支持 binlog-format=row #默认为mixed混合模式,更改成row复制,为了数据一致性 log-slave-updates=1 #从库binlog记录主库同步的操作日志 skip-slave-start=1 #跳过slave复制线程 binlog-ignore-db=mysql
然后重启一下数据库
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld
查看GTID模式状态
[root@localhost ~]# mysql -uroot -pMySql@123 -e 'show variables like "%GTID%";' +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+
配置从库文件
一样记得清注释
[root@localhost ~]# vim /etc/my.cnf [mysqld] port=3306 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid skip-name-resolve log-bin=mysql-bin server-id=2 gtid-mode=on enforce-gtid-consistency=on binlog-format=row log-slave-updates=1 skip-slave-start=1
然后重启一下数据库
[root@localhost ~]# systemctl restart mysqld [root@localhost ~]# systemctl status mysqld
查看GTID模式状态
[root@localhost ~]# mysql -uroot -pMySql@123 -e 'show variables like "%GTID%";' +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+
开启主从同步
mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_host='192.168.121.164', master_user='slave', master_password='MySql@123', master_auto_position=1; mysql> start slave; Query OK, 0 rows affected (0.00 sec)
使用GTID方式同步的时候,必须指定master_auto_position=1
GTID使用master_auto_position=1代替了基于binlog和position号的主从同步方式,更便于主从同步的搭建
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.11 Master_User: Jaking Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: server12-relay-bin.000002 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql-bin.000002 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: 154 Relay_Log_Space: 577 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: 11 Master_UUID: 03a1eb63-c21a-11ec-b07f-000c2987bea6 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: Executed_Gtid_Set: Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
然后进行数据测试
create table aa(nid int,stuname char(15),sex int); insert into aa value (2001,'tom',1),(2002,'mary',0),(2003,'cak',1); select * from aa;
然后去从库查看数据
发现库已经有了,到这里已经可以高兴了
当然还是要看下表了,结果还是令人满意的
这时候我们可以看下两个库的状态
查看主库状态
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000002 | 1620 | | | 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
查看从库状态
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000002 | 1584 | | | 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
到这里就完成辣
还有可以测试一下从库停机后,主库仍在写入数据,从库恢复后能否自动同步数据
从库停掉主从同步
mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.10.11 Master_User: Jaking Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 1620 Relay_Log_File: server12-relay-bin.000002 Relay_Log_Pos: 1833 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: No Slave_SQL_Running: No 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: 1620 Relay_Log_Space: 2043 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: NULL 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: 11 Master_UUID: 03a1eb63-c21a-11ec-b07f-000c2987bea6 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 Executed_Gtid_Set: 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
然后去主库添加数据
查看从库
是没有同步信息过来的,因为我们没有开启同步
然后我们开启同步
所有测试就完成了。
祝大家都能成功。
我在解决问题的时候也有参考一些文档,如有侵权请告知删除