Easily implement MySQL master-slave replication

What is master-slave replication?

It means that one MySQL database server acts as the master, and another or more database servers act as slaves. The master is responsible for mainly writing data, and the slave is responsible for only reading data.

What is the principle of master-slave replication?

The master-slave replication between MySQL servers is mainly implemented through binary log files. The host is responsible for using binary log files to record changes in database data, while the slave maintains data consistency with the host by reading and executing binary log files .

What are the advantages of master-slave replication?

  • Because reading data and writing data are executed in different servers, the pressure of reading and writing data on the same server is shared, which separates the reading and writing of the database and improves the performance of data reading and writing.
  • The data consistency between the slave machine and the host machine can be backed up on the server without destroying the corresponding data of the host machine, improving data security
  • When the master machine is down, a slave machine will be used as the master machine to continue running without affecting the reading and writing of data

MySQL master-slave replication process

The following process is configured in the Alibaba Cloud server, using docker to pull the MySQL image, and creating two MySQL containers with different ports.

① Prepare two MySQL servers (one as the master and the other as the slave)
  • Create a master server (host)
sudo docker run --name mysql_master -p 3307:3306  -d mysql:5.7.22 -e 
MYSQL_ROOT_PASSWORD=123456 

mysql:5.7.22: Indicates the version of the pulled image. It is recommended to use version 5.7, which is relatively stable.
MYSQL_ROOT_PASSWORD: Indicates setting the password of the database, which must be customized when creating it.
-p: Indicates the binding port. Before the colon is the port that can be accessed through the external network, and after the colon is the port of the database service.

Tips: No new port will be created in your own server, you need to add a new port yourself to create it successfully.

  • Create a slave server (slave)
sudo docker run --name mysql_slave -p 3308:3306 -d mysql:5.7.22 -e 
MYSQL_ROOT_PASSWORD=123456 
  • Check if created successfully
sudo docker ps

If you can see the newly created container and it is running, it means the creation is successful; otherwise, delete the container and create it again.

② Configure the main server
  • First enter mysql_masterthe container named
sudo docker exec -it mysql_master /bin/bash
  • Modify the configuration file /etc/mysql/mysql.conf.d/mysqld.conf
# 在末尾加上两个配置

## 设置server_id,一般设置为IP的最后一位,同一局域网内注意要唯一
server_id = 3

## 开启二进制日志功能,可以随便取,最好有含义(关键就是这里了)
log-bin = edu-mysql-bin

log-bin: Indicates that the binary log function is enabled, and the name must be meaningful (key configuration)
server_id: The id of the host, this value must be unique.

tips: If there is no vim command, you need to install it.
①apt-get update
②apt install vim

  • Restart mysql after the configuration is complete
service mysql restart
  • reopen the container
docker container start mysql_master
  • Enter the mysql database to create a data synchronization user
mysql -u root -p -h xx.xxx.xxx.xxx --port=3307

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; 

-h: The value of this parameter needs to be changed to the external network ip address of your own server

③ Configure the slave server
  • First enter mysql_slavethe container named
sudo docker exec -it mysql_slave /bin/bash
  • Modify the configuration file /etc/mysql/mysql.conf.d/mysqld.conf
# 在末尾加上两个配置

## 设置server_id,一般设置为IP的最后一位,同一局域网内注意要唯一
server_id = 4

## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin = edu-mysql-bin

Tips: In the slave, you don’t need to configure the parameter log-bin, if you want to configure it, it must be the same as the value of the master. The function of this parameter: When the master is down, the slave configured with this parameter in the parameter will be selected as the new master.

  • Restart the service, restart the container
service mysql restart

docker container start mysql_slave
④ Complete the Master and Slave link
  • Enter the host to view the status of the host
mysql -u root -p -h xx.xxx.xxx.xxx --port=3307

show master status;


注意:记录下 File 和 Position 字段的值,后面需要使用

  • Enter the slave to connect
mysql -u root -p -h xx.xxx.xxx.xxx --port=3308

change master to master_host='xx.xxx.xxx.xxx', master_user='slave', master_password='123456', 
master_port=3307, master_log_file='edu-mysql-bin.000001', master_log_pos=34659, 
master_connect_retry=30;

master_host: Master’s IP address
master_user: User authorized for data synchronization in Master
master_password: Password of user who synchronizes data
master_port: Port number of Master’s database
master_log_file: Specifies which log file the Slave starts to copy data from, that is, the File field mentioned above Value
master_log_pos: From which Position to start reading, that is, the value of the Position field mentioned above

  • View slave synchronization status
show slave status \G;


Slave_IO_RunningWhen the values ​​of and Slave_SQL_Runningare both No, it means that the copying process has not started yet, and if both are Yes, it means that the copying process has started.

  • Start master-slave replication
start slave;

tips: test method, use Navicat to connect to the master-slave database respectively, write data on the master, when the slave synchronizes to the master data, it means the master-slave replication is successful.


Related articles: [Django Implementation of Read-Write Separation Tutorial](https://www.jianshu.com/p/f2caab1712fa)

おすすめ

転載: blog.csdn.net/qq_42349944/article/details/103343033