GTID
定义
GTID 是 MySQL 事务标识,为每一个提交的事务都生成一个标识,并且是全局唯一的,这个特性是从 MySQL5.6 引进的。
组成
GTID 是由 UUID + TID,UUID 是MySQL的唯一标识,每个MySQL实例之间都是不同的。TID是代表了该实例上已经提交的事务数量,并且随着 事务提交 单调递增。
优点
MySQL 主从 基于 GTID 复制,不同于传统复制基于 binlog 日志位点。当主从切换时,MySQL从节点 自动根据事务 在新主库上找到复制位点。GTID复制时,会跳过已经执行过的事务。加强了数据库主备数据一致性。
搭建主从
主库数据备份
mysqldump -uroot -p123456 -h127.0.0.1 -P3307 --single-transaction --master-data=2 --triggers --routines --all-databases > /backup/all.sql
主从开启GTID
主从库 配置文件添加
gtid_mode = on #开启gtid模式 enforce_gtid_consistency = on #强制gtid一致性,开启后对特定的create table不被支持
之后重启 主从 数据库
GTID验证
登录主从验证
mysql> show variables like '%gtid%'; +---------------------------------------------------+-----------+ | Variable_name | Value | +---------------------------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | group_replication_allow_local_disjoint_gtids_join | OFF | | group_replication_gtid_assignment_block_size | 1000000 | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +---------------------------------------------------+-----------+ 10 rows in set (0.01 sec) mysql> show master status\G *************************** 1. row *************************** File: binlog.000016 Position: 1658 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: aadaaaaa-adda-adda-aaaa-aaaaaaddaaaa:1-52, b9193c37-89a7-11ee-8978-00155d68e7c7:1-9 1 row in set (0.00 sec)
可以看到,GTID 开启后 执行 查看当前数据库状态。会多一个 Executed_Gtid_Set 指标
从库还原主库数据
root@LAPTOP-FPIQJ438:/usr/local/mysql-slave# mysql -uroot -p123456 -h127.0.0.1 -P3309 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.38-log MySQL Community Server (GPL) Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> mysql> mysql> mysql> source /backup/all.sql
主库创建复制用户
mysql> create user 'fz'@'%' identified by "123456"; Query OK, 0 rows affected (0.01 sec) mysql> grant replication slave on *.* to 'fz'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> mysql> mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
从库开启复制
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> mysql> mysql> mysql> mysql> reset slave ; Query OK, 0 rows affected (0.01 sec) mysql> mysql> mysql> mysql> change master to master_host='127.0.0.1',master_user='fz',MASTER_PORT=3307,master_password='123456',master_auto_position=1; Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> mysql> mysql> mysql> start slave; Query OK, 0 rows affected (0.01 sec)
可以看到 GTID 复制 不像 传统的基于binlog复制。不需要 binlog文件 和 pos位置位点
从库验证
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: fz Master_Port: 3307 Connect_Retry: 60 Master_Log_File: binlog.000016 Read_Master_Log_Pos: 1658 Relay_Log_File: LAPTOP-FPIQJ438-relay-bin.000002 Relay_Log_Pos: 1777 Relay_Master_Log_File: binlog.000016 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: 1658 Relay_Log_Space: 1986 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: 2 Master_UUID: b9193c37-89a7-11ee-8978-00155d68e7c7 Master_Info_File: mysql.slave_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: b9193c37-89a7-11ee-8978-00155d68e7c7:2-9 Executed_Gtid_Set: 1d48af6d-89a9-11ee-a07d-00155d68e7c7:1-2, aadaaaaa-adda-adda-aaaa-aaaaaaddaaaa:1-52, b9193c37-89a7-11ee-8978-00155d68e7c7:1-9 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
可以看到 Slave_IO_Running 和 Slave_SQL_Running 均为 YES,搭建成功