当前位置:网站首页>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 .
边栏推荐
猜你喜欢
2019腾讯暑期实习生正式笔试
{one week summary} take you into the ocean of JS knowledge
分布式節點免密登錄
vs2019 第一个MFC应用程序
Machine learning notes week02 convolutional neural network
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
4、安装部署Spark(Spark on Yarn模式)
Small L's test paper
人脸识别 face_recognition
【flink】flink学习
随机推荐
搞笑漫画:程序员的逻辑
ES6 let 和 const 命令
Niuke novice monthly race 40
Distribute wxWidgets application
ES6 let and const commands
快来走进JVM吧
【yarn】Yarn container 日志清理
[Bluebridge cup 2020 preliminary] horizontal segmentation
Password free login of distributed nodes
{一周总结}带你走进js知识的海洋
数据库面试常问的一些概念
AcWing 1298. Solution to Cao Chong's pig raising problem
【yarn】CDP集群 Yarn配置capacity调度器批量分配
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
L2-001 emergency rescue (25 points)
L2-007 family real estate (25 points)
Funny cartoon: Programmer's logic
express框架详解
Wangeditor rich text component - copy available
使用lambda在循环中传参时,参数总为同一个值