MySQL 5.6.5開始支持的,全局事務(wù)標(biāo)識符(GTID(Global Transaction ID))是創(chuàng)建的唯一標(biāo)識符,并與在源(主)服務(wù)器上提交的每個(gè)事務(wù)相關(guān)聯(lián)。
此標(biāo)識符不但是唯一的,而且在給定復(fù)制設(shè)置中的所有服務(wù)器上都是唯一的。
所有交易和所有GTID之間都有一對一的映射關(guān)系 。
它由服務(wù)器ID以及事務(wù)ID組合而成。
這個(gè)全局事務(wù)ID不僅僅在原始服務(wù)器上唯一,在所有存在主從關(guān)系 的mysql服務(wù)器上也是唯一的。
正是因?yàn)檫@樣一個(gè)特性使得mysql的主從復(fù)制變得更加簡單,以及數(shù)據(jù)庫一致性更可靠。
一個(gè)GTID在一個(gè)服務(wù)器上只執(zhí)行一次,避免重復(fù)執(zhí)行導(dǎo)致數(shù)據(jù)混亂或者主從不一致。
保證同一個(gè)事務(wù)在某slave上絕對只執(zhí)行一次,沒有執(zhí)行過的gtid事務(wù)總是會(huì)被執(zhí)行。
不用像傳統(tǒng)復(fù)制那樣保證binlog的坐標(biāo)準(zhǔn)確,因?yàn)楦静恍枰猙inlog以及坐標(biāo)。
故障轉(zhuǎn)移到新的master的時(shí)候很方便,簡化了很多任務(wù)。
很容易判斷master和slave的數(shù)據(jù)是否一致。只要master上提交的事務(wù)在slave上也提交了,那么一定是一致的。
1.當(dāng)一個(gè)事務(wù)在主庫端執(zhí)行并提交時(shí),產(chǎn)生GTID,一同記錄到binlog日志中。
2.binlog傳輸?shù)絪lave,并存儲(chǔ)到slave的relaylog后,讀取這個(gè)GTID的這個(gè)值設(shè)置gtid_next變量,即告訴Slave,下一個(gè)要執(zhí)行的GTID值。
3、sql線程從relay log中獲取GTID,然后對比slave端的binlog是否有該GTID。
4、如果有記錄,說明該GTID的事務(wù)已經(jīng)執(zhí)行,slave會(huì)忽略。
5、如果沒有記錄,slave就會(huì)執(zhí)行該GTID事務(wù),并記錄該GTID到自身的binlog,在讀取執(zhí)行事務(wù)前會(huì)先檢查其他session持有該GTID,確保不被重復(fù)執(zhí)行。
6、在解析過程中會(huì)判斷是否有主鍵,如果有就用二級索引,如果沒有就用全部掃描。
1.更簡單的實(shí)現(xiàn)故障轉(zhuǎn)移(failover),不需要找log_file,log_pos
2.更簡單的搭建主從復(fù)制
3.更加安全
4.GTID是連續(xù)沒有空洞的,因此主數(shù)據(jù)庫發(fā)生沖突時(shí),可以添加空事件的方式進(jìn)行跳過
啟動(dòng)前,先關(guān)閉master的寫入,保證master端和slave端數(shù)據(jù)保持同步,所有slave需要加上skip_slave_start=1的配置參數(shù),避免啟動(dòng)后還是使用之前的復(fù)制協(xié)議
centos8.0 ip:192.168.136.239 有數(shù)據(jù) hostname:mysql01
centos8.0 ip:192.168.136.219 無數(shù)據(jù) hostname:mysql02
#二進(jìn)制安裝以及mysql自啟動(dòng)服務(wù)略
mysql> grant replication slave on *.* to 'slave'@'192.168.136.219' identified by 'slave'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #倆服務(wù)器均關(guān)閉防火墻 [root@mysql01 ~]# systemctl stop firewalld [root@mysql01 ~]# setenforce 0 [root@mysql02 ~]# systemctl stop firewalld [root@mysql02 ~]# setenforce 0 從庫測試連接: [root@mysql02 ~]# mysql -u slave -p'slave' -h192.168.136.239 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
1.對主庫進(jìn)行鎖表 mysql> flush tables with read lock; 2.對主庫進(jìn)行全備 [root@mysql01 ~]# mysqldump -uroot -A > /clq/all-databases-20210519.sql 3.拷貝到從庫主機(jī)上去 [root@mysql01 ~]# scp /clq/all-databases-20210519.sql root@192.168.136.219:/backup/ [root@mysql02 backup]# ll -rw-r--r--. 1 root root 873527 5月 19 16:40 all-databases-20210519.sql 4.從庫上進(jìn)行主庫的恢復(fù) [root@mysql02 backup]# mysql -uroot -pHuawei0917@ all-databases-20210519.sql
[mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables log-bin = master_bin #開啟主庫日志 server-id = 10 #服務(wù)唯一標(biāo)識id gtid-mode = on #GTID模式開啟 enforce_gtid_consistency = on #強(qiáng)制gtid模式一致性 log-slave-updates = 1 #從庫允許更新日志,同步操作日志 binlog_format = row #binlog日志格式為行格式, 默認(rèn)是mixed混合模式 skip_slave_start = 1 #跳過從庫開啟,以主庫開始開啟 #重啟 systemctl restart mysqld
[root@mysql02 data]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables gtid_mode=on enforce_gtid_consistency=on server-id=20 log-bin=slave_binlog #開啟從庫日志 log_slave-updates=1 #從庫允許更新 binlog_format=row #格式為行 skip-slave_start=1 #重啟 systemctl restart mysqld
查看gtid狀態(tài)情況
mysql> show variables like '%gtid%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 8 rows in set (0.00 sec)
#從庫上root登錄配置 #help change master to 可以查看幫助文檔實(shí)例 mysql> change master to -> master_host='192.168.136.239', -> master_user='slave', -> master_password='slave', -> master_port=3306, #主庫端口 -> master_auto_position=1; #位置 #master_use_gtid = current_pos Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; Slave_IO_Running: Connecting Slave_IO_Running: Yes Slave_SQL_Running: Yes 保證系統(tǒng)一致性 授權(quán)一致性
(一主一從GTID)測試:
主庫創(chuàng)建一個(gè)數(shù)據(jù)庫test,進(jìn)行測試查看
從庫創(chuàng)建一個(gè)數(shù)據(jù)庫test02,進(jìn)行測試查看
#主庫創(chuàng)建一個(gè)test數(shù)據(jù)庫 mysql> create database test; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ #從庫上查看同步情況 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 6 rows in set (0.00 sec) #從庫創(chuàng)建test02庫 mysql> create database test02; Query OK, 1 row affected (0.00 sec) #主庫上查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | #是沒有test02庫的 | test | +--------------------+ 5 rows in set (0.00 sec)
小結(jié):主庫上的數(shù)據(jù)操作會(huì)同步到從庫上面去,而從庫上的數(shù)據(jù)操作與主庫沒聯(lián)系
第三臺(tái)mysql連接的話,相應(yīng)配置
第3臺(tái)mysql ,版本:centos8 ip:192.168.136.230 主機(jī)名:mysql03
[root@mysql03 ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables # replication config log-bin = master_bin server-id = 21 #id必須與之前不同 gtid-mode = on enforce-gtid-consistency = on log-slave-updates = 1 binlog-format = row skip-slave-start = 1 #查看gtid情況 mysql> show variables like '%gtid%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ #由于之前只權(quán)限了一個(gè)ip,此刻在mysql01主數(shù)據(jù)庫上再授權(quán)一個(gè)ip mysql> grant replication slave on *.* to 'slave'@'192.168.136.230' identified by 'slave'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #測試連接 [root@mysql ~]# mysql -uslave -pslave -h192.168.136.239 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 17 Server version: 5.7.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, 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> #mysql03從庫上root用戶連接進(jìn)行相應(yīng)配置 [root@mysql03 ~]# mysql -uroot -p1 mysql> change master to -> master_host='192.168.136.239', #主庫ip -> master_user='slave', #主庫授權(quán)的普通用戶 -> master_password='slave', -> master_port=3306, #主庫端口 -> master_auto_position=1; #位置從1開始同步 #也可以查看幫助進(jìn)行配置 mysql> help change master to; CHANGE MASTER TO MASTER_HOST='source2.example.com', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_PORT=3306, MASTER_LOG_FILE='source2-bin.001', MASTER_LOG_POS=4, MASTER_CONNECT_RETRY=10; URL: https://dev.mysql.com/doc/refman/5.7/en/change-master-to.html #開啟 mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.136.239 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master_bin.000002 Read_Master_Log_Pos: 2172 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 2387 Relay_Master_Log_File: master_bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes #顯示倆個(gè)yes則運(yùn)行成功! #mysql03查看數(shù)據(jù)庫,數(shù)據(jù)庫內(nèi)容也同步成功 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)
版本 | ip | 主機(jī)名 | 身份 |
---|---|---|---|
centos8 | 192.168.136.239 | master01 | 主庫 |
centos8 | 192.168.136.219 | master02 | 主庫 |
centos8 | 192.168.136.230 | slave | 從庫 |
[root@master01 ~]# systemctl stop firewalld [root@master01 ~]# systemctl disable firewalld [root@master02 ~]# systemctl stop firewalld [root@master02 ~]# systemctl disable firewalld [root@slave ~]# systemctl stop firewalld [root@slave ~]# systemctl disable firewalld
mysql> grant replication slave on *.* to 'user'@'192.168.136.%' identified by 'user';
[root@slave ~]# mysql -uuser -p'user' -h192.168.136.239 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.33 MySQL Community Server (GPL) Copyright (c) 2000, 2021, 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> grant replication slave on *.* to 'app'@'192.168.136.%' identified by 'app'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
[root@slave ~]# mysql -uapp -papp -h192.168.136.219 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 3 Server version: 5.7.33 MySQL Community Server (GPL) Copyright (c) 2000, 2021, 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>
#master01主機(jī): [root@master01 ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve skip-grant-tables log-bin = master_bin server-id = 10 gtid-mode = on enforce-gtid-consistency = on log-slave-updates = 1 binlog-format = row skip-slave-start = 1 #master02主機(jī) [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #replication config log-bin = master_bin server-id = 11 gtid-mode = on enforce-gtid-consistency = on log-slave-updates = 1 binlog-format = row skip-slave-start = 1 #slave主機(jī) [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve log-bin = slave_bin server-id = 13 gtid-mode = on enforce-gtid-consistency = on log-slave-updates = 1 binlog-format = row skip-slave-start = 1
[root@master01 ~]# systemctl restart mysqld [root@master02 ~]# systemctl restart mysqld [root@slave ~]# systemctl restart mysqld
channel
(頻道):每一個(gè)channel都是一個(gè)獨(dú)立的slave服務(wù),都有一個(gè)IO_THREAD和SQL_THREAD,原理和普通復(fù)制一樣,只是需要在change master to語句后面使用FOR Channel來進(jìn)行區(qū)分slave
在使用channel時(shí)需要將從庫的master-info-repository、relay-log-info-repository設(shè)置為table,否則會(huì)報(bào)錯(cuò)。
將信息存儲(chǔ)庫設(shè)置為table格式
方式一(mysql內(nèi)設(shè)置): set global master_info_repository='table'; set global relay_log_info_repository='table'; 方式二(/etc/my.cnf內(nèi)設(shè)置): 3.在my.cnf中設(shè)置 master_info_repository = TABLE relay_log_info_repository = TABLE #檢查是否更改成功 mysql> show variables where variable_name in ('relay_log_info_repository','master_info_repository'); +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | master_info_repository | TABLE | | relay_log_info_repository | TABLE | +---------------------------+-------+
#slave從庫上配置倆個(gè)主庫GTID復(fù)制 mysql> change master to -> master_host='192.168.136.219', #mysql02主庫ip -> master_user='app', #mysql02主庫授權(quán)的普通用戶 -> master_password='app', #mysql02主庫授權(quán)的普通用戶密碼 -> master_port=3306, #主庫端口 -> master_auto_position=1 for channel 'master01'; #位置從1開始同步,并且第一個(gè)slave取名master01 mysql> change master to -> master_host='192.168.136.239', #mysql01主庫ip -> master_user='user', -> master_password='user', -> master_port=3306, #主庫端口 -> master_auto_position=1 for channel 'master02'; #位置從1開始同步,并且第一個(gè)slave取名master01 #查看倆個(gè)slave狀態(tài) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.136.219 Master_User: app Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Read_Master_Log_Pos: 4 Relay_Log_File: slave02-relay-bin-master1.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: Slave_IO_Running: No Slave_SQL_Running: No #都是關(guān)閉的 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: 0 Relay_Log_Space: 154 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: NULL 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: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: b4326a77-0a31-11ec-a991-000c298d3571:1-2, d68b404d-0a35-11ec-9df1-000c29581959:1 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Master_Host: 192.168.136.239 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Read_Master_Log_Pos: 4 Relay_Log_File: slave02-relay-bin-master2.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: Slave_IO_Running: No Slave_SQL_Running: No 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: 0 Relay_Log_Space: 154 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: NULL 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: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: b4326a77-0a31-11ec-a991-000c298d3571:1-2, d68b404d-0a35-11ec-9df1-000c29581959:1 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master2 Master_TLS_Version: 2 rows in set (0.00 sec) #開啟倆個(gè)slave mysql> start slave; #再次查看狀態(tài)
#master01主庫創(chuàng)建一個(gè)test數(shù)據(jù)庫 mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) #master02主庫上查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | #沒有內(nèi)容 +--------------------+ 4 rows in set (0.00 sec) #slave從庫查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | #已經(jīng)同步了test庫 +--------------------+ 5 rows in set (0.00 sec) #mysql02主庫創(chuàng)建一個(gè)RHCA數(shù)據(jù)庫 mysql> create database RHCA; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | RHCA | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) #slave從庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | RHCA | | mysql | | performance_schema | | sys | #有了mysql01主庫的test庫和mysql02的RHCA的庫 | test | +--------------------+ 6 rows in set (0.00 sec)
show slave status; //查看全部slave狀態(tài)
show slave status for channel ‘naem'; //查看單個(gè)slave狀態(tài)
reset slave; #重置全部slave
reset slave for channel ‘master1'; #重置單個(gè)slave
stop slave for channel ‘master1'; #暫停單個(gè)slave
start slave for channel ‘master1'; #開啟單個(gè)slave
雖然我在做的過程沒有遇到錯(cuò)誤,但是下面這個(gè)是最最容易出現(xiàn)的錯(cuò)誤
配置完開啟slave出現(xiàn)報(bào)錯(cuò)
mysql> start slave; ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
由于mysql.slave_relay_log_info表中保留了以前的復(fù)制信息,導(dǎo)致新從庫啟動(dòng)時(shí)無法找到對應(yīng)文件,那么我們清理掉該表中的記錄即可
mysql> reset slave; Query OK, 0 rows affected (0.00 sec)
以上就是MySQL示例DTID主從原理解析的詳細(xì)內(nèi)容,更多關(guān)于MySQL示例DTID主從原理的資料請關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:無錫 來賓 汕尾 七臺(tái)河 寶雞 邯鄲 營口 西寧
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL示例DTID主從原理解析》,本文關(guān)鍵詞 MySQL,示例,DTID,主從,原理,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。