当前位置:网站首页>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
边栏推荐
- [today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
- [leetcode] 28. Implement strstr ()
- How to obtain the subordinate / annotation information of KEGG channel
- leetcode-79:单词搜索
- 结构体,枚举类型与联合体
- What is cluster analysis? Categories of cluster analysis methods [easy to understand]
- leetcode-6126:设计食物评分系统
- 预处理指令
- Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
- Introduction to several scenarios involving programming operation of Excel in SAP implementation project
猜你喜欢

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve

KEGG通路的从属/注释信息如何获取
![[advanced mathematics] [1] function, limit, continuity](/img/c5/f9fd3814a61d0fba24b37253c7e51c.png)
[advanced mathematics] [1] function, limit, continuity

How much memory does bitmap occupy in the development of IM instant messaging?

How to choose a microservice registration center?

预处理指令

Clickhouse notes 02 -- installation test clickvisual
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born

Brush questions with binary tree (4)

leetcode-114:二叉树展开为链表
随机推荐
Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
[advanced mathematics] [6] differential calculus of multivariate functions
Online XML to JSON tool
7.23
2022.7.24-----leetcode.1184
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
Struct, enum type and union
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
“链”接无限可能:数字资产链,精彩马上来!
JS作用域与作用域链
Increase swap space
wokerman 自定义写入日志文件
Web crawler principle analysis "suggestions collection"
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Interpretation of filter execution sequence source code in sprigboot
【单细胞高级绘图】07.KEGG富集结果展示
Is QQ 32-bit or 64 bit software (where to see whether the computer is 32-bit or 64 bit)
[MSA] a brief description of the moveit Configuration Assistant chain in planning groups
程序的编译和运行
Leetcode-6130: designing digital container systems