当前位置:网站首页>Master-slave replication principle of MySQL
Master-slave replication principle of MySQL
2022-07-07 06:58:00 【Rookie ~ ~】
List of articles
In the actual production environment , If the mysql The read and write of the database are operated in a database server , Whether it's in security 、 High availability , Or high concurrency and other aspects can not meet the actual needs , General Synchronize data through master-slave replication , Then through read-write separation to improve the concurrent load capacity of the database .
One 、 Master slave replication concept
Master slave replication is MySQL Basic technology provided , Master slave replication process :binlog Binary log ( In addition to querying, other changes and related operations will be recorded in binlog Inside )、relay log Logs and three threads (master A thread and slave Two threads of ).
- Main library (master) foreign Provide data addition, deletion, modification and query services , The modification of data involved in the main database will be written in binlog
- Slave Library (slave) be used for Data synchronization and data backup , From the main library through special threads binlog Put the main database and data 、 jurisdiction 、 The changes related to the table structure are synchronized to the slave library , It means that all modifications made on the master database will be reflected on the slave database through the master-slave replication mechanism .
The benefits of master-slave replication : The data backup , We can even make a hot backup for it , It is through MySQL middleware mycat
, Can achieve disaster , Disaster tolerance also reflects High availability .
disaster : If the main library hangs , Middleware agent mycat Automatically map service requests to slave libraries , The slave library continues to provide services , It shows high availability ( The back-end service allows certain exceptions to occur , But the back-end architecture services should be fault-tolerant , Deal with these abnormal errors , And provide normal services again )
Two 、 The concept of separation of reading and writing
Read write separation is based on master-slave replication . In the actual application environment , It must be that there are many reading operations , It's like we buy things on the e-commerce platform , Maybe I did 100 I just bought oneortwo . So there are always many more read operations than write update operations . So we configure the read-write separation based on master-slave replication , It is to let a master database be specially used for data modification , When writing, write it on the main database , The master database changes the data through master-slave replication binlog Sync to the slave Library , Then other client query requests will eventually be mapped to the slave Library , And we have a master database with twoorthree slave databases , The main database is specially used to update data ( Write operations ), The slave library is specially used for reading operations, which can well share the pressure of reading and writing , Don't concentrate all on the main database , The concurrent processing ability of back-end services has been greatly improved , The other is its high availability disaster tolerance , When the main library hangs up , You can change the specified slave library into the master library .
In the picture above binlog, Even if we don't have master-slave replication , Also can write binlog Of , Only master-slave replication is through binlog To achieve .
3、 ... and 、 Master and slave Libraries
1. Main library
Main library master The server creates a binlog Dump thread , Send binary log contents to the slave server
2. Slave Library
- There is a special one from the library I/O Threads Specially read and receive the content sent by the main database , It will send it from the main database binlog The content is received and written to a
relay log
Inside , The relay log is equivalent to a buffer , such master You don't have to wait slave The next event will not be sent until the execution is completed . It's not the main library binlog Read the content and execute it directly , In this way, the main database is the place where the direct implementation is not good binlog There may be a lot of content , And received from the library binlog Content execution may be slow , As a result, the gap between the updated data from the slave database and the master database will become larger and larger . Data replication may be relatively backward . - There will also be one from the library SQL Threads , From Relay log read Corresponding operation , be-all SQL Do it all once , That's it Synchronization of slave library content and master library content
Four 、 Master slave replication process
Master slave replication process : Two logs (binlog Binary log and relay log journal ) And three threads (master A thread and slave Two threads of ).
- The update operation of the main library is written to binlog Binary log in ( The main library needs to be opened binlog switch )
- master The server creates a binlog Dump thread , Send binary log contents to the slave server
- slave Machine execution
START SLAVE
The command will create a... From the server IO Threads , receive master Of binary log Log copied to it ( In memory , Read and write fast ). First slave Start a worker thread (I/O Threads ),I/O Threads will actively connect master , Then the main library will open dump Threads ,dump The thread from master Of binlog Read the event in and send it to slave Of I/O Threads , If dump The thread has caught up with master( On the main library dump The thread has put binlog The content of is finished , And on the main library binlog No more content ),dump The thread will sleep and wait binlog Create new events ,slave Of I/O Events received by the thread are written to the relay log - slave Of SQL Threads The last step in the process ,SQL The thread from relay log Read events in , And execute the event update slave The data of , Make it relate to master Data synchronization for . as long as SQL Threads and I/O Threads are consistent , The relay log is usually located at os In cache , So the overhead of relay log is very small
5、 ... and 、 Master slave copy effect display
linux Upper centos As main storehouse win10 Upper mysql server As a slave library to demonstrate :
Master slave replication is one-way synchronization ,master Changes to slave Sync . When configuring master-slave replication , The data of the two libraries may be different , Start by configuring master-slave replication , All changes in the master library will be synchronized to the slave library .
master establish mytest database
see slave, Find out mytest Synced
master Created user surface ,slave Also synchronized user surface
Now? linux Terminal MySQL(master) Delete mytest library
here slave Of mytest There is no more
show processlist
You can see master Worker threads in the current environment
see slave Worker threads in the current environment
边栏推荐
- 如何给目标机器人建模并仿真【数学/控制意义】
- MySql用户权限
- 途家、木鸟、美团……民宿暑期战事将起
- Postgresql源码(60)事务系统总结
- Programmers' daily | daily anecdotes
- Redhat5 installing vmware tools under virtual machine
- 肿瘤免疫治疗研究丨ProSci LAG3抗体解决方案
- ESXI挂载移动(机械)硬盘详细教程
- Under what circumstances should we consider sub database and sub table
- 从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
猜你喜欢
Matlab tips (29) polynomial fitting plotfit
快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!
Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
品牌·咨询标准化
Bus消息总线
Brand · consultation standardization
Matlab tips (30) nonlinear fitting lsqcurefit
unity3d学习笔记
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Big coffee gathering | nextarch foundation cloud development meetup is coming
随机推荐
【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
Redhat5 installing vmware tools under virtual machine
Big coffee gathering | nextarch foundation cloud development meetup is coming
根据IP获取地市
什么情况下考虑分库分表
如何给目标机器人建模并仿真【数学/控制意义】
AddressSanitizer 技术初体验
场馆怎么做体育培训?
Basic introduction of JWT
Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
Linear algebra (1)
How can flinksql calculate the difference between a field before and after update when docking with CDC?
隐马尔科夫模型(HMM)学习笔记
毕业设计游戏商城
Leetcode T1165: 日志分析
From zero to one, I will teach you to build the "clip search by text" search service (2): 5 minutes to realize the prototype
How to install swoole under window
剑指offer-高质量的代码
Jetpack compose is much more than a UI framework~
The latest trends of data asset management and data security at home and abroad