当前位置:网站首页>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
边栏推荐
- Jetpack compose is much more than a UI framework~
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
- Matlab tips (30) nonlinear fitting lsqcurefit
- MOS管参数μCox得到的一种方法
- 企業如何進行數據治理?分享數據治理4個方面的經驗總結
- mobx 知识点集合案例(快速入门)
- Prompt for channel security on the super-v / device defender side when installing vmmare
- Linear algebra (1)
- Algorithm --- bit count (kotlin)
- What books can greatly improve programming ideas and abilities?
猜你喜欢
化工园区危化品企业安全风险智能化管控平台建设四大目标
Abnova 免疫组化服务解决方案
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
Leetcode T1165: 日志分析
The latest trends of data asset management and data security at home and abroad
Jetpack compose is much more than a UI framework~
unity3d学习笔记
JESD204B时钟网络
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
ESXI挂载移动(机械)硬盘详细教程
随机推荐
Unable to debug screen program with serial port
Abnova 膜蛋白脂蛋白体技术及类别展示
Stack and queue-p78-8 [2011 unified examination true question]
JDBC database connection pool usage problem
Bus消息总线
循环肿瘤细胞——Abnova 解决方案来啦
js装饰器@decorator学习笔记
【mysqld】Can't create/write to file
Matlab tips (30) nonlinear fitting lsqcurefit
dolphinscheduler3. X local startup
企業如何進行數據治理?分享數據治理4個方面的經驗總結
unity3d学习笔记
大促过后,销量与流量兼具,是否真的高枕无忧?
[solution] final app status- undefined, exitcode- 16
企业如何进行数据治理?分享数据治理4个方面的经验总结
一条慢SQL拖死整个系统
libcurl返回curlcode说明
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
CompletableFuture使用详解
毕业设计游戏商城