当前位置:网站首页>MySQL performance optimization (5): principle and implementation of master-slave synchronization
MySQL performance optimization (5): principle and implementation of master-slave synchronization
2022-06-30 03:27:00 【The undead bird Alexander. brutal young person】
1 What is master-slave synchronization
MySQL Master-slave synchronization means that data can be synchronized from one MySQL The database server master node is replicated to one or more slave nodes .MySQL Asynchronous replication is used by default , In this way, the slave node does not need to visit the master server all the time to update its data , Data can be updated on a remote connection , The slave node can copy all databases in the master database or specific databases , Or a specific watch .
2 Application scenarios
2.1 Read / write separation
In the development work , Sometimes I meet someone sql Statement needs lock table , Resulting in temporary inability to use the read service , This will affect the existing business , Use master-slave replication , Let the main database be responsible for writing , Read from the library , such , Even if the main database appears to lock the table , The normal operation of the business can also be ensured by reading from the database . When the reading pressure increases , You can also configure multiple slave databases , Share the pressure of reading data together , Improve the stability of the system .
2.2 Architecture extension
With the increase of business access in the system , If it is a stand-alone deployment database , It will lead to I/O Access frequency is too high . With master-slave replication , Add multiple data storage nodes , Distribute the load across multiple slave nodes , Reduce the cost of stand-alone disk I/O Frequency of visits , Improve the... Of a single machine I/O performance .
3 Schematic diagram

Operation principle after master-slave synchronization :
The main database is generally only responsible for adding, deleting and modifying , Rarely responsible for inquiry , The database is only responsible for making queries , You are not responsible for adding, deleting, or modifying , At this time, the data in the master database will change in some way , Automatically synchronize to all slave databases , Finally, keep the data consistent
- Master Open a thread in , Record data changes to a binary log file (binary log) in
- Slave Open one in IO Threads are dedicated to reading Master Binary log contents in , And write the data into your own playback log file (relay log) in
- Slave in SQL The thread reads the playback log (relay log) The content of , hold Master Implemented in DML Played back , Complete master-slave synchronization
Be careful :
There is a certain delay in master-slave synchronization , Therefore, there is no guarantee that the master database synchronization information can be read from the database immediately , Therefore, as long as the write operation is involved in the business method , Then the operation must be performed on the primary database
4 Implementation steps
preparation :
First, let Master and Slave All have the same database , And the table data in the database is consistent , I use my own here skywalking library .
1、 stay Master In the configuration file [mysqld] Some of the following configurations are made , Turn on binary-log
# Configure the current server id
server-id=1
# Configure binary log file name
log-bin=master-bin
# Configure which database to log , You can configure it multiple times
binlog-do-db=skywalkingAnd then restart MySQL After service , Log in again to see how to use SQL Command to view information , The following contents are displayed to show that the first step is successful
-- View the status of the primary database
show master status;
-- Be careful :bin-log The file name of will change with the restart of the master server 
2、 stay Master Create a new user in the database for master-slave synchronization
-- You can use a graphical interface to operate
-- Create user slave For master-slave synchronization , And conferred rbac Database operation permission
GRANT ALL ON `skywalking`.* TO 'slave'@'%' IDENTIFIED BY '1'
-- Grant master-slave synchronization permission , This permission is a global permission
GRANT replication slave ON *.* TO 'slave'@'%'
-- Refresh the permissions
flush privileges3、 stay Slave In the configuration file [mysqld] Some of the following configurations are made , Then restart the database service
# Configure slave id, It must be the only one that cannot repeat
server-id=2
# Which database logs are played back , You can configure it multiple times
replicate-do-db=skywalking4、 Sign in Slave In the implementation of SQL
CHANGE MASTER TO
MASTER_HOST='192.168.222.131',
MASTER_USER='slave',
MASTER_PASSWORD='1',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=154 among :
MASTER_HOST Primary service ip Address ,
MASTER_USER The user name of the master-slave replication created above ,
MASTER_PASSWORD Password for the user created above ,
MASTER_LOG_FILE by bin-log The name of the file ,
MASTER_LOG_POS Is the length position of the log file
Execute... On the main database
show master status; see bin-log File name and length of log file

File For the file name ,Position Is the length position of the log file
Be careful :
If the master-slave synchronization has been enabled in the slave database, you need to close it first , To execute the above configuration
stop slaveStart master-slave synchronization after configuration
start slaveCheck the database status after startup :
-- View from database status
show slave status
notice Slave_IO_Running and Slave_SQL_Running All for YES It means success
If there is an error , Please check the corresponding Last_Error_Log

边栏推荐
- On the role of database tables
- [QT] QMap使用详解
- 【常见问题】浏览器环境、node环境的模块化问题
- TiDB 6.0:讓 TSO 更高效丨TiDB Book Rush
- Huawei interview question: tall and short people queue up
- QT中foreach的使用
- Huawei interview question: divide candy
- 51单片机的室内环境监测系统,MQ-2烟雾传感器和DHT11温湿度传感器,原理图,C编程和仿真
- [practical skills] how to write agile development documents
- hudi记录
猜你喜欢

Mathematical solution of Joseph Ring
![[live broadcast notes 0629] Concurrent Programming II: lock](/img/5c/42f5c9a9969b4d2bb950a7caac5555.png)
[live broadcast notes 0629] Concurrent Programming II: lock

Hisense A7 ink screen mobile phone cannot be started

Golang BiliBili live broadcast bullet screen
![[ten minutes] manim installation 2022](/img/54/7b895d785c7866271f06ff49cb20aa.png)
[ten minutes] manim installation 2022

1148_ Makefile learning_ Targets, variables, and wildcards in makefile

Implementation of property management system with ssm+ wechat applet

一篇文章带你入门vim

LitJson解析 生成json文件 读取json文件中的字典

F1C100S自制开发板调试过程
随机推荐
一篇文章带你入门vim
4-4 beauty ranking (10 points)
Mysql性能优化(6):读写分离
4-5 count words and spaces (15 points)
*Write a program to initialize a string object with a vector < char> container*/
简单自定义mvc
1148_ Makefile learning_ Targets, variables, and wildcards in makefile
实用调试技巧
个人PC安装软件
Are you a "social bull" or a "social terrorist" in the interview?
C # [advanced part] C # multithreading
F1c100s self made development board debugging process
A GPU approach to particle physics
MySQL + JSON = King fried
Global and Chinese market for nasal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
[qt] qmap usage details
1152_ Makefile learning_ Pattern matching rules
OP diode limit swing
1151_ Makefile learning_ Static matching pattern rules in makefile
124 articles in total! Motianlun "high availability architecture" dry goods document sharing (including Oracle, mysql, PG)