当前位置:网站首页>MySQL master-slave synchronization and its basic process of database and table division
MySQL master-slave synchronization and its basic process of database and table division
2022-06-22 23:08:00 【Bald Xiake】
Main memory synchronization
principle
mysql Three threads are designed in master-slave synchronization (log dump thread,i/o thread,sql thread), Two logs (bin log ,relay log).
Master node log dump Threads
When the slave node connects to the master node , The master node creates a log dump Threads , Used for sending bin-log The content of . In the reading bin-log Operating time , This thread will change the bin-log Lock , When the read is complete , Even before launching to the slave node , The lock will be released .
From the node I/O Threads When executed from a node start
slave After the command , From the node, a I/O Threads are used to connect to the master node , Request the updated bin-log.I/O Thread received master binlog dump
After the update from the process , Keep it locally relay-log in .From the node SQL Threads
SQL The thread is responsible for reading relay log The content in , Parse into specific operations and execute , Finally, ensure the consistency of master-slave data .
Execute the process

After the database is updated, the updated data will be written to bin log among , From the slave node i/o The thread reads the master node bin log Update content to relay log in , Through sql The process is executing on the slave node .
Copy way
MySQL Master slave replication is asynchronous by default .MySQL All additions, deletions and modifications will be recorded in binary log in , When slave Node connection master when , From the active master Get the latest bin log file , And put bin log Medium sql adopt I/O relay To local .
- Asynchronous mode (mysql async-mode) In this mode , The master node will not take the initiative push bin
log To the slave node ( After the master node completes execution, it submits directly ), This could lead to failover Under the circumstances , Maybe the slave node did not update the latest bin log Sync to local .

- Full sync mode
The full synchronization mode means that the master node and all the slave nodes execute commit And confirm to return success to the client - Semi synchronous mode (mysql semi-sync)
In this mode, the master node only needs to receive the return information from one of the slave nodes ( A slave node commit), will commit; Otherwise, you need to wait until the timeout, and then switch to asynchronous mode to submit ;
binlog Record format
binlog There are three ways to generate
- be based on SQL Copy of sentences (statement-based replication,SBR)
- Line based replication (row-based replication,RBR)
- Hybrid mode replication (mixed-based replication,MBR)
be based on SQL Copy of sentences
Statement-base Replication (SBR) It's the record sql Statements in bin log in ,Mysql 5.1.4 And previous versions use this copy format .
advantage : You only need to record the data that will be modified sql Statement to binlog in , Less binlog Daily quality , save I/O, Improve performance .
shortcoming : In some cases , This will cause data inconsistency between master and slave nodes ( such as sleep(),now() etc. ).
Line based replication
Row-based Relication(RBR) yes mysql master take SQL The statement is broken down to be based on Row Change the statement and record it in bin log in , That is, record which data has been modified , What's it going to look like .
advantage : There will be no stored procedures under certain circumstances 、 Or functions 、 perhaps trigger Can't be copied correctly .
shortcoming : There will be a lot of logs , Especially the revision table It's going to make the logs surge , At the same time increase bin log Synchronization time . It can't pass bin log Parse to get the executed sql sentence , You can only see what happened data change .
Hybrid mode replication
Mixed-format Replication(MBR),MySQL NDB cluster 7.3 and 7.4 The use of MBR. It's a mixture of the two modes , For general replication use STATEMENT Save the pattern to binlog, about STATEMENT If the mode cannot be copied, use ROW Pattern to save ,MySQL It will be executed according to SQL Statement to choose how to save the log .
Reference resources mysql Master-slave synchronization of - Principles
Sub database and sub table
Why do we need to divide the database and the table
There is too much data in the database , This leads to slower query efficiency , Previously used to increase the index , Read / write separation , When index optimization is not effective , To divide the database and table .
Single table bottleneck , Too much data , Query efficiency becomes low .
Single library bottleneck ,cpu, Memory , bandwidth , Disk pressure is too high .
Sub database and sub table mode
- Vertical sub table : You can set the fields of a wide table according to the access frequency 、 Business coupling is loose and tight 、 The principle of whether a large field is split into multiple tables , This not only makes the business clear , It can also improve some performance . After break up , Try to avoid associated queries from a business perspective , Otherwise, the performance will not be worth it .
- Vertical sub database : Multiple tables can be classified by business coupling , Store them in different warehouses , These libraries can be distributed on different servers , Thus, the access pressure is loaded by multiple servers , Greatly improve performance , At the same time, it can improve the business clarity of the overall architecture , Different business libraries can customize the optimization scheme according to their own situation . But it needs to solve all the complex problems brought about by cross Library .
- Horizontal sub database : You can put the data of a table ( Press the data line ) It's divided into different warehouses , Each database has only part of the data of this table , These libraries can be distributed on different servers , Thus, the access pressure is loaded by multiple servers , Greatly improve performance . It not only needs to solve all the complex problems caused by cross Library , We also need to solve the problem of data routing .
- Horizontal sub table : You can put the data of a table ( Press the data line ) It is divided into multiple tables in the same database , Each table has only part of the data of this table , This can slightly improve performance , It is only used as a supplementary optimization of the horizontal sub database .
Generally speaking , In the system design stage, we should determine the vertical sub base according to the business coupling tightness , Vertical table scheme , When the amount of data and access pressure is not particularly large , Consider caching first 、 Read / write separation 、 Index technology, etc . If the amount of data is huge , And continue to grow , Then consider the scheme of horizontal database and table .
Split steps
- Objective assessment Split into several tables and several libraries
- Segmentation strategy
- Range segmentation Segment by field interval 1-100 101-200 Subsequent writes will not be evenly distributed
- Intermediate table mapping Disadvantages the intermediate mapping table can be very large
- hash segmentation ( recommend ) Data uniformity
- Select the sub table field Minimize quark library queries
- Resources to prepare
- Double write New and old databases are written at the same time
- Full data migration Put the old database data into the new database
- Data consistency check
- Grayscale cut reading Gradually migrate the traffic to the new database
- Stop writing to the old library after cutting and reading .
Need tools
monitor bin Log Tools DataBus canal
Sub database and sub table tool Sharding-jdbc
Reference resources Billion level commodity data , Detailed explanation of the core process of sub database and sub table
Reason for sub warehouse and sub table , The way of sub database and sub table , Problems caused by sub database and sub table
边栏推荐
- Rendu stéréo
- How to improve work efficiency? Macintosh efficiency tool set
- What if the SQL execution plan of the production system suddenly becomes worse?
- Live broadcast forecast the sixth issue of the webinfo lecture hall of the China Information Association will be broadcast on June 24
- Solution to cache inconsistency
- 2021-04-14
- 2021-05-02
- Fundamentals of shell programming (Part 7: branch statement -if)
- Next permutation [give play to subjective initiative to discover laws]
- Experiment 4 operation comparison between NoSQL and relational database
猜你喜欢

Is it bad for NFT that the market starts to cool down?

In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!

Grafana report display of sentinel based high availability current limiting system

Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)

Spark RDD Programming Guide(2.4.3)

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
![下一个排列[发挥主观能动性发现规律]](/img/bb/262e1a21e4babb8d221d737ced3bcc.png)
下一个排列[发挥主观能动性发现规律]

In a frame because it set 'X-FRAME-OPTIONS' to' deny '

Remote access and control - SSH Remote Management and TCP wrappers access control

Generate detailed API and parameters of QR code using qrcodejs2
随机推荐
Reinforcement learning weekly (issue 50): saferl kit, gmi-drl, rp-sdrl & offline meta reinforcement learning
Task management of embedded development foundation (thread management)
NFT can only be viewed from afar, but not blatantly played
. Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
Generate detailed API and parameters of QR code using qrcodejs2
Developing salary management system based on C language course paper + source code and executable EXE file
Greedy interval problem (2)
R language builds a binary classification model based on H2O package: using H2O GLM constructs regularized logistic regression model and uses H2O AUC value of AUC calculation model
R language data Table data import practice: data Rename the name of the table data column (rename)
Live broadcast forecast the sixth issue of the webinfo lecture hall of the China Information Association will be broadcast on June 24
C language -- 17 function introduction
2021-03-06
R language data preprocessing, converting type variables into factor variables, converting data sets into H2O format, and dividing data sets (training set, test set, verification set)
MySQL multi table operation
2021-04-14
Summary of transport layer knowledge points
Technology cloud report: East to West computing is not only about "computing", but also needs "new storage"
Total number of combinations [standard backtracking + backtracking techniques -- reducing stack depth]
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China