Centos7搭建基於GTID的MySQL的M-M-S-S架構

簡介

M-M-S-S架構即雙主雙從結點,具體架構爲兩臺主結點互爲對方的從節點,且兩臺從節點機器均有兩個主結點,可用於當其中一臺主結點服務發生故障時,有備用主結點能夠對業務進行支撐
主從複製
主從複製(也稱 AB 複製)是未來自一個MySQL數據庫服務器(主服務器)中的數據複製到一個或多個MySQL數據庫服務器(從服務器)中,部署方法可參考:https://blog.51cto.com/14832653/2500735
GTID
從MySQL 5.6.5 開始新增了一種基於 GTID 的主從複製方式,GTID (Global Transaction ID)是全局事務ID,經過 GTID能夠保證每個在主庫中提交的事務在整個數據庫集羣中有一個惟一的ID,所以當在主庫上提交事務或者被從庫應用時,能夠經過ID定位和追蹤每個事務,不用再經過手工去能夠找偏移量的值,這種方式強化了數據庫的主備一致性,故障恢復以及容錯能力,部署方法可參考:http://www.javashuo.com/article/p-uvvjifho-nr.htmlmysql

前期準備

準備四臺centos7虛擬機,配置ip和hostname,關閉防火牆和selinux
在/etc/hosts中添加主機名和IP地址映射linux

hostname ip
master1 192.168.29.132
master2 192.168.29.138
slave1 192.168.29.131
slave2 192.168.29.133

配置MySQL

兩個master節點添加主從複製帳號
MySQL5.7:sql

mysql>grant replication slave on *.* to 'repl'@'%' identified by 'your_password';
mysql>flush privileges;

MySQL8:shell

mysql>create user 'repl'@'%' identified by 'your_password';
mysql>grant replication slave on *.* to 'repl'@'%';
mysql>flush privileges;

master1主機初始化數據數據庫

mysql> create database mydb;
mysql> use mydb;
mysql> create table test(id int primary key);
mysql> insert into test values(1);
mysql> insert into test values(2);

修改兩個master結點的配置文件

master1centos

[root@master1 ~]# vi /etc/my.cnf 
[mysqld]
#設置Master主機信息
server-id=1
log-bin=binlog
#基於事務的Replication
#能夠實現基於庫的多線程複製,減小主從複製的延遲
#主機配置
gtid_mode=ON
#強制執行GTID一致性
enforce_gtid_consistency=1
#設定把master主機信息保存在表中
#默認以文件形式保存
master-info-repository=table
relay-log-info-repository=table

master2服務器

[root@master2 ~]# vi /etc/my.cnf 
[mysqld]
server-id=2
log-bin=binlog
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=table
relay-log-info-repository=table

配置兩臺slave主機的配置文件

slave1多線程

[root@slave1 ~]# vi /etc/my.cnf
[mysqld]
server-id=3
gtid_mode=ON
enforce_gtid_consistency=1 
master-info-repository=table
relay-log-info-repository=table

slave2架構

[root@slave2 ~]# vi /etc/my.cnf
[mysqld]
server-id=4
gtid_mode=ON
enforce_gtid_consistency=1 
master-info-repository=table
relay-log-info-repository=table

備份master1主機的數據並導入到其他全部主機中

master1備份數據ide

[root@master1 ~]#  mysqldump -u root -p  -A > mydb.sql
[root@master1 ~]# scp mydb.sql root@master2:/tmp/
[root@master1 ~]# scp mydb.sql root@slave1:/tmp/
[root@master1 ~]# scp mydb.sql root@slave2:/tmp/

其他結點中導入數據

[root@master2 ~]# mysql -u root -p < /tmp/mydb.sql 
[root@slave1 ~]# mysql -u root -p < /tmp/mydb.sql 
[root@slave2 ~]# mysql -u root -p < /tmp/mydb.sql

配置兩臺master結點互爲主從

master1

mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.138',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1
參數含義:
    master_host:主機IP地址
    master_user:主從複製遠程主機用戶
    master_password:主從複製遠程主機用戶的密碼
    master_auto_position:自動尋找偏移量

master2

mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.132',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1

查看兩臺主機中slave狀態

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

配置兩臺slave結點爲兩個master的從結點

slave1

mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.132',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1 for channel 'repl-master-1';
參數for channel爲指定主結點在不一樣的頻道
mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.138',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1 for channel 'repl-master-2';

slave2

mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.132',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1 for channel 'repl-master-1';
mysql>stop slave;
mysql>change master to
    ->master_host='192.168.29.138',
    ->master_user='repl',
    ->master_password='your_password',
    ->master_auto_position=1 for channel 'repl-master-2';

查看兩臺slave中的slave狀態

mysql>start slave;
mysql>show slave status\G;
#能夠看到有兩份主機信息
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

測試驗證

master1結點操做數據庫

mysql> insert into test values(1111);

master2結點操做數據庫

mysql> insert into test values(2222);

slave1和slave2結點查看數據

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
| 1111 |
| 2222 |
+------+
4 rows in set (0.00 sec)

模擬master1宕機

關閉master1的mysql服務

[root@master ~]# systemctl stop mysqld

查看其他結點的狀況

Last_IO_Error: error reconnecting to master 'repl@192.168.29.132:3306' - retry-time: 60 retries: 1 message: Can't connect to MySQL server on '192.168.29.132' (111)

master2結點操做數據庫

mysql> insert into test values(3);
mysql> insert into test values(4);

slave1和slave2結點查看數據

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
| 1111 |
| 2222 |
+------+
6 rows in set (0.00 sec)

重啓master1的mysql服務

[root@master ~]# systemctl restart mysqld
#其他結點的從節點恢復正常

master1查詢數據

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
| 1111 |
| 2222 |
+------+
6 rows in set (0.00 sec)

故障

配置過程當中報錯

Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Cannot replicate because the master purged required binary logs. Replicate the missing transactions from elsewhere, or provision a new slave from backup. Consider increasing the master's binary log expiration period. To find the missing transactions, see the master's error log or the manual for GTID_SUBTRACT
緣由是主結點數據庫進行了過多數據操做或進行太重啓等致使binlog產生了變化,從節點沒法進行同步

解決方法

在master1中進行操做後在備份數據到其餘從節點
mysql>reset master
mysql>reset slave
相關文章
相關標籤/搜索