当前位置:网站首页>Principle and implementation of MySQL master-slave replication
Principle and implementation of MySQL master-slave replication
2022-07-06 11:50:00 【Ride the wind to break the bug】
List of articles
What is master-slave replication ?
Master slave replication refers to the transfer of the master database DDL and DML Operations are passed to the slave database through binary logs , These logs are then re executed on the slave database , So that the data of the slave database and the master database are consistent .
The principle of master-slave replication
- Mysql When the transaction is committed, the master database records the data changes as events in the binary log Binlog in ;
- The main library pushes binary log files Binlog Relay logs from events in to slave libraries Relay log in , After that, the slave database redoes the data change operation according to the relay log , The consistency of the master and slave databases is achieved by replication ;
- Mysql Through three threads to complete the master-slave database data replication , among Binlog Dump Threads run on the main library ,I/O Threads and SQL The thread runs on the slave library ;
- When starting replication from the slave Library , First create I/O Threads connect to the main library , The master library is then created Binlog Dump Thread reads database events and sends them to I/O Threads ,I/O The thread gets the event and updates it to the relay log of the slave Library Relay log In the middle , And then from the library sql Thread read relay log Relay log Update database events and apply , As shown in the figure below :
Build the master instance
- function mysql Main instance
docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
- stay mysql Configuration folder /mydata/mysql-master/conf Create a configuration file in my.cnf:
touch my.cnf
- Modify the configuration file my.cnf, The configuration information is as follows
[mysqld]
## Set up server_id, You need to be unique in the same LAN
server_id=101
## Specify the name of the database that does not need to be synchronized
binlog-ignore-db=mysql
## Turn on binary log function
log-bin=wdj-mysql-bin
## Set the memory size of binary log ( Business )
binlog_cache_size=1M
## Set the binary log format to use (mixed,statement,row)
binlog_format=mixed
## Binary log expiration cleanup time . The default value is 0, Does not automatically clean up .
expire_logs_days=7
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave End copy interrupt .
## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database data is inconsistent
slave_skip_errors=1062
- Restart the instance after modifying the configuration
docker restart mysql-master
- Get into mysql-master In the container
docker exec -it mysql-master /bin/bash
- Use... In containers mysql To connect to the client
mysql -uroot -proot
- Create data synchronization users
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Build from instance
- function mysql From the example
docker run -p 3308:3306 --name mysql-slave \
-v /mydata/mysql-slave/log:/var/log/mysql \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
- stay mysql Configuration folder /mydata/mysql-slave/conf Create a configuration file in my.cnf
touch my.cnf
- Modify the configuration file my.cnf
[mysqld]
#server_id, You need to be unique in the same LAN
server_id=102
## Specify the name of the database that does not need to be synchronized
binlog-ignore-db=mysql
## Turn on binary log function , in preparation for Slave As an instance of another database Master When using
log-bin=wdj-mysql-slave1-bin
## Set the memory size of binary log ( Business )
binlog_cache_size=1M
## Set the binary log format to use (mixed,statement,row)
binlog_format=mixed
## Binary log expiration cleanup time . The default value is 0, Does not automatically clean up .
expire_logs_days=7
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave End copy interrupt .
## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database data is inconsistent
slave_skip_errors=1062
## relay_log Configure relay logs
relay_log=wdj-mysql-relay-bin
## log_slave_updates Express slave Write the copy event to your own binary log
log_slave_updates=1
## slave Set to read only ( have super Except for users with permission )
read_only=1
- Restart the instance after modifying the configuration
docker restart mysql-slave
Connect the master and slave databases
- Connect to the master database mysql client , View the status of the primary database
show master status;
The master database status is shown below :
- Get into mysql-slave In the container
docker exec -it mysql-slave /bin/bash
- Use... In containers mysql To connect to the client
mysql -uroot -proot
- Configure master slave replication in the slave database
change master to master_host='192.168.6.132', master_user='slave', master_password='123456', master_port=3307, master_log_file='wdj-mysql-bin.000001', master_log_pos=785, master_connect_retry=30;
- Master slave copy command parameter description
- master_host: The main database IP Address ;
- master_port: The running port of the primary database ;
- master_user: A user account created in the master database to synchronize data ;
- master_password: The user password created in the master database for synchronizing data ;
- master_log_file: Specify the log file to copy data from the database , By looking at the status of the master data , obtain File Parameters ;
- master_log_pos: Specify where to start copying data from the database , By looking at the status of the master data , obtain Position Parameters ;
- master_connect_retry: The connection failed and the time interval between retries , The unit is in seconds .
- Check the master-slave synchronization status
The above figure shows no Indicates that the connection has not been made - Turn on master-slave synchronization
start slave;
- Check the database status and find that it has been synchronized
Status has changed from no Turn into yes 了
Master slave replication test
There are many ways to test master-slave replication , You can create a database in the main instance , See if there is the database in the instance , If there is , Indicates that the master-slave replication has been successfully established .
- Create a database in the main instance testzhucong
After creating the database in the main database , Now check the database from the Library
The newly created database of the master database has also been created synchronously from the database , Since then, a simple Mysql The practical operation of master-slave replication .
边栏推荐
猜你喜欢
随机推荐
[mrctf2020] dolls
Punctual atom stm32f103zet6 download serial port pin
Gallery之图片浏览、组件学习
ES6 Promise 对象
ES6 promise object
DICOM: Overview
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Reading BMP file with C language
[Flink] cdh/cdp Flink on Yan log configuration
L2-004 这是二叉搜索树吗? (25 分)
[Blue Bridge Cup 2017 preliminary] buns make up
B tree and b+ tree of MySQL index implementation
Stage 4 MySQL database
使用LinkedHashMap实现一个LRU算法的缓存
vs2019 使用向导生成一个MFC应用程序
Solution to the practice set of ladder race LV1 (all)
encoderMapReduce 随手记
Mtcnn face detection
ES6 let and const commands
Valentine's Day flirting with girls to force a small way, one can learn