溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

初識(shí)MariaDB之8——GTID主從復(fù)制

發(fā)布時(shí)間:2020-07-03 20:46:44 來源:網(wǎng)絡(luò) 閱讀:19989 作者:qiao645 欄目:MySQL數(shù)據(jù)庫

一、背景介紹

MySQL5.6之前,主從復(fù)制是通過binlog和position實(shí)現(xiàn)的,當(dāng)A主機(jī)宕機(jī)后,B主機(jī)成為新的主節(jié)點(diǎn),此時(shí)在C主機(jī)上需要使用sql語句:CHANGE MASTER TO MASTER_HOST='xxx', MASTER_LOG_FILE='xxx', MASTER_LOG_POS='xxx';將自己的復(fù)制源指向B主機(jī),難點(diǎn)在于,同一個(gè)事務(wù)在每臺(tái)機(jī)器上的binlog名字和位置都不一樣,怎么找到C主機(jī)當(dāng)前同步停止點(diǎn)在B主機(jī)上的master_log_file和master_log_pos位置就成了問題

初識(shí)MariaDB之8——GTID主從復(fù)制初識(shí)MariaDB之8——GTID主從復(fù)制

于是MySQL在5.6.2之后產(chǎn)生了GTID,即全局事務(wù)ID(global transaction ID),其形式為:DomainID-ServerID-TransactionID,在配置是必須確保每個(gè)MySQL服務(wù)器的server_id都不相同,同一GTID事務(wù)在每個(gè)節(jié)點(diǎn)上都是相同的。

二、GTID與binlog

需要注意的是Mariadb的gtid配置方式與MySQL不相同,二者之間不兼容。MaraDB在10.0.2之后的版本默認(rèn)是啟用的,即使從服務(wù)器使用的是binlog和position進(jìn)行主從復(fù)制,但他仍是采用GTID進(jìn)行追蹤

初識(shí)MariaDB之8——GTID主從復(fù)制初識(shí)MariaDB之8——GTID主從復(fù)制

這就意味著原先的slave配置可以簡單的切換到GTID模式

STOP SLAVE;

CHANGE MASTER TO master_host='xxxx', master_port=3306, master_user='xxx',master_password='xxx',master_use_gtid=current_pos;

START SLAVE;

而從GTID模式切換回以前的binlog模式也不復(fù)雜

STOP SLAVE;

CHANGE MASTER TO MASTER_HOST='xxx', MASTER_LOG_FILE='xxx', MASTER_LOG_POS='xxx';

START SLAVE;

三、master_use_gtid介紹

master_use_gtid = { slave_pos | current_pos | no }有3種選項(xiàng):

slave_pos:slave將Master最后一個(gè)GTID的position復(fù)制到本地,Slave主機(jī)可通過gtid_slave_pos變量查看最后一個(gè)GTID的position

current_pos:假設(shè)有AB兩臺(tái)主機(jī),A是Master,當(dāng)A故障后,B成為Master,A修復(fù)后以Slave的身份重新添加,A之前從沒擔(dān)任過slave角色,所以沒有之前復(fù)制的GTID號(hào),此時(shí)gtid_slave_pos為空,為了能讓A能自動(dòng)添加為Slave,此時(shí)就用到該選項(xiàng)。該選項(xiàng)是大多數(shù)情況下使用的選項(xiàng),因?yàn)樗唵我子猛?,不必在意服?wù)器之前是Master還是Slave角色。但要注意不要讓從服務(wù)器在binlog日志中寫入事務(wù)。

建議在服務(wù)器上啟用gtid_strict_mode,這樣非Master產(chǎn)生的事物將被拒絕。如果從服務(wù)器沒有開啟binlog上面兩種方式等價(jià)。

no:關(guān)閉GTID功能

四、環(huán)境及Maradb配置介紹

本次實(shí)驗(yàn)采用CentOS7.4,數(shù)據(jù)庫版本為MariaDB-10.2.14,拓?fù)淙缦聢D所示:

初識(shí)MariaDB之8——GTID主從復(fù)制初識(shí)MariaDB之8——GTID主從復(fù)制

本次模擬當(dāng)A主機(jī)故障后C主機(jī)將Master主機(jī)重新指向B主機(jī),并且當(dāng)A主機(jī)修復(fù)后以Slave的身份重新加入集群

三、操作步驟

1.3臺(tái)服務(wù)器安裝MariaDB-10.2.14(略)

2.A主機(jī)操作

(1)編輯配置文件

[root@host3 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

skip_name_resolve=ON

relay_log=mysql-relaylog

relay_log_index=mysql-relaylog

relay_log_purge=OFF

slow_query_log=ON

server-id=10

innodb_file_per_table=ON

binlog_format=ROW

log_bin=mysql-binlog

log_slave_updates=ON

gtid_strict_mode=ON

(2)啟動(dòng)并進(jìn)入MySQL

[root@host3 ~]# systemctl start mariadb.service

[root@host3 ~]# mysql

(3)創(chuàng)建一個(gè)用于主從復(fù)制的賬號(hào)

MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';

MariaDB [(none)]> flush privileges;

(4)備份當(dāng)前數(shù)據(jù)庫并發(fā)送給B主機(jī)

[root@host3 ~]# mysqldump -uroot --single-transaction  --databases=hellodb --masterdata=2 --quick > /tmp/hello.sql

[root@host3 ~]# scp -r /tmp/hellodb.sql root@172.16.10.40:/tmp

(5)之后做任意DML操作,查看當(dāng)前 gtid_binlog_pos

MariaDB [hellodb]> show global variables like 'gtid%';

+------------------------+---------+

| Variable_name          | Value   |

+------------------------+---------+

| gtid_binlog_pos        | 0-10-40 |

| gtid_binlog_state      | 0-10-40 |

| gtid_current_pos       | 0-10-40 |

| gtid_domain_id         | 0       |

| gtid_ignore_duplicates | OFF     |

| gtid_slave_pos         |         |

| gtid_strict_mode       | ON      |

+------------------------+---------+

3.B主機(jī)操作(以備份形式復(fù)制)

(1)編輯配置文件

[root@host4 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

skip_name_resolve=ON

relay_log=mysql-relaylog

relay_log_index=mysql-relaylog

relay_log_purge=OFF

slow_query_log=ON

server-id=20

innodb_file_per_table=ON

binlog_format=ROW

log_bin=mysql-binlog

log_slave_updates=ON

gtid_strict_mode=ON

(2)查看備份時(shí)A主機(jī)的gtid_slave_pos位置

[root@host4 ~]# cat /tmp/hello.sql

初識(shí)MariaDB之8——GTID主從復(fù)制初識(shí)MariaDB之8——GTID主從復(fù)制

(3)登陸MySQL,創(chuàng)建一個(gè)用于主從復(fù)制的賬號(hào)

[root@host4 ~]# systemctl start mariadb.service

[root@host4 ~]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';

MariaDB [(none)]> flush privileges;

(4)以備份方式還原并同步數(shù)據(jù)庫

MariaDB [(none)]> source /tmp/hellodb.sql;

MariaDB [(none)]> SET GLOBAL gtid_slave_pos = '0-10-38';

MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.30', master_port=3306, master_user='bak', master_password='bakpass',master_use_gtid=slave_pos;

MariaDB [(none)]> start slave;

(5)驗(yàn)證效果

MariaDB [hellodb]> show global variables like 'gtid%';

+------------------------+-----------------+

| Variable_name          | Value           |

+------------------------+-----------------+

| gtid_binlog_pos        | 0-10-40         |

| gtid_binlog_state      | 0-20-37,0-10-40 |

| gtid_current_pos       | 0-10-40         |

| gtid_domain_id         | 0               |

| gtid_ignore_duplicates | OFF             |

| gtid_slave_pos         | 0-10-40         |

| gtid_strict_mode       | ON              |

+------------------------+-----------------+

可以看到gtid_binlog_pos已經(jīng)和A主機(jī)保持一致,之前導(dǎo)入數(shù)據(jù)庫和創(chuàng)建復(fù)制賬號(hào)所以B主機(jī)上gtid_binlog_state有2個(gè)值,官方建議開啟gtid_strict_mode選項(xiàng)或臨時(shí)禁用sql_log_bin

4.設(shè)置C服務(wù)器(以新服務(wù)器方式同步)

(1)編輯配置文件(該服務(wù)器只作為Slave角色binlog可以不要)

[root@host5 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

skip_name_resolve=ON

relay_log=mysql-relaylog

relay_log_index=mysql-relaylog

relay_log_purge=OFF

slow_query_log=ON

server-id=20

innodb_file_per_table=ON

binlog_format=ROW

log_bin=mysql-binlog

log_slave_updates=ON

gtid_strict_mode=ON

(2)啟動(dòng)并進(jìn)入MySQL

[root@host5 ~]# systemctl start mariadb.service

[root@host5 ~]# mysql

(3)以空服務(wù)器進(jìn)行數(shù)據(jù)庫同步恢復(fù)

MariaDB [(none)]> SET GLOBAL gtid_slave_pos = "";

MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.30', master_port=3306, master_user='bak', master_password='bakpass',master_use_gtid=current_pos;

MariaDB [(none)]> start slave;

(4)驗(yàn)證效果

MariaDB [(none)]> show global variables like 'gtid%';

+------------------------+---------+

| Variable_name          | Value   |

+------------------------+---------+

| gtid_binlog_pos        | 0-10-40 |

| gtid_binlog_state      | 0-10-40 |

| gtid_current_pos       | 0-10-40 |

| gtid_domain_id         | 0       |

| gtid_ignore_duplicates | OFF     |

| gtid_slave_pos         | 0-10-40 |

| gtid_strict_mode       | ON      |

+------------------------+---------+

===================以上完成主從環(huán)境搭建=========================

5.停止A主機(jī)MySQL服務(wù),模擬故障

[root@host3 ~]# systemctl stop mariadb.service

6.此時(shí)將B主機(jī)提升為Master主機(jī),并進(jìn)行操作

MariaDB [hellodb]> stop slave;

MariaDB [hellodb]> show global variables like 'gtid%';

+------------------------+-----------------+

| Variable_name          | Value           |

+------------------------+-----------------+

| gtid_binlog_pos        | 0-10-40         |

| gtid_binlog_state      | 0-20-37,0-10-40 |

| gtid_current_pos       | 0-10-40         |

| gtid_domain_id         | 0               |

| gtid_ignore_duplicates | OFF             |

| gtid_slave_pos         | 0-10-40         |

| gtid_strict_mode       | ON              |

+------------------------+-----------------+

MariaDB [hellodb]> delete from students where stuid=21;

MariaDB [hellodb]> show global variables like 'gtid%';

+------------------------+-----------------+

| Variable_name          | Value           |

+------------------------+-----------------+

| gtid_binlog_pos        | 0-20-41         |

| gtid_binlog_state      | 0-10-40,0-20-41 |

| gtid_current_pos       | 0-20-41         |

| gtid_domain_id         | 0               |

| gtid_ignore_duplicates | OFF             |

| gtid_slave_pos         | 0-10-40         |

| gtid_strict_mode       | ON              |

+------------------------+-----------------+

7.將C主機(jī)的Master有A指向B,并觀察變化

MariaDB [(none)]> show global variables like 'gtid%';

+------------------------+---------+

| Variable_name          | Value   |

+------------------------+---------+

| gtid_binlog_pos        | 0-10-40 |

| gtid_binlog_state      | 0-10-40 |

| gtid_current_pos       | 0-10-40 |

| gtid_domain_id         | 0       |

| gtid_ignore_duplicates | OFF     |

| gtid_slave_pos         | 0-10-40 |

| gtid_strict_mode       | ON      |

+------------------------+---------+

MariaDB [(none)]> stop slave;

MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.40', master_port=3306, master_user='bak', master_password='bakpass',master_use_gtid=current_pos;

MariaDB [(none)]> start slave;

MariaDB [(none)]> show global variables like 'gtid%';

+------------------------+-----------------+

| Variable_name          | Value           |

+------------------------+-----------------+

| gtid_binlog_pos        | 0-20-41         |

| gtid_binlog_state      | 0-10-40,0-20-41 |

| gtid_current_pos       | 0-20-41         |

| gtid_domain_id         | 0               |

| gtid_ignore_duplicates | OFF             |

| gtid_slave_pos         | 0-20-41         |

| gtid_strict_mode       | ON              |

+------------------------+-----------------+

此時(shí)C主機(jī)的gtid_slave_pos已發(fā)生改變

MariaDB [(none)]> show variables like 'gtid_slave_pos';

+----------------+---------+

| Variable_name  | Value   |

+----------------+---------+

| gtid_slave_pos | 0-20-41 |

+----------------+---------+

8.再將A主機(jī)以Slave的身份加入集群操作

[root@host3 tmp]# systemctl start mariadb.service

[root@host3 tmp]# mysql

MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.40', master_port=3306, master_user='bak', master_password='bakpass',master_use_gtid=current_pos;

MariaDB [(none)]> show variables like 'gtid_slave_pos';

+----------------+---------+

| Variable_name  | Value   |

+----------------+---------+

| gtid_slave_pos | 0-20-41 |

+----------------+---------+

至此全部操作完成

補(bǔ)充說明:

1.master_use_gtid = { slave_pos | current_pos }具體用哪一種仍不是很明白,文檔說current_pos適用于大部分環(huán)境,所以后期使用都是用該值。

2.Master和Candidate之間可以采用半同步方式降低數(shù)據(jù)不一致

3.雙主模式下1臺(tái)采用binlog1臺(tái)采用GTID可以正常工作


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI