自MySQL 5.6.5起,引進了一種基於全域事務識別碼(GTID)的複製方法。 GTID確保了在叢集中每個提交到主庫的交易都有一個唯一的識別碼。這種方式強化了資料庫的主備一致性,故障復原以及容錯能力。
GTID (Global Transaction ID) 是對於一個已提交交易的編號,並且是一個全域唯一的編號。 GTID實際上是由UUID TID 組成的。其中UUID是一個 MySQL實例的唯一識別。 TID的值隨著每個提交的交易單調遞增,記錄了該實例上已經提交的事務數量。
下面是一個GTID的具體形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23,冒號分割前邊為uuid,後邊為TID。
GTID集合可以包含來自多個MySQL實例的事務,它們之間以逗號分隔。
多個交易序號的範圍區間若來自相同MySQL實例,則應以冒號分隔各組範圍。例如:e6954592-8dba-11e6-af0e-fa163e1cf111:1-5:11-18,e6954592-8dba-11e6-af0e-fa163e1cf3f2:1-27。
在原來基於二進位日誌的複製中,從庫需要告知主庫要從哪個偏移量進行增量同步,如果指定錯誤會造成數據的遺漏,造成數據的不一致。借助GTID,在發生主備切換的情況下,MySQL的其它從庫可以自動在新主庫上找到正確的複製位置,這大大簡化了複雜複製拓撲下集群的維護,也減少了人為設定複製位置發生誤操作的風險。使用基於GTID的複製可以排除已執行的事務,從而降低資料不一致性的風險。
主庫基於gtid set可以準確的知道從庫缺少哪些數據,不會多給從庫數據,也不會少給,避免網路頻寬浪費。
mysql主從結構在一主一從情況下對GTID來說就沒有優勢了,而對於2台主以上的結構優勢異常明顯,可以在資料不遺失的情況下切換新主。
注意:在建構主從複製之前,在一台將成為主的實例上進行一些操作(如資料清理等),透過GTID複製,這些在主從成立之前的操作也會複製到從伺服器上,造成複製失敗。也就是說透過GTID複製都是從最先開始的交易日誌開始,即使這些操作在複製之前執行。例如在server1上執行一些drop、delete的清理操作,接著在server2上執行change的操作,會使得server2也進行server1的清理操作。
當一個交易在主庫端執行並提交時,產生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,確保不被重複執行。
主機規劃:
$ 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 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)
$ docker run --name mysql3313 -p 3313: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/3313/conf:/etc/mysql/conf.d -v /home/mysql/docker-data/3313/data/:/var/lib/mysql -v /home/mysql/docker-data/3313/logs/:/var/log/mysql -d mysql:5.7
mysql> change master to master_host='172.23.252.98',master_port=3310,master_user='repluser',master_password='123456',master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.02 sec)
mysql> show master status; +------------------+----------+--------------+------------------+----------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+----------------------------------------+ | mysql-bin.000008 | 154 | | | cd2eaa0a-7a59-11ec-b3b4-0242ac110002:1 | +------------------+----------+--------------+------------------+----------------------------------------+ 1 row in set (0.00 sec) mysql> show slave status\G; *************************** 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)
mysql> insert into t_order values(4,"V");
mysql> select * from order.t_order; +------+------+ | id | name | +------+------+ | 4 | V | +------+------+ 3 rows in set (0.00 sec)
mysql> insert into t_order values(5,"X");
mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> select * from order.t_order; +------+------+ | id | name | +------+------+ | 4 | V | +------+------+ 3 rows in set (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.02 sec) mysql> select * from order.t_order; +------+------+ | id | name | +------+------+ | 4 | V | | 5 | X | +------+------+ 4 rows in set (0.00 sec)
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.
mysql> show variables like 'server_id';
mysql> show variables like '%uuid%';
以上是MySQL的GTID複製怎麼應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!