前言
之前文章介绍了GTID主从复制,实现了一主多从的MySQL架构,今天我们来介绍一下主主复制的架构。
原理:主主复制就是根据主从复制的原理,将两个主库设置互为主从即可。
变化:在这两个主库的基础上,还可以搭建双主多从架构。
下面介绍在安装好MySQL8的centos8虚拟机上,实现GTID主主复制,以及双主多从复制的过程:
虚拟机列表:
Master1: 192.168.25.149
Master2: 192.168.25.150
Slave1: 192.168.25.152
Slave2: 192.168.25.153
一、Master1配置
1.1 删除auto.cnf,重启自动生成server-uuid
rm -f /var/lib/mysql/auto.cnf
# 复制的虚拟机镜像,UUID会重复
1.2 设置server-id,打开GTIT
vim /etc/my.cnf
# 添加
[mysqld]
server-id=1
gtid_mode=on
enforce-gtid-consistency=true
1.3 重启MySQL
systemctl restart mysqld.service
1.4 登录mysql,添加用于同步的数据库账号
mysql> create user 'kunlun'@'%' identified by '123456';
mysql> grant replication slave on *.* to 'kunlun'@'%';
mysql> flush privileges;
二、Master2配置
2.1 删除auto.cnf,重启自动生成server-uuid
rm -f /var/lib/mysql/auto.cnf
# 复制的虚拟机镜像,UUID会重复
2.2 设置server-id,打开GTID
vim /etc/my.cnf
# 添加
[mysqld]
server-id=2
gtid_mode=on
enforce-gtid-consistency=true
2.3 重启MySQL
systemctl restart mysqld.service
2.4 登录mysql,添加用于同步的数据库账号
mysql> create user 'kunlun'@'%' identified by '123456';
mysql> grant replication slave on *.* to 'kunlun'@'%';
mysql> flush privileges;
三、Master1启动主从复制
3.1 登录MySQL,配置主从同步
mysql> change master to
master_host='192.168.25.150', # Master2的IP
master_port=3306, #默认端口
master_user='kunlun', # Master2创建的账户
master_password='123456', #密码
master_auto_position = 1;
3.2 启动从库服务
mysql> start slave;
3.3 查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
四、Master2启动主从复制
4.1 登录MySQL,配置主从同步
mysql> change master to
master_host='192.168.25.149', # Master1的IP
master_port=3306, #默认端口
master_user='kunlun', # Master1创建的账户
master_password='123456', #密码
master_auto_position = 1;
4.2 启动从库服务
mysql> start slave;
4.3 查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
至此,互为主从复制的GTID主主复制已经搭建成功,下面再为两个Master各搭建一个从库。
五、Slave1配置,并启动主从复制
5.1 删除auto.cnf,重启自动生成server-uuid
rm -f /var/lib/mysql/auto.cnf
# 复制的虚拟机镜像,UUID会重复
5.2 设置server-id
vim /etc/my.cnf
# 添加
[mysqld]
server-id=3
gtid_mode=on
enforce-gtid-consistency=true
5.3 重启MySQL
systemctl restart mysqld.service
5.4 登录mysql,配置主从同步
mysql> change master to
master_host='192.168.25.149', # Master1的IP
master_port=3306, # 默认端口
master_user='kunlun', # Master1创建的账户
master_password='123456', # 密码
master_auto_position = 1;
5.5 启动从库服务
mysql>start slave;
5.6 查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
六、Slave2配置,并启动主从复制
6.1 删除auto.cnf,重启自动生成server-uuid
rm -f /var/lib/mysql/auto.cnf
# 复制的虚拟机镜像,UUID会重复
6.2 设置server-id
vim /etc/my.cnf
# 添加
[mysqld]
server-id=4
gtid_mode=on
enforce-gtid-consistency=true
6.3 重启MySQL
systemctl restart mysqld.service
6.4 登录mysql,配置主从同步
mysql> change master to
master_host='192.168.25.150', # Master2的IP
master_port=3306, #默认端口
master_user='kunlun', # Master2创建的账户
master_password='123456', #密码
master_auto_position = 1;
6.5 启动从库服务
mysql>start slave;
6.6 查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
至此,基于GTID的双主多从复制已经搭建成功
KunlunDB项目已开源
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
END