GTID
定義
GTID 是 MySQL 事務(wù)標(biāo)識,為每一個提交的事務(wù)都生成一個標(biāo)識,并且是全局唯一的,這個特性是從 MySQL5.6 引進的。
組成
GTID 是由 UUID + TID,UUID 是MySQL的唯一標(biāo)識,每個MySQL實例之間都是不同的。TID是代表了該實例上已經(jīng)提交的事務(wù)數(shù)量,并且隨著 事務(wù)提交 單調(diào)遞增。
優(yōu)點
MySQL 主從 基于 GTID 復(fù)制,不同于傳統(tǒng)復(fù)制基于 binlog 日志位點。當(dāng)主從切換時,MySQL從節(jié)點 自動根據(jù)事務(wù) 在新主庫上找到復(fù)制位點。GTID復(fù)制時,會跳過已經(jīng)執(zhí)行過的事務(wù)。加強了數(shù)據(jù)庫主備數(shù)據(jù)一致性。
搭建主從
主庫數(shù)據(jù)備份
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不被支持
之后重啟 主從 數(shù)據(jù)庫
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 開啟后 執(zhí)行 查看當(dāng)前數(shù)據(jù)庫狀態(tài)。會多一個 Executed_Gtid_Set 指標(biāo)
從庫還原主庫數(shù)據(jù)
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
主庫創(chuàng)建復(fù)制用戶
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)
從庫開啟復(fù)制
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 復(fù)制 不像 傳統(tǒng)的基于binlog復(fù)制。不需要 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)
文章來源:http://www.zghlxwxcb.cn/news/detail-795226.html
可以看到 Slave_IO_Running 和 Slave_SQL_Running 均為 YES,搭建成功文章來源地址http://www.zghlxwxcb.cn/news/detail-795226.html
到了這里,關(guān)于MySQL 基于 GTID 主從復(fù)制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!