mysql使用gtid主从复制

2024-02-09 18:58

本文主要是介绍mysql使用gtid主从复制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境:
master:192.168.56.201
slave:192.168.56.211

1、配置主从节点的服务配置文件

1.1、配置master节点:
   
[mysqld]
binlog-format=ROW
server_id =1
log_slave_updates=1
#binlog-do-db = brent
#binlog-ignore-db = mysql
#replicate_ignore_db=mysql
#replicate_do_table=brent.t1
#replicate_ignore_table=brent.t2
#######GTID#######
gtid-mode = on
enforce-gtid-consistency = true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=192.168.56.201


1.2、配置slave节点:
   
[mysqld]
binlog-format=ROW
#################REPLICATE###############
server_id =11
log_slave_updates=1
#binlog-do-db = brent
#binlog-ignore-db = mysql
#replicate_ignore_db=mysql
#replicate_do_table=brent.t1
#replicate_ignore_table=brent.t2
#######GTID#######
gtid-mode = on
enforce-gtid-consistency = true
master-info-repository=TABLE
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=192.168.56.211

2、创建复制用户
在master端创建复制用户
   
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl@'%‘ IDENTIFIED BY 'rpl';


3、为备节点提供初始数据集

锁定主表,备份主节点上的数据,将其还原至从节点;如果没有启用GTID,在备份时需要在master上使用show master status命令查看二进制日志文件名称及事件位置,以便后面启动slave节点时使用。
我这里使用的mysqldump命令:
mysqldump --lock-all-tables --all-databases  >suq.sql
再将数据导入到slave节点
cat suq.sql |mysql

4、启动从节点的复制线程

如果启用了GTID功能,则使用如下命令:
mysql> CHANGE MASTER TO MASTER_HOST='master.magedu.com', MASTER_USER='rpl', MASTER_PASSWORD='rpl',  MASTER_AUTO_POSITION=1;


############################GTID管理###########################
1.gtid中将relay_log_info_repository=table、master_info_repository=table设置为table后,会将master.info和relay.info中的数据保存在mysql的表中,
分别为:
mysql.slave_master_info
mysql.slave_relay_info
mysql.slave_worker_info

2.gtid模式中报错,不能使用skip counter跳过事务,必须将事务ID设置为空值,具体如下:
使用show slave status查看当前状态:
   
Last_SQL_Error: Worker 1 failed executing transaction 'ca6ccc10-b5c5-11e5-aed5-08002775af00:911' at master log master-bin.000006, end_log_pos 280262; Could not execute Delete_rows event on table suq.t1; Can't find record in 't1', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log FIRST, end_log_pos 280262
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: ca6ccc10-b5c5-11e5-aed5-08002775af00
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 160108 15:27:58
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: ca6ccc10-b5c5-11e5-aed5-08002775af00:331-911
Executed_Gtid_Set: 22e602cb-b5c8-11e5-aee4-08002775af00:1-2,
ca6ccc10-b5c5-11e5-aed5-08002775af00:1-910

retrieved_gtid_set:表示接受到的事务
executed_gtid_set:表示已经完成的事务, 这里有多行表是曾经或者现在有多个主,分别对应其中的uuid和事务
其中1-910已经完成,911事务报错
这时跳过这条事务:
   
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set gtid_next='ca6ccc10-b5c5-11e5-aed5-08002775af00:911';
Query OK, 0 rows affected (0.00 sec)
 
mysql> begin;commit;
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
mysql> set gtid_next="automatic";
Query OK, 0 rows affected (0.00 sec)
 
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
slave端已经正常:
   
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.201
Master_User: rpl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000006
Read_Master_Log_Pos: 280293
Relay_Log_File: relay-bin.000005
Relay_Log_Pos: 451
Relay_Master_Log_File: master-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 280293
Relay_Log_Space: 1226
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: ca6ccc10-b5c5-11e5-aed5-08002775af00
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: ca6ccc10-b5c5-11e5-aed5-08002775af00:331-911
Executed_Gtid_Set: 22e602cb-b5c8-11e5-aee4-08002775af00:1-2,
ca6ccc10-b5c5-11e5-aed5-08002775af00:1-911
Auto_Position: 1

3.gtid模式改为传统模式
首先在my.cnf中注释掉gtid-mode=ON和enforce-gtid-consistency=ON并且重启mysql
然后使用语句change master to master_auto_potision=0;
最后使用:
slave> CHANGE MASTER TO MASTER_HOST='192.168.56.201',
-> MASTER_USER='rpl',
-> MASTER_PASSWORD='rpl',
-> MASTER_LOG_FILE='master-bin.000003',
-> MASTER_LOG_POS=1174;











这篇关于mysql使用gtid主从复制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/695039

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)