当前位置:网站首页>MySQL8.0轻松完成GTID主从复制
MySQL8.0轻松完成GTID主从复制
2022-06-22 21:23:00 【KunlunBase 昆仑数据库】
为什么需要主从复制?
通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低。
为了提升业务系统性能,优化用户体验,可以通过做主从复制来减轻主数据库的负载。
而且如果主数据库宕机,可快速将业务系统切换到从数据库上,可避免数据丢失。
主从复制原理
主库将更新记录保存于Binlog文件中,并通过Binglog Dump线程发送给从库。
从库的I/O线程从主库读取Binlog更新的数据,写入到Relay log中。
从库的SQL线程读取Relay log文件的数据写入从库,实现同步。

采用GTID (Global Transaction ID)主从复制方式,保证了每个在主库上提交的事务在集群中有一个唯一的ID。
这种方式强化了数据库的主从一致性,故障恢复以及容错能力。
相比于早期通过master_log_file,master_log_pos同步的主从复制,省去了从库宕机重启后,需要重新配置的问题。
接下来介绍在虚拟机centos8版本,实现GTID主从复制的三大步骤:
一.MySQL8.0安装
1.1、使用rpm来安装MySQL
wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm
rpm -ivh mysql80-community-release-el8-1.noarch.rpm
yum install mysql-server -y1.2、启动mysql并设置开机自启动
systemctl start mysqld.service
systemctl enable mysqld.service1.3、关闭防火墙并设置开机不启动
systemctl stop firewalld.service
systemctl disable firewalld.service1.4、登录,无需密码,当前最新版本为8.0.26
mysql1.5、查看BINLOG状态,最新版本默认已经开启
mysql> show variables like '%log_bin%';1.6、复制安装好MySQL8的虚拟机镜像,作为从库
二.主库配置
2.1、设置server-id,打开GTIT
vim /etc/my.cnf
# 添加
[mysqld]
server-id=1
gtid_mode=on
enforce-gtid-consistency=true
# 重启mysql
systemctl restart mysqld.service2.2、登录mysql,添加用于同步的数据库账号
mysql> create user 'kunlun'@'%' identified by '123456';
mysql> grant replication slave on *.* to 'kunlun'@'%';
mysql> flush privileges;三.从库配置
3.1、删除auto.cnf,重启自动生成server-uuid
rm -f /var/lib/mysql/auto.cnf
# 复制的虚拟机镜像,UUID会重复3.2、设置server-id, 主库和从库server-id不要重复
vim /etc/my.cnf
# 添加
[mysqld]
server-id=2
gtid_mode=on
enforce-gtid-consistency=true
# 重启mysql
systemctl restart mysqld.service3.3、登录mysql,配置主从同步
mysql> change master to
master_host='192.168.25.139', #主库IP
master_port=3306, #默认端口
master_user='kunlun', #第二步创建的账户
master_password='123456', #密码
master_auto_position = 1;3.4、启动从库服务
mysql> start slave;
3.5、查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes3.6、测试
在主库创建数据库:
mysql> create database mydb;在从库查看:
mysql> show databases;至此,GTID方式的主从复制已经搭建成功。
注意
要实现MySQL的Replication,首先必须打开master端的binlog(mysql-bin.xxxxxx)日志功能,否则无法实现mysql的主从复制。
因为MySQL的整个主从复制过程实际上就是:slave端从master端获取binlog日志,然后再在自己身上完全顺序的执行该日志中所记录的各种SQL操作。
KunlunDB项目已开源
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
END
边栏推荐
- Tp5.1 solving cross domain problems
- Tp5.1 upload excel file and read its contents
- 斐波那契数列合集
- SSH method 2 for adding node nodes in Jenkins
- The breakthrough of key chips hindering Huawei 5g mobile phones has been achieved, and domestic chips have gained 10% share
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- OJ每日一练——验证子串
- LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
- tp5.1解决跨域
- C language -- 17 function introduction
猜你喜欢

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

2021-08-22

同步电路与跨时钟域电路设计2——多bit信号的跨时钟域传输(FIFO)

wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz

Safe and reliable! Tianyi cloud data security management platform passed the evaluation

Optimization - linear programming

【Kubernetes 系列】Kubernetes 概述

弱电转职业网工难不难?华为售前工程师分享亲身经历
![[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip](/img/32/88321db57afb50ccc096d687ff9c41.png)
[STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip

Asynchronous FIFO
随机推荐
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
LeakCanary 源码详解(2)
剑指 Offer 07. 重建二叉树
[redisson] source code analysis of multilock
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
tp5.1解决跨域
OJ每日一练——整理命名
13. 罗马数字转整数
2021-04-14
Redis cache
Sword finger offer 05 Replace spaces
SSH method 2 for adding node nodes in Jenkins
OJ daily exercise - virus proliferation
'dare not doubt the code, but have to doubt the code 'a network request timeout analysis
剑指 Offer 05. 替换空格
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
Uniapp modifies array properties, and the view is not updated
Is it safe to make an appointment to pay new debts? Is it reliable?
13. Roman numeral to integer
【Kubernetes 系列】Kubernetes 概述