MySQL master-slave replication: achieving high data availability and load balancing

1. Quotes

MySQL master-slave replication is a commonly used database replication technology that can achieve high data availability and load balancing. Through master-slave replication, we can copy data from a MySQL master server to one or more slave servers, thereby achieving redundant backup of data and separation of read and write. This article will introduce the principles, configuration steps, and solutions to common problems of MySQL master-slave replication.

2. The principle of master-slave replication

  • The master server (Master) is responsible for receiving and processing all write operations, and recording the write operations to the binary log (Binary Log).
  • The slave server (Slave) connects to the master server and copies the data change operations on the master server by reading the binary log of the master server.
  • The slave server applies the received data change operations to its own database to maintain data consistency with the master server.

3. Basic steps of master-slave replication

The basic process of traditional master-slave replication is as follows: 
1) The IO process on the Mysql Slave side connects to the Master and requests the Master for the specified location of the specified log file (or from the beginning log); 
2) After the Master receives the request from the Slave’s IO process, the IO process responsible for replication reads the corresponding log content based on the Slave’s request information and returns it to Slave's IO process. And return the bin-log file name and location read by this request to the Slave side. 
3) After the Slave's IO process receives the information, it will add the received log content to the end of the relay-log file on the Slave side, and read the bin-log file on the Master side. The file name and location of the log are recorded in the master-info file, so that the Master can be clearly told the next time it is read, "I need to start the next log content from a certain bin-log location, please send it to me." ; 
4) After Slave’s Sql process detects the newly added content in relay-log, it will immediately parse the content of relay-log and become the executable content when it is actually executed on the Master side. , and execute on itself.


Simply put:
Main: I/O process

Slave: I/O process The slave server communicates with the I/O process of the master server through the I/O process, synchronizes the BINLOG log of the master server, and writes data to the slave database through the SQL process according to the content of the BINLOG log.

4. Experimental process

1. Prepare the environment

Prepare two clean virtual machines (CentOS 7). Cloning is not recommended and they must be on the same network segment. Each virtual machine must install the MySQL 5.7 version database.

Turn off the firewall and SELinnx before experimenting

systemctl stop firewalld       #关闭防火墙
setenforce  0                  #关闭selinux

Rename the two machines master and slave respectively.

hostnamectl set-hostname master
hostnamectl set-hostname slave

 Perform mapping processing on the two machines and perform IP resolution to facilitate the connection between the two machines, but this step is not mandatory and is not mandatory.

vim到/etc/hostd文件下,再尾行添加上
master的ip地址       master
slave的ip地址        slave
#两台机器都要分别添加上

 Once this is done, our environment is ready, and then we can configure it.

2. master configuration

Modify configuration file

在/etc/my.conf目录下添加
log-bin=/var/lib/mysql/master
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1 

After the addition is completed, we need to restart the mysql service

systemctl restart mysqld

After the mysql database is restarted, we need to log in to create a user in mysql and authorize it.

grant replication slave,super,reload on *.* to slave@'%' identified by '创建用户的密码';

After completing the above operations, the configuration of the master is completed, and then we can configure the slave.

3. Configure slave

Modify configuration file

在/etc/my.conf目录下添加
log-bin=/var/lib/mysql/master
server-id=2
gtid_mode=ON
enforce_gtid_consistency=1 

After completing the configuration, we also need to restart the mysql database.

systemctl restart mysqld

Just like configuring the master, we also need to log in to mysql, but instead of creating a user, we connect to the mysql user in the master.

change master to master_host='slave',master_user='slave',master_password='密码',master_auto_position=1;

 Turn on slave

start slave

 After turning it on, we can use the show command to check whether it is successful.

show slave status \G

When you see yes below, it means success.

We can now write on the master host and synchronize it to the slave host, achieving read-write separation and backup.

5. Problems in master-slave replication experiments

We may encounter many problems during the master-slave replication experiment. Let me talk about the common problems below.

1. There is an error in the way of doing gtid in the new version of mysql.

First check the current gtid number on the master

mysql> show master status\G
....
Executed_Gtid_Set: 130fc529-a688-11ec-9793-000c2922001e:1-2,f5d2ff8f-a688-11ec-981d-000c29a7f0ed:1-2
....

Operate on slave 

# mysql -u root -p'密码'
mysql> set @@global.GTID_PURGED='130fc529-a688-11ec-9793-000c2922001e:1-2,f5d2ff8f-a688-11ec-981d-000c29a7f0ed:1-2';

如果master上拿到的是一个gtid,那么按照上面的语法,重复两次就可以,比如:
mysql> set @@global.GTID_PURGED='f24c3ee0-7a88-11ed-a20e-000c29988ddf:1-3,f24c3ee0-7a88-11ed-a20e-000c29988ddf:1-3';

2. Mysql server uuid duplication problem, because cloning causes no change in the content of the file recording uuid

解决:
1.全新机器 
或者
2.直接修改文件/var/lib/mysql/auto.cnf 内的uuid 
使用命令 # uuidgen  生成全新uuid直接替换文件内的旧uuid即可


 6. Conclusion

MySQL master-slave replication is a powerful database replication technology that can provide high availability of data, redundant backup and load balancing. By correctly configuring and managing master-slave replication, we can ensure data consistency and reliability and improve system performance and availability. I hope this article will help you understand and implement MySQL master-slave replication, and can play a role in your project.

If you have other questions about MySQL master-slave replication or need more in-depth study, it is recommended that you continue to explore MySQL's official documentation and related resources to obtain more guidance and practical experience. I wish you success in using MySQL master-slave replication!

Guess you like

Origin blog.csdn.net/XX_HK/article/details/134340949