"MySQL"-dual master replication (GTID-based replication) @20210227

brief introduction

The so-called "dual master replication" is actually "mutual master-slave replication". That is, each master host has both a master role and a slave role. In this way, the changes made by any party will be replicated to the database of another host through the master and slave to achieve data consistency. This is "dual master replication".

There are two ways of database replication: GTID-based replication; and binary log location-based replication. The former "GTID-based replication" discussed in this article.

System environment

   
operating system: CentOS Linux release 7.4.1708 (Core)
Software version: MySQL 5.6.45
Internet Information: master-01: 10.10.50.115
  master-02: 10.10.50.116

Install MySQL service

(Skip, refer to the article "MySQL 5.6 (CentOS 7)" for details)

Configuration service

# On master-01

#!/bin/sh 
########################################## ################################### 
# (0) Create the necessary directories: 
##### ############################################## ######################## 
mkdir -pv /var/log/mysql/ 
chown mysql.mysql /var/log/mysql/ 

#### ############################################## ######################### 
# (1) Modify the /etc/mysql/my.cnf file: 
#! ! ! Different distributions have different configuration locations, please don't copy them! Please don't copy it! Please don't copy it! 
############################################## ############################# 
mv /etc/mysql/my.cnf /etc/mysql/my.cnf.backup 
cat <<MYCNF >> /etc/mysql/my.cnf 
server_id = 1 
auto-increment-offset = 1 
auto-increment-increment = 2
 
log_bin = mysql-bin
binlog_format = MIXED 
enforce_gtid_consistency = 1 
log_slave_updates = ON 
gtid_mode = ON 
MYCNF 

################################### ########################################## 
# (2) Restart the service : 
############################################## ############################## 
systemctl restart mysqld.service

# On master-02

#!/bin/sh 
########################################## ################################### 
# (0) Create the necessary directories: 
##### ############################################## ######################## 
mkdir -pv /var/log/mysql/ 
chown mysql.mysql /var/log/mysql/ 

#### ############################################## ######################### 
# (1) Modify the /etc/mysql/my.cnf file: 
#! ! ! Different distributions have different configuration locations, please don't copy them! Please don't copy it! Please don't copy it! 
############################################## ############################# 
mv /etc/mysql/my.cnf /etc/mysql/my.cnf.backup 
cat <<MYCNF >> /etc/mysql/my.cnf 
server_id = 2 
auto-increment-offset = 2 
auto-increment-increment = 2
 
log_bin = mysql-bin
binlog_format = MIXED 
enforce_gtid_consistency = 1 
log_slave_updates = ON 
gtid_mode = ON 
MYCNF 

################################### ########################################## 
# (2) Restart the service : 
############################################## ############################## 
systemctl restart mysqld.service

Create user for replication

Create users for replication on master-01 and master-02 respectively. After creating a user, don't forget to verify whether the user can log in normally.

# On master-01

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'BU3CGK6x';

# On master-02

-- SQL
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'BU3CGK6x';

Enable replication

Respectively enable replication on master-01 and master-02:

# On master-01

-- SQL --

STOP SLAVE;

CHANGE MASTER TO master_host='10.10.50.116', master_port=3306,
    master_user='replication', master_password='BU3CGK6x',
    master_auto_position=1;

START SLAVE;

# On master-02

-- SQL --

STOP SLAVE;

CHANGE MASTER TO master_host='10.10.50.115', master_port=3306,
    master_user='replication', master_password='BU3CGK6x',
    master_auto_position=1;

START SLAVE;

Copy verification

The verification process is not detailed here. Because it is relatively simple, here is only an overview of the idea of ​​verifying whether the copy works:

	Create libraries and tables on master-01, and then view them on master-02. 
	Create libraries and tables on master-02, and then view them on master-01.

If there is no problem, there is no problem. If there is no synchronization, you can execute the SHOW SLAVE STATUS \G statement on the database to view the synchronization status and synchronization error information (if any).

Check status

Use SHOW MASTER STATUS \G statement to view your own status; use SHOW SLAVE STATUS \G statement to view your own status as a slave;

related articles

"MySQL"-set up master-slave replication (GTID-based replication)
"MySQL" -dual -master replication (location-based replication)
"MySQL"-set up master-slave replication (location-based replication)

references

Setting up MySQL Master-Slave Replication with GTID
Bug #83713 Slave failed to initialize relay log info after OS crash when use MTS and GTID

Guess you like

Origin blog.csdn.net/u013670453/article/details/114161370