MySQL基于GTID的主從復(fù)制-創(chuàng)新互聯(lián)

1、什么是GTID?

創(chuàng)新互聯(lián)專注于澄江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供澄江營(yíng)銷型網(wǎng)站建設(shè),澄江網(wǎng)站制作、澄江網(wǎng)頁(yè)設(shè)計(jì)、澄江網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造澄江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供澄江網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。1、全局唯一,一個(gè)事務(wù)對(duì)應(yīng)一個(gè)GTID 2、替代傳統(tǒng)的binlog+pos復(fù)制;使用master_auto_position=1自動(dòng)匹配GTID斷點(diǎn)進(jìn)行復(fù)制 3、MySQL5.6開始支持 4、在傳統(tǒng)的主從復(fù)制中,slave端不用開啟binlog;但是在GTID主從復(fù)制中,必須開啟binlog 5、slave端在接受master的binlog時(shí),會(huì)校驗(yàn)GTID值 6、為了保證主從數(shù)據(jù)的一致性,多線程同時(shí)執(zhí)行一個(gè)GTID

2、組成

Master_UUID:序列號(hào)

舉例:

ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5
ceb0ca3d-8366-11e8-ad2b-000c298b7c9a其實(shí)就是master的uuid值;1-5是序列號(hào),每次一個(gè)事務(wù)完成都會(huì)自增1,也就是說(shuō)下一次為1-6。

3、工作原理

1、master更新數(shù)據(jù)時(shí),會(huì)在事務(wù)前產(chǎn)生GTID,一同記錄到binlog日志中。 2、slave端的i/o 線程將變更的binlog,寫入到本地的relay log中。 3、sql線程從relay log中獲取GTID,然后對(duì)比slave端的binlog是否有記錄。 4、如果有記錄,說(shuō)明該GTID的事務(wù)已經(jīng)執(zhí)行,slave會(huì)忽略。 5、如果沒有記錄,slave就會(huì)從relay log中執(zhí)行該GTID的事務(wù),并記錄到binlog。 6、在解析過程中會(huì)判斷是否有主鍵,如果沒有就用二級(jí)索引,如果沒有就用全部掃描

4、GTID主從配置

版本:MySQL5.7

配置master

vim /etc/my.cnf [client] socket=/usr/local/mysql/mysql.sock [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data user=mysql pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/usr/local/mysql/mysql.sock port=3306 server-id=1 gtid-mode=ON enforce-gtid-consistency=ON server-id=1 binlog_format=row log-bin=/usr/local/mysql/data/mysql-bin systemctl restart mysqld firewall-cmd --add-port=3306/tcp --permanent firewall-cmd --reload

配置slave

vim /etc/my.cnf [client] socket=/usr/local/mysql/mysql.sock [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data user=mysql pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/usr/local/mysql/mysql.sock port=3306 server-id=2 gtid-mode=ON enforce-gtid-consistency=ON server-id=2 binlog_format=ROW log-bin=/usr/local/mysql/data/mysql-bin log_slave_updates=ON skip-slave-start=1 systemctl restart mysqld firewall-cmd --add-port=3306/tcp --permanent firewall-cmd --reload

master授權(quán)配置

mysql -uroot -p mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123'; mysql> flush privileges;

slave配置同步

mysql -uroot -p mysql> change master to master_host='10.0.0.132', master_user='rep',master_password='123',master_port=3306,master_auto_position=1; mysql> start slave;

查看slave的狀態(tài)

mysql> show slave status\G; *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: 10.0.0.132                   Master_User: rep                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: mysql-bin.000003           Read_Master_Log_Pos: 635                Relay_Log_File: slave-relay-bin.000005                 Relay_Log_Pos: 848         Relay_Master_Log_File: mysql-bin.000003              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: 635               Relay_Log_Space: 1308               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: 1                   Master_UUID: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a              Master_Info_File: /usr/local/mysql/data/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: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4             Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4                 Auto_Position: 1          Replicate_Rewrite_DB:                   Channel_Name:             Master_TLS_Version:  1 row in set (0.00 sec)

MySQL基于GTID的主從復(fù)制

出現(xiàn)這兩個(gè)yes表示同步成功

MySQL基于GTID的主從復(fù)制

通過slave的狀態(tài)信息,可以看到GTID的值、Matser_UUID等信息

查看master狀態(tài)

mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000003 |      635 |              |                  | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)

注意對(duì)比slave端,Executed_Gtid_Set的值應(yīng)該是一樣的。

5、驗(yàn)證主從

master上

mysql> create database test01; Query OK, 1 row affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000003 |      800 |              |               | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)

slave上

mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | mysql              | | performance_schema | | sys                | | test01             | +--------------------+ 5 rows in set (0.07 sec) mysql> show slave status\G; *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: 10.0.0.132                   Master_User: rep                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: mysql-bin.000003           Read_Master_Log_Pos: 800                Relay_Log_File: slave-relay-bin.000005                 Relay_Log_Pos: 1013         Relay_Master_Log_File: mysql-bin.000003              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: 800               Relay_Log_Space: 1473               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: 1                   Master_UUID: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a              Master_Info_File: /usr/local/mysql/data/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: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5             Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5                 Auto_Position: 1          Replicate_Rewrite_DB:                   Channel_Name:             Master_TLS_Version:  1 row in set (0.00 sec)

需要注意的是,GTID的值在完成一次事務(wù)后,變成了ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5(自增1)

6、排障

思路

a、確保master開放3306端口

b、最好關(guān)閉selinux

c、master上授權(quán)同步,slave上change master命令指定master的信息不要寫錯(cuò)

d、UUID問題

MySQL基于GTID的主從復(fù)制

如果你出現(xiàn)了上圖所示的問題,表示你的master和slave的UUID是一樣的,一般這種情況多出現(xiàn)于克隆虛擬機(jī)

解決辦法:

找到slave上的MySQL數(shù)據(jù)目錄下的auto.cnf文件(這個(gè)文件其實(shí)是自動(dòng)生成的mysql服務(wù)器的UUID值),將它刪除,然后重啟MySQL,然后MySQL會(huì)重新生成一個(gè)UUID。然后停掉slave,重新開啟就可以了(我的mysql的數(shù)據(jù)目錄是在/usr/local/mysql/data下,詳情查看my.cnf配置文件)

cd /usr/local/mysql/data rm -f auto.cnf systemctl restart mysql[root@slave data]# cat auto.cnf [auto] server-uuid=020c7f26-be57-11e8-8e2d-000c29b63bad

通過cat命令查看該文件,發(fā)現(xiàn)UUID已經(jīng)改變

mysql -uroot -p mysql> stop slave; mysql> start slave;

e、總結(jié)

排障過程中,注意需要停掉slave,做完修改之后在開啟,否則你的修改可能是不會(huì)生效的。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章名稱:MySQL基于GTID的主從復(fù)制-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://muchs.cn/article18/dchhdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)微信小程序、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、Google網(wǎng)頁(yè)設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)