zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql双主gtid模式,在有数据情况下

mysql模式数据 情况 GTID 双主
2023-09-11 14:21:08 时间

【1】需求

(1.1)需求

现有主从 b(主)=》c从

现在想合并数据,有一个主库 a,a与b的数据无交集

想把 b变成a的从,从而构建 a=>b=>c 的复制情况

如何在 b不 reset master 的情况下 b变成a的从啊(gtid模式)

最终的结果是 主库 a 会一直写入,b即做a的从库同时也是主库有数据写入

(1.2)架构

数据库版本:mysql8.0.22 社区版,启用 gtid 

192.168.148.27    a 机器

192.168.148.39    b 机器

192.168.148.30    c 机器

 

《1》b(主)=》c(从)

《2》a(主)=》b(从,同时是主)=》c

 

【2】测试

(2.1)构造测试数据

-- <1> a / b 机器都创建复制账户
create user repl@'192.168.148.%' identified by 'a123456!';
grant replication client,replication slave on *.* to repl@'192.168.148.%';
reset master;
-- <2> a 机器创建测试库 a ; create database a; create table a.a1(id int); insert into a.a1 values(1); -- <3> b 机器创建测试库 b ; create database b; create table b.b1(id int); insert into b.b1 values(1); -- <4> c 机器 change master to b机器 change master to master_host='192.168.148.39', master_user='repl', master_password='a123456!', master_auto_position=1;
start slave;

 

当前是  b(主)=》c(从),a 是独立主库;

(2.2)a机器的 a库数据初始化dump 到b机器

a机器:

mysqldump --single-transaction --master-data=2 --set-gtid-purged=ON -B a > a.txt

 

less a.txt

   

 

b 机器,执行上面这个 set @@global.gtid_purged....

   

 

成功了;

a机器上执行,修改dump出来的文件 去掉该参数,且把文件内的 binlog 修改为1;

sed -i 's/SET @@GLOBAL.GTID_PURGED=/#SET @@GLOBAL.GTID_PURGED=/g' a.txt

sed -i 's/SET @@SESSION.SQL_LOG_BIN= 0/SET @@SESSION.SQL_LOG_BIN= 1/g' a.txt

b机器上应用 a.txt

mysql < a.txt 

  

我们可以看到 a1表 只有 1 一行数据,2是我们mysqldump 导出该文件之后操作的。

  

 

 

(2.3)b 机器 change master to  a机器

change master to 
master_host='192.168.148.27',
master_user='repl',
master_password='a123456!',
master_auto_position=1;
start slave;

 

核验:

(1)show slave status

  

 

(2)数据同步查看

  

 

 至此完成;

a-b的复制,那么 b-c的复制有问题;

(2.4)b(主)=》c(从)的复制出现问题

报错如下:

 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.
The GTID set sent by the slave is '', and the missing transactions are '101e0f50-69e2-11ec-b4dc-000c2992a4b1:1-6''

很明显,报错是说确实事务: 

  missing transactions are '101e0f50-69e2-11ec-b4dc-000c2992a4b1:1-6'

那我们同理也是要在 c机器上运行:

stop slave;
set @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '101e0f50-69e2-11ec-b4dc-000c2992a4b1:1-6';  
start slave;

成功:

 

  

 

【3】双主,a 也变成 b的从库

记住,我们的前提是 现在想合并数据,有一个主库 a,a与b的数据无交集

(3.1)b 机器的 b 库数据初始化dump 到 a 机器

b机器:

mysqldump --single-transaction --master-data=2 --set-gtid-purged=ON -B b > b.txt

这里我们就不做 b.txt 文件内容替换了

(3.2)应用 dump文件到 a 机器 

[root@DB4 /data/dba]$ mysql < b.txt 
ERROR 3546 (HY000) at line 24: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED

如上面代码,我们发现报错了;

文件中的 set @@global.gtid_pirged 是下面值:

  SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '101e0f50-69e2-11ec-b4dc-000c2992a4b1:1-7,aa548eae-d889-11ea-8601-000c29c8f925:1-12';

是因为有自己的gtid啊;那么我们去掉自己的gtid;那么我们自己的 gtid 是多少呢

  

 

 我们可以看到是101开头的,那很明显了,我们把文件中的 

  SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '101e0f50-69e2-11ec-b4dc-000c2992a4b1:1-7,aa548eae-d889-11ea-8601-000c29c8f925:1-12';

改成如下:

  SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ 'aa548eae-d889-11ea-8601-000c29c8f925:1-12';

vim b.txt

  

 

 应用,核验:如下

mysql < b.txt

  

 

(3.3)a 机器 change master to b 机器

b机器:

insert into b.b1 values(2),(3);

a 机器:

change master to 
master_host='192.168.148.39',
master_user='repl',
master_password='a123456!',
master_auto_position=1;
start slave;

核验:成功!