当前位置:网站首页>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 .
边栏推荐
- L2-007 家庭房产 (25 分)
- AcWing 1298. Solution to Cao Chong's pig raising problem
- Word typesetting (subtotal)
- Yarn installation and use
- B tree and b+ tree of MySQL index implementation
- QT creator support platform
- How to set up voice recognition on the computer with shortcut keys
- Solution to the practice set of ladder race LV1 (all)
- error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
- mysql实现读写分离
猜你喜欢
【yarn】Yarn container 日志清理
Vs2019 desktop app quick start
Vert. x: A simple TCP client and server demo
Request object and response object analysis
UDS learning notes on fault codes (0x19 and 0x14 services)
Valentine's Day flirting with girls to force a small way, one can learn
Linux yum安装MySQL
2019腾讯暑期实习生正式笔试
Composition des mots (sous - total)
wangeditor富文本引用、表格使用问题
随机推荐
Matlab learning and actual combat notes
Face recognition_ recognition
Antlr4 uses keywords as identifiers
小L的试卷
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
【Flink】CDH/CDP Flink on Yarn 日志配置
Those commonly used tool classes and methods in hutool
JS array + array method reconstruction
Common regular expression collation
[蓝桥杯2017初赛]方格分割
Funny cartoon: Programmer's logic
使用lambda在循环中传参时,参数总为同一个值
Stage 4 MySQL database
Pytoch Foundation
Word排版(小计)
[NPUCTF2020]ReadlezPHP
mysql实现读写分离
【flink】flink学习
Julia 1.6 1.7 common problem solving
MySQL and C language connection (vs2019 version)