MYSQL database - master-slave replication (principle and construction)

1 Overview

Master-slave replication refers to transferring the DDL and DML operations of the master database to the slave library server through binary logs, and then re-executes (also called redo) these logs on the slave library, so that the data in the slave library and the master library remains Synchronize.
MySQL supports one master database to replicate to multiple slave databases at the same time, and the slave database can also serve as the master database of other slave servers to realize chain replication.
insert image description here
The points of MySQL replication mainly include the following three aspects

  • 1. If there is a problem with the master library, it can quickly switch to the slave library to provide services.
  • 2. Realize the separation of reading and writing, and reduce the access pressure of the main library.
  • 3. The backup can be performed in the slave library to avoid affecting the main library service during the backup

2 principle

From the above figure, replication is divided into three steps:

1. When the master master library commits a transaction, it will record the data change in the binary log file Binlog
2. The slave library reads the binary log file Binlog of the master library and writes it to the relay log Relay Log of the slave library
3. The slave reloads Do relay the event in the log, which will change to reflect its own data

insert image description here

3 build

3.1 Main library configuration

1 Modify the configuration file /etc/my.cnf

#mysql服务ID,保证整个集群环境中唯一,取值范围: 1 - 232-1,默认为1
server-id=1
#是否只读,1 代表只读,0代表读写
read-only=0
#忽略的数据,指不需要同步的数据库
binlog-ianore-db=mysgl
#指定同步的数据库
binlog-do-db=db01

2. Restart the MySQL server

systemctl  restart mysqld

3. Log in to mysql, create an account for remote connection, and grant master-slave replication permission

#创建itcast用户,并设置密码,该用户可在任意主机连接该MySOL服务
CREATE USER 'test'@'%' IDENTIFIED WITH mysql native password BY Root@123456' ;
#为'test'@%' 用户分配主从复制权限
GRANT REPLICATION SLAVE ON ** TO 'test'@'%':

4. Check the binary log coordinates by command

show master status ;
field Explanation of meaning:
file From which log file to start pushing log files
position Where to start pushing logs
binlog_ignore_db Specify databases that do not need to be synchronized

3.2 Slave library configuration

1. Modify the configuration file /etc/my.cnf

#mysql服务ID,保证整个集群环境中唯一,取值范围: 1 - 232-1,和主库不一样即可
server-id=2
#是否只读,1 代表只读,0 代表读写
read-only=1

2. Restart the MySQL service

systemctl restart mysald

3. Log in to mysql, set the main library configuration

CHANGE REPLICATION SOURCE TO SOURCE_HOST='主库ip地址', SOURCE USER='主库用户名', SOURCE_PASSWORD='密码',SOURCE_LOG FILE='日志文件', SOURCE LOG_POS='biglog日志位置',

The above is the syntax in 8.0.23. If mysgl is a version before 8.0.23, execute the following SQL

CHANGE MASTER TO MASTER_HOST='xx' ,MASTER_USER='x',MASTER_PASSWORD='x',MASTER_LOG_FILE='x', MASTER_LOG_POS=:
parameter name meaning Before 8.0.23
SOURCE_HOST Main library IP address MASTER_HGST
SOURCE_USER The username to connect to the main library MASTER_USER
SOURCE_PASSWORD Password to connect to the main library MASTER_PASSWORD
SOURCE_LOG_FILE binlog log file name MASTER_LOG_FILE
SOURCE_LOG_POS Binlog log file location MASTER_LOG_POS

4. Turn on the synchronous operation
and enter the command in mysql

#8.0.22之后
start replica;
#8.0.22之前
start slave;

5. Check the master-slave synchronization status

#8.0.22之后
show replica status :
#8.0.22之前
show slave status ;

Guess you like

Origin blog.csdn.net/qq_46645840/article/details/129027428