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.000016Position: 1658Binlog_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 eventMaster_Host: 127.0.0.1Master_User: fzMaster_Port: 3307Connect_Retry: 60Master_Log_File: binlog.000016Read_Master_Log_Pos: 1658Relay_Log_File: LAPTOP-FPIQJ438-relay-bin.000002Relay_Log_Pos: 1777Relay_Master_Log_File: binlog.000016Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 1658Relay_Log_Space: 1986Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: b9193c37-89a7-11ee-8978-00155d68e7c7Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: b9193c37-89a7-11ee-8978-00155d68e7c7:2-9Executed_Gtid_Set: 1d48af6d-89a9-11ee-a07d-00155d68e7c7:1-2, aadaaaaa-adda-adda-aaaa-aaaaaaddaaaa:1-52, b9193c37-89a7-11ee-8978-00155d68e7c7:1-9Auto_Position: 1Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version: 1 row in set (0.00 sec)
可以看到 Slave_IO_Running 和 Slave_SQL_Running 均为 YES,搭建成功