当前位置:网站首页>MYSQL master-slave replication
MYSQL master-slave replication
2022-08-01 01:10:00 【March is immortal】
主从复制
一、什么是主从复制
将主数据库中的DDL和DML操作通过二进制日志传输到从数据库上,然后将这些日志重新执行(重做);从而使得从数据库的数据与主数据库保持一致.
基本原理:
MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.
MySQL复制是基于主服务器在二进制日志中跟踪所有对数据库的更改.因此,要进行复制,必须在主服务器上启用二进制日志.每个从服务器从主服务器接收主服务器已经记录到日志的数据.
当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置.从服务器接收从那时起发生的任何更新,并在本机上执行相同的更新.然后封锁并等待主服务器通知新的更新.从服务器执行备份不会干扰主服务器,在备份过程中主服务器可以继续处理更新.
slave会从master读取binlog来进行数据同步
二、主从复制的作用
1、主数据库出现问题,可以切换到从数据库.
2、可以进行数据库层面的读写分离.
3、可以在从数据库上进行日常备份.
三、复制过程

Binary log:主数据库的二进制日志.
Relay log:从服务器的中继日志.
**第一步:**master在每个事务更新数据完成之前,将该操作记录串行地写入到binlog文件中.
**第二步:**salve开启一个I/O Thread,该线程在master打开一个普通连接,主要工作是binlog dump process.如果读取的进度已经跟上了master,就进入睡眠状态并等待master产生新的事件.I/O线程最终的目的是将这些事件写入到中继日志中.
**第三步:**SQL Thread会读取中继日志,并顺序执行该日志中的SQL事件,从而与主数据库中的数据保持一致.

Demonstration of the master-slave replication process:
1.Master-slave is requiredmysql版本一致且后台以服务运行,First make sure that the two hosts can communicate with each other,然后修改配置文件
2.主从配置在[mysqld]节点下,都是小写
3.主机修改my.ini配置文件
【必须】1.主服务器唯一ID
server-id=1
【必须】2.启用二进制日志
log-bin=自己本地的路径/mysqlbin
log-bin=D:/devSoft/MySQLServer5.5/data/mysqlbin
【可选】3.【可选】启用错误日志
log-err=自己本地的路径/mysqlerr #每次启动mysql,shutdown都会记录在里面,如果出错了,Available here
log-err=D:/devSoft/MysQLServer5.5/data/mysqlerr
【可选】4.根目录
basedir="自己本地路径"
basedir="D:/devSoft/MySQLServer5.5/"
【可选】5.临时目录
tmpdir="自己本地路径"
tmpdir="D:/devSoft/MySQLServer5.5/"
【可选】6.数据目录
datadir="自己本地路径/Data/"
datadir="D:/devSoft/MySQLServer5.5/Data/"
主机,读写都可以
7.read-only=0
【可选】8.设置不要复制的数据库
binlog-ignore-db=mysql
【可选】9.设置需要复制的数据库
binlog-do-db=需要复制的主数据库名字


4.从机修改my.cnf配置文件
(必须)从服务器唯一ID
(非必须)启用二进制日志

【因修改过配置文件,从机+The host needs to restart the backgroundmysql后台服务】
修改完配置service mysql restart
5.主从机都关闭防火墙
windows手动关闭
虚拟机Linux防火墙 service iptables stop
在windows主机上建立账户并授权slave【Database data is important,Normally, other machines are not allowed to be accessed by any user,需要授权】
Slave allowed192.168.14.167以zhangshan这个用户名,123456password to log in to the host

Position=341:That is, the host's database slavemysqlbin的341行开始copy,The disk has a magnetic cylinder,Which track to start copying from
7.在Linux从机上配置需要复制的主机
CHANGE MASTER TO MASTER_HOST='192.168.14.165',
MASTER_USER='zhangsan',
MASTER PASSWORD='123456',
MASTER_LOG_FILE='mysqlbin.具体数字’,MASTER_LOG_POS=具体值;

8.主机新建库,新建表,insert记录,从机复制


如何停止从服务复制功能
stop salve;
注意点:一般stop slave情况就是1、Want to stop the copy function,2、The database assignment function did not start properly,
slave_10_Running : Yes S1ave_sQL_Running : Yes不是这种情况,need to stop first,Check the host againshow slave status,Then re-set the following assignment information according to the displayed parameters
CHANGE MASTER TO MASTER_HOST='192.168.14.165',
MASTER_USER='zhangsan',
MASTER PASSWORD='123456',
MASTER_LOG_FILE='mysqlbin.具体数字’,MASTER_LOG_POS=具体值;
边栏推荐
- 类和对象:上
- 虹科分享|如何用移动目标防御技术防范未知因素
- WindowInsetsControllerCompat简单使用
- Unity3D学习笔记10——纹理数组
- OSF understands the agile development model in one minute
- js 实现复制功能
- High dimensional Gaussian distribution basics
- WindowInsetsControllerCompat is simple to use
- 【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
- 【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
猜你喜欢

ROS2系列知识(4): 理解【服务】的概念

Likou Binary Tree

Academicians of the two academies speak bluntly: Don't be superstitious about academicians

Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation

Application of integrated stepper motor in UAV automatic airport

VPGNet

Four ways the Metaverse is changing the way humans work

你需要知道的 TCP 四次挥手

清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架

Key Points Estimation and Point Instance
随机推荐
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
RTL8762DK UART(二)
Web3.0:构建 NFT 市场(一)
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
YOLO怎么入门?怎么实现自己的训练集?
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
Introduction to machine learning how to?
【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
WeChat applet page syntax
【密码学/密码分析】基于TMTO的密码分析方法
RTL8762DK PWM(七)
OSF understands the agile development model in one minute
Application of integrated stepper motor in UAV automatic airport
VPGNet
Beijing suddenly announced that yuan universe big news
Four ways the Metaverse is changing the way humans work
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
Luogu P3373: Segment tree
What practical projects can machine learning beginners learn?
Matlab/ArcGIS processing GPM global monthly precipitation data