当前位置:网站首页>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

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 :
     Insert picture description here

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 :
 Insert picture description here

  • 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
  1. master_host: The main database IP Address ;
  2. master_port: The running port of the primary database ;
  3. master_user: A user account created in the master database to synchronize data ;
  4. master_password: The user password created in the master database for synchronizing data ;
  5. 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 ;
  6. master_log_pos: Specify where to start copying data from the database , By looking at the status of the master data , obtain Position Parameters ;
  7. master_connect_retry: The connection failed and the time interval between retries , The unit is in seconds .
  • Check the master-slave synchronization status
     Insert picture description here
    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
     Insert picture description here
    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
     Insert picture description here
    After creating the database in the main database , Now check the database from the Library
     Insert picture description here
    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 .
原网站

版权声明
本文为[Ride the wind to break the bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913128739.html