当前位置:网站首页>Explain the principle of MySQL master-slave replication in detail
Explain the principle of MySQL master-slave replication in detail
2022-07-25 20:41:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
brief introduction : What is master-slave replication , How to realize the separation of reading and writing , Read this article and you will understand !
Mind mapping
Preface
In many projects , Especially Internet projects , In the use of MySQL Master slave replication will be used 、 Read write separation architecture .
Why should we adopt a master-slave replication read-write separation architecture ? How to achieve ? What's the downside ? Let's start this learning journey with these questions !
Why use master-slave replication 、 Read / write separation
Master slave copy 、 Read write separation is usually used together . The goal is simple , Just for Improve the concurrent performance of database . Would you , Suppose it's a single machine , Reading and writing are all on the same set MySQL It's done , The performance is definitely not high . If there are three MySQL, a mater Write only , Two sets of salve Only responsible for reading operations , Can't performance be greatly improved ?
So master-slave replication 、 Read write separation is to support more concurrency in the database .
With the expansion of business volume 、 If it's a stand-alone deployment MySQL, It can lead to I/O The frequency is too high . Using master-slave replication 、 Read write separation can improve the availability of the database .
The principle of master-slave replication
① When Master Nodes carry out insert、update、delete In operation , It will be written to binlog in .
②salve Connect from library master Main library ,Master How many slave How many binlog dump Threads .
③ When Master Node binlog When something changes ,binlog dump The thread will notify all salve node , And the corresponding binlog Push the content to slave node .
④I/O Thread received binlog After content , Write content to local relay-log.
⑤SQL Thread reads I/O Thread writes relay-log, And according to relay-log The content of the database to do the corresponding operation .
How to realize master-slave replication
I use three virtual machines here (Linux) demonstration ,IP Namely 104(Master),106(Slave),107(Slave).
The expected effect is one master and two slaves , As shown in the figure below :
Master To configure
Using the command line to enter mysql:
1
mysql -u root -pThen the input root User's password ( If you forget the password, check the reset password online ~), And then create the user :
1
//192.168.0.106 yes slave Slave IP2
GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.0.106' identified by '[email protected]';3
//192.168.0.107 yes slave Slave IP4
GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.0.107' identified by '[email protected]';5
// Refresh the configuration of the system permission table 6
FLUSH PRIVILEGES; The two users created are configuring slave You need to use .The next step is to find mysql Configuration file for /etc/my.cnf, Add the following configuration :
1
# Turn on binlog2
log-bin=mysql-bin3
server-id=1044
# Databases that need to be synchronized , If not configured, synchronize all databases 5
binlog-do-db=test_db6
# binlog The number of days the log is kept , Clear more than 10 Day's diary 7
# Prevent log files from being too large , Cause disk space shortage 8
expire-logs-days=10When the configuration is complete , restart mysql:
1
service mysql restart You can do this from the command line show master status\G; View the current binlog Log information ( Behind the useful ):
Slave To configure
Slave The configuration is relatively simple . The slave must also be a MySQL The server , So and Master equally , find /etc/my.cnf The configuration file , Add the following configuration :
1
# Don't talk to other people mysql service id Just repeat 2
server-id=106 Then log in to... Using the command line mysql The server :1
mysql -u root -pThen type in your password and log in .
Enter into mysql after , Enter the following command again :
1
CHANGE MASTER TO 2
MASTER_HOST='192.168.0.104',// host IP3
MASTER_USER='root',// Previously created user account 4
MASTER_PASSWORD='[email protected]',// Previously created user password 5
MASTER_LOG_FILE='mysql-bin.000001',//master The host binlog Log name 6
MASTER_LOG_POS=862,//binlog Log offset 7
master_port=3306;// port It's not over yet. , After setting, you need to start :
1
# start-up slave service 2
start slave; How to check whether the startup is successful after startup ? Use the following command :1
show slave status\G; You can see the following information ( Extract some key information ):
Test master-slave replication
stay master Host execution sql:
1
CREATE TABLE `tb_commodity_info` (2
`id` varchar(32) NOT NULL,3
`commodity_name` varchar(512) DEFAULT NULL COMMENT ' Name of commodity ',4
`commodity_price` varchar(36) DEFAULT '0' COMMENT ' commodity price ',5
`number` int(10) DEFAULT '0' COMMENT ' The number ',6
`description` varchar(2048) DEFAULT '' COMMENT ' Commodity Description ',7
PRIMARY KEY (`id`)8
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=' Commodity information table '; Then we can see two slave Slave synchronization also creates product information tables :
Master slave replication is done !
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111542.html Link to the original text :https://javaforall.cn
边栏推荐
- How to obtain the subordinate / annotation information of KEGG channel
- 预处理指令
- [today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
- leetcode-6126:设计食物评分系统
- [advanced mathematics] [6] differential calculus of multivariate functions
- [matlab] download originality documents based on oil monkey script and MATLAB
- IEC61131 address representation
- Leetcode-155: minimum stack
- Remote - actual combat
- 2022.7.24-----leetcode.1184
猜你喜欢

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

Network protocol: TCP part2
![[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console

Leetcode-114: expand binary tree into linked list

KEGG通路的从属/注释信息如何获取

Kubernetes advanced part learning notes

Yolov7 training error indexerror: list index out of range

Kubernetes进阶部分学习笔记
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

Increase swap space
随机推荐
preprocessor directives
How to obtain the subordinate / annotation information of KEGG channel
TGA file format (waveform sound file format)
[advanced mathematics] [8] differential equation
[advanced mathematics] [5] definite integral and its application
Web crawler principle analysis "suggestions collection"
【TensorRT】动态batch进行推理
[advanced mathematics] [3] Application of differential mean value theorem and derivative
leetcode-6129:全 0 子数组的数目
How much memory does bitmap occupy in the development of IM instant messaging?
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
Behind every piece of information you collect, you can't live without TA
leetcode-146:LRU 缓存
Working principle of radar water level gauge and precautions for installation and maintenance
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
Jmeter——接口测试
Success factors of software R & D effectiveness measurement
Detailed explanation of document operation
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released