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 .
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 .
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 .
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 .
The pressure from the reservoir is greater
Get more slave computers , Share the pressure of reading request .
Network delay
Contact the O & M or cloud service provider for solutions .
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 .
Semi-synchronous replication
After at least one execution from the library is completed , It returns success to the client .
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 .
Senior programmers must know and know , A detailed explanation MySQL Master slave synchronization principle , I recommend more related articles in my collection
- 【 Good programmer note sharing 】——UIView And CALayer Detailed explanation
-iOS train ,iOS Study ------- Technology blog . Looking forward to communicating with you !------------ UIView And CALayer Detailed explanation Research Core Animation It's been a while , About Core Animati ...
- Android Interview questions for development engineers handler Detailed explanation .android The programmer ,android Develop interview materials , Detailed explanation
Message: news : It contains messages ID, Message object and processed data, etc , from MessageQueue Unified formation , In the end Handler Handle Handler: handler : be responsible for Message Send messages and process .Handler ...
- .NET Programmers must know and know about project development —Dev Context checking during execution of integration test cases in environment ( actual combat )
Microsoft.NET Solution , Project development must know, must know . Starting from this article, I will share a series of things that I think are very necessary in practical work .NET The core technology of project development , So I call it "must know must know" . Although this series is using .NET/C# ...
- Android Programmers must know the necessary network communication transmission layer protocol ——UDP and TCP
1. comment The Internet has been highly developed so far , And for Internet applications ( Especially instant messaging technology ) For the developers , Network programming is the foundation of the foundation , Only a better understanding of the relevant basic knowledge , For the development of application layer, we can do it with ease . about Android ...
- To the higher level : good Android The network foundation that programmers must know
1. Preface Network communication has always been Android A more important module in the project ,Android There have been many excellent network frameworks in open source projects , From the beginning it was just a couple of right HttpClient and HttpUrlConnection Easy to use ...
- PHP Advanced programmers must see knowledge points : Catalogue ( Irregular update )
Interview questions series : Share a wave of Tencent PHP Interview questions 2019 year PHP The latest interview questions ( With answers ) Redis Advanced interview questions I'm afraid I won't be able to enter the big factory if I learn these ? Ali interviewer has three years of experience PHP Summary of programmer knowledge points , Learn that you are the next Ali ! ph ...
- SqlServer Summary of precautions , Advanced programmers must recite !
This article mainly introduces SqlServer Precautions for use . Want to be a senior programmer , The use of database is necessary . And the proficiency of database , It also reflects the level of development . Let's introduce SqlServer In the process of use and design ...
- ( turn )【 recommend 】 primary .NET The programmer , You have to know EF Knowledge and experience
from :http://www.cnblogs.com/zhaopei/p/5721789.html [ recommend ] primary .NET The programmer , You have to know EF Knowledge and experience Read the directory [ This article has put a curse on . Look first and then look. , It will go up ...
- MySQL Will know 1-20 Zhang's reading notes
MySQL Memo Catalog Catalog Use MySQL Retrieving data Sorting and retrieving data Filtering data Data filtering Filter with wildcards Search with regular expressions Create calculated fields Using data processing functions Numerical processing function Summary data Grouped data Use child ...
- SQL Will know
This article introduces the basic SQL sentence , Including inquiries . Filter . Sort . grouping . coupling . View . insert data . Create control tables, etc . Introductory series , It's not enough , I hope you can give me some advice . Note that some examples in this article can only be used in specific DBMS To realize ( Some have indicated that , Some are not marked ), No ...
Random recommendation
- Android PullToRefresh (ListView GridView The drop-down refresh ) The use of,
Reprint please indicate the source :http://blog.csdn.net/lmj623565791/article/details/38238749, This article from the :[ Zhang Hongyang's blog ] A friend in the group mentioned this by chance in today's chat git ...
- QueryRunner Of API
org.apache.commons.dbutils Class QueryRunner java.lang.Object org.apache.commons.dbutils.AbstractQue ...
- Welcome to Devil_lixu Technology blog
Welcome to my blog If you think the content will help you a little , Then make a point of praise ^_^
- 201521123036 《Java Programming 》 The first 6 Weekly learning summary
This week's learning summary 1.1 Object oriented learning is over , Please use mind map , To encapsulate . Inherit . Draw a mind map for the core concept , Make a summary of the object-oriented thinking . 1.2 Optional : Summarize other lessons in the usual way . Object cloning :Clon ...
- java.net.UnknownHostException
java.net.UnknownHostException Wrong solution see Test whether the real machine or simulator is connected to the network . If the first step is not wrong , Generally, the tester is not connected to the network , You can open a web page or other ...
- Experiment two 《Java Object oriented programming 》 A brief explanation of
Experiment two <Java Object oriented programming > A brief explanation of The experimental requirements of this week are : Complete experiment 2 <Java Object oriented programming > The content in among Eclipse Relevant content reference Intellj IDEA Simple and easy ...
- sql server The defects of String or binary data will be truncated Which field
String or binary data will be truncated Which field news 8152, Level 16, state 14, The first 2 Line truncates string or binary data . sql server2000 sql server2014 Nor can it directly prompt which field ...
- ElasticSearch Inquire about Second articles : Document update
<ElasticSearch Inquire about > Directory navigation : ElasticSearch Inquire about Chapter one : Search for API ElasticSearch Inquire about Second articles : Document update ElasticSearch Inquire about Third articles : ...
- To configure Git binding [email protected]
user name , This name will appear in future submission records . git config --global user.name "[email protected] user name " And then there was Email, Again , This Email It's going to show up on you, too ...
- JAVA Build an introductory programming environment
This article is written for new contacts or want to learn JAVA My new friend . Study JAVA, Need to find a good introductory book , recommend <Java From entry to mastery >, Then is JAVA Construction of programming development environment . First , We need to install JAVA development tool ...