Centos7搭建基於GTID的MySQL主從複製架構

簡介

主從複製(也稱 AB 複製)是未來自一個MySQL數據庫服務器(主服務器)中的數據複製到一個或多個MySQL數據庫服務器(從服務器)中
GTID
從MySQL 5.6.5 開始新增了一種基於 GTID 的主從複製方式,GTID(Global Transaction ID)是全局事務ID,經過 GTID能夠保證每個在主庫中提交的事務在整個數據庫集羣中有一個惟一的ID,所以當在主庫上提交事務或者被從庫應用時,能夠經過ID定位和追蹤每個事務,不用再經過手工去能夠找偏移量的值,這種方式強化了數據庫的主備一致性,故障恢復以及容錯能力。mysql

準備Centos7設備

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

hostname ip
master 192.168.29.132
bak 192.168.29.138

配置MySQL

添加主從複製帳號
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;

修改master結點的配置文件/etc/my.cnf數據庫

[root@master ~]# 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

備份master主機的數據並導入到bak主機中服務器

在master結點執行備份命令,並傳到bak結點中多線程

[root@master ~]#  mysqldump -u root -p  -A > mydb.sql
[root@master ~]# scp mydb.sql root@bak:/tmp/

在bak結點中建立數據庫並進行數據導入ide

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

配置bak主機的配置文件測試

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

啓動bak結點的MySQL服務並進行主從配置線程

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

參數含義:
    master_host:主機IP地址
    master_user:主從複製遠程主機用戶
    master_password:主從複製遠程主機用戶的密碼
    master_auto_position:自動尋找偏移量
mysql>start slave;
mysql>show slave status\G;

查看bak主機中slave狀態

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

測試驗證

master結點操做數據庫

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

bak結點查看數據

mysql> select * from mydb.test;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
相關文章
相關標籤/搜索