当前位置:网站首页>Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
2022-07-07 23:25:00 【u012804784】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
1. MySQL Implementation of master-slave synchronization
MySQL Master slave synchronization is based on Bin Log Realized , and Bin Log What is recorded is the original SQL sentence .
Bin Log There are three log formats , Sure binlog_format Configuration parameter assignment .
| Parameter values | meaning |
|---|---|
| Statement | Record original SQL sentence , This will cause the update time to be inconsistent with the original database . such as update_time=now() |
| Row | Record the changes in each row of data , Ensure that the data is consistent with the original database , The disadvantage is the large amount of data . |
| Mixed | Statement and Row Mixed mode of , By default Statement Pattern , Date involved 、 When the function is related, use Row Pattern , It reduces the amount of data , It also ensures data consistency . |
Common master-slave synchronization architectures include one master and multiple slaves 、 Two masters and many subordinates .


2. MySQL The role of master-slave synchronization
- Read / write separation , Improve database performance
- disaster recovery , When the primary server is unavailable , Provide services from the server , Improve availability
- Redundant backup , The primary server data is damaged or lost , Keep backups from the server
One master and many slaves :
Generally, the master database is responsible for all read / write requests , The slave database is only responsible for disaster recovery and redundant backup .
If you do read-write separation , The main library is responsible for writing requests , The slave library is responsible for reading requests , Can improve database performance .
Dual master multi slave architecture :
Generally, it is the main database 1 Responsible for all read and write requests , Main library 2 No external services , Only for disaster recovery .
Compared with the one master multi slave architecture , Dual master multi slave architecture can reduce downtime , Faster recovery of database availability .
3. The principle of active synchronization

- When the master database data changes , Write local Bin Log file
- Slave Library IO Thread initiation dump Main library Bin Log File request
- Main library IO Thread push Bin Log File to and from the library
- Slave Library IO Thread Bin Log Content is written locally Relay Log In file
- Slave Library SQL Thread reads Relay Log The contents of the document
- Slave Library SQL The thread executes again SQL sentence
4. Master slave synchronization delay problem
The most common problem of master-slave synchronization is the delay of master-slave synchronization , You can do this by executing show slave status Command view delay time ,Seconds_Behind_Master Indicates the number of seconds to delay .

What are the reasons for the delay of master-slave synchronization ?
- The slave machine has poor performance
The master database is responsible for all read / write requests , The slave library is only used for backup , Be able to use machines with poor performance , The execution time is naturally slow .
2. The pressure from the reservoir is greater
After the separation of reading and writing , The main library is responsible for writing requests , The slave library is responsible for reading requests .
Internet applications generally have more read requests , So reading from the library is more stressful , Take up more CPU resources .
3. Network delay
When the main library Bin Log When a file is sent to or from the library , Network delays may occur , It will also cause the data from the database to not keep up .
4. The main database has large transactions
When a large transaction needs to be executed on the master database 5 minute , hold Bin Log File sent to slave Library , The slave library also needs to perform at least 5 minute , So at this time, the database appears 5 Minute delay .
The solution of master-slave synchronization delay ?
- The slave machine has poor performance
Replace the slave library with a machine of the same specification as the master library .
2. The pressure from the reservoir is greater
Get more slave computers , Share the pressure of reading request .
3. Network delay
Contact the O & M or cloud service provider for solutions .
4. The main database has large transactions
Divide large transactions into small ones for execution , Major events will not only cause slave delay , Deadlock may also occur , Reduce database concurrency performance , So try not to use big things .
5. How to improve master-slave synchronization performance
1. Start multi-threaded replication from the library
It is to use multithreading in the last two steps of master-slave synchronization , Modify the configuration slave_parallel_workers=4, Representative opens 4 Copy threads .

2. Modify the synchronization mode , Change to asynchronous
There are three replication methods for master-slave synchronization :
- Full synchronous replication
When the main library completes a transaction , And after all slave libraries have executed the transaction , To return success to the client .
2. Semi-synchronous replication
After at least one execution from the library is completed , It returns success to the client .
3. Asynchronous replication
After the main database is executed , Return to success immediately , Do not care whether the execution from the library is completed .
If the data security requirements are not so high , You can change the synchronous mode to semi synchronous replication or asynchronous replication .
3. Modify slave Library Bin Log To configure
modify sync_binlog To configure :
sync_binlog=0 , Said to write binlog Do not refresh the disk now , The system decides when to refresh the disk .
sync_binlog=1, Every time binlog Both refresh disks , High safety , Poor performance .
sync_binlog=N, Write N Time binlog To refresh the disk .
The requirements for data security from the database are not so high , You can set sync_binlog=0.
modify innodb_flush_log_at_trx_commit To configure :
innodb_flush_log_at_trx_commit=0, Every second , Flush the transaction log to disk .
innodb_flush_log_at_trx_commit=1, Each transaction is flushed to disk .
innodb_flush_log_at_trx_commit=2, Each transaction does not actively refresh the disk , The system decides when to refresh the disk .
The requirements for data security from the database are not so high , You can set innodb_flush_log_at_trx_commit=2.
Summary of knowledge points :

Articles are constantly updated , You can search through wechat 「 One light architecture 」 Read more technical dry goods for the first time .
边栏推荐
- 三问TDM
- UE4_ Ue5 panoramic camera
- UE4_UE5结合罗技手柄(F710)使用记录
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- CXF call reports an error. Could not find conduct initiator for address:
- Ros2 topic (03): the difference between ros1 and ros2 [02]
- Inftnews | the wide application of NFT technology and its existing problems
- POJ2392 SpaceElevator [DP]
- Inftnews | web5 vs Web3: the future is a process, not a destination
- In the field of software engineering, we have been doing scientific research for ten years!
猜你喜欢

Oracle database backup and recovery

MySQL Index Optimization Practice II

家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
![MATLAB signal processing [Q & A essays · 2]](/img/be/0baa92767c3abbda9b0bff47cb3a75.png)
MATLAB signal processing [Q & A essays · 2]

Wechat forum exchange applet system graduation design completion (1) development outline

14、 Two methods of database export and import

Inftnews | the wide application of NFT technology and its existing problems

Add data analysis tools in Excel

UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)

UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
随机推荐
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
Network security - joint query injection
Coreseek: the second step is index building and testing
FPGA基础篇目录
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
Matlab-SEIR传染病模型预测
USB(十五)2022-04-14
Add data analysis tools in Excel
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
[microservices SCG] gateway integration Sentinel
What are the similarities and differences between smart communities and smart cities
【微服务|SCG】gateway整合sentinel
Network security - information query of operating system
Unity3d learning notes 5 - create sub mesh
UE4_UE5全景相机
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Unity3D学习笔记5——创建子Mesh
MySQL Index Optimization Practice I
Unity3d learning notes 4 - create mesh advanced interface
Solution: prompt "unsupported video format" when inserting avi format video into the message