当前位置:网站首页>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
边栏推荐
- JS作用域与作用域链
- JS scope and scope chain
- Rand1 generates rand9
- 数据库清空表数据并让主键从1开始
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- [today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
- Leetcode-155: minimum stack
- Interpretation of filter execution sequence source code in sprigboot
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- 程序的编译和运行
猜你喜欢

The database empties the table data and makes the primary key start from 1

IEC61131 address representation

数据库清空表数据并让主键从1开始
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay

MySQL 日期【加号/+】条件筛选问题
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

Leetcode-6130: designing digital container systems

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

Leetcode-6125: equal row and column pairs

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
随机推荐
[advanced mathematics] [3] Application of differential mean value theorem and derivative
Docker builds redis cluster
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
文件操作详解
Introduction to MySQL engine and InnoDB logical storage structure
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
Why did I choose to become a network engineer after graduating from weak current for 3 months
Implementation of simple registration and login
[technical dry goods] how to ensure the idempotency of the interface?
leetcode-114:二叉树展开为链表
Leetcode-6130: designing digital container systems
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
JS作用域与作用域链
Leetcode customs clearance: hash table six, this is really a little simple
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
JS scope and scope chain
How to obtain the subordinate / annotation information of KEGG channel
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP