本篇文章為大家帶來了關於mysql的相關知識,其中主要介紹了關於GTID複製的相關問題,GTID是對於一個已提交事務的編號,並且是一個全局唯一的編號,下面一起來看一下,希望對大家有幫助。
推薦學習:mysql影片教學
#從MySQL 5.6.5開始新增了一個基於GTID的複製方式。透過GTID保證了每個在主庫上提交的事務在叢集中都有一個唯一的ID。這種方式強化了資料庫的主備一致性,故障復原以及容錯能力。
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的清理操作。
主機規劃:
設定檔my.cnf內容如下:
$ 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實例:
$ 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)
設定檔my.cnf內容與master一致,注意修改server-id,保持唯一。
建立docker實例:
$ 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
開啟GTID同步:
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)
在master.order表插入資料:
mysql> insert into t_order values(4,"V");
發現資料已經同步至slave:
mysql> select * from order.t_order; +------+------+ | id | name | +------+------+ | 4 | V | +------+------+ 3 rows in set (0.00 sec)
先停止slave,再在master.order表插入資料:
mysql> insert into t_order values(5,"X");
然後再啟動slave,發現資料已自動同步:
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)
在slave伺服器show slave status:
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.
先檢查master和slave的server_id是否一致,如果一致去修改my.cnf檔案中的server_id欄位:
mysql> show variables like 'server_id';
然後排查master和slave的uuid是否一致:
mysql> show variables like '%uuid%';
如果uuid一致去修改data目錄下的auto.cnf文件,拷貝整個data目錄,把auto.cnf文件也拷貝過來了,裡面記錄了資料庫的uuid,每個函式庫的uuid應該是不一樣的。
推薦學習:mysql影片教學
以上是MySQL複製實例詳解之GTID複製的詳細內容。更多資訊請關注PHP中文網其他相關文章!