MySQL master-slave setup: one master and one slave

Table of contents

1. Environment

2. Master-slave replication principle

3. Set up one master and one slave

1. Close the master-slave server firewall

2. Close the master-slave server SELINUX

3. Configure the main server

4. Configure the slave server

5. Verify master-slave synchronization

4、Freedom

1. apparmor service

2、caching_sha2_password

3、max_connect_errors


1. Environment

main server from server
operating system Rocky 8.8 Rocky 8.6
MySQL version 8.0.32 8.0.32
IP 10.0.0.151 10.0.0.152

Note: It is best that the MySQL versions of the master server and slave server are consistent. If they are inconsistent, compatibility issues need to be fully considered.

2. Master-slave replication principle

The MySQL master-slave replication architecture mainly includes 3 threads and 2 logs. The three threads are dump thread, IO thread, and SQL thread, and the two logs are binary log (binlog) and relay log (relay log).

1. When the master (main server) performs a write operation, it will generate a corresponding binary file, and all the writing processes are recorded in the binary file.

2. The master will provide a dump thread for the IO thread on each slave (slave server) node. The IO thread on the slave node will request the binary log content from the master, and then the master will provide the binary log to the slave node through the dump thread. Then the IO thread of the slave node writes it to the local replay log (relay log).

3. The SQL thread on the slave node monitors in real time whether the replay log content has been updated. If it is updated, the content in the file is parsed into a SQL statement and restored to the database on the slave node, so as to ensure that the relationship between the master and slave nodes is data synchronization.

3. Set up one master and one slave

1. Close the master-slave server firewall

Check the firewall status: systemctl status firewalld.service

 Turn off the firewall: systemctl stop firewalld.service

2. Close the master-slave server SELINUX

Enter the SELINUX configuration file:

vim /etc/selinux/config

Set the value of SELINUX to disabled:

 Then restart the server (must be restarted to take effect):

reboot

3. Configure the main server

a. Configure the master node log_bin (binary log file generation path) and server-id.

Server-id is used to uniquely identify the MySQL server. Each node in the master-slave architecture must have a different server-id to facilitate identification of their identities and roles.

Regarding server-id, here is a brief expansion: both the master and the slave can generate binary log files. It is possible for a write operation outside of synchronization to occur on the slave. So how to identify in the binary log that this write operation is synchronized by the master? Or do you do it yourself? Through the server-id, otherwise you don't know where the data is written.

vim /etc/my.cnf

#加入以下内容到my.cnf中
[mysqld]
server-id=151
log_bin=/data/mysql/logbin/mysql-bin

b. Because the binary log file path is a custom path that does not exist, it is necessary to create a path and add permissions to the file. And restart the MySQL service.

#注意我这里没有创建mysql-bin,它是用来当二进制日志文件的前缀的,不需要创建
mkdir -pv /data/mysql/logbin

#给文件添加属主属组,-R表示递归
chown -R mysql.mysql /data/mysql/

#重启MySQL服务
systemctl restart mysqld.service

c. Record the location of the binary log

The location here does not refer to the file path, but to where the binary log file starts to be synchronized. Enter show master status; in MySQL to view it.

 d. Create an account with copy permissions

#给10.0.0.0/24网段创建一个用户及密码,因为考虑到后面有其他同网段的主机做多从
create user slavenode@'10.0.0.%' identified by '123456';

#给这个用户授予复制权限,*.*表示所有库所有表
grant replication slave on *.* to slavenode@'10.0.0.%';

4. Configure the slave server

a. Modify the configuration file and restart the mysql service

vim /etc/my.cnf


#添加以下内容到my.cnf中
[mysqld]
log_bin=/data/mysql/logbin/mysql-bin
server-id=152
read_only=on
relay_log=relay_log
relay_log_index=relay-log.index

read_only=on: Set the database to read-only;

The path of log_bin is also created by myself. If you follow this path, you need to create it yourself, modify the owner and group, and restart the MySQL service.

#注意我这里没有创建mysql-bin,它是用来当二进制日志文件的前缀的,不需要创建
mkdir -pv /data/mysql/logbin

#给文件添加属主属组,-R表示递归
chown -R mysql.mysql /data/mysql/

#重启MySQL服务
systemctl restart mysqld.service

relay_log=relay_log: Specify the relay log file path, default /var/lib/mysql/
relay_log_index=relay-log.index: Relay log index file name

b. Use a user account with replication permissions to connect to the master server and start the replication thread

Connect to the slave server MySQL and enter show slave status. You can see that it is currently empty:

 Enter the following statement:

CHANGE MASTER TO MASTER_HOST='10.0.0.151',  #指定master节点
MASTER_USER='slavenode',                    #连接用户
MASTER_PASSWORD='123456',                   #连接密码
MASTER_LOG_FILE='mysql-bin.000001',         #从哪个二进制文件开始复制
MASTER_LOG_POS=157,                         #指定同步开始的位置
MASTER_DELAY = 2;                    #可指定延迟复制实现防止误操作,单位秒,这里可以用作 
                                             延时同步,一般用于备份,默认是10s

Then use show slave status\G (\G is displayed in a readable form):

You can see some information just configured, and you can find that the current IO thread and SQL thread are closed. The realization of master-slave synchronization needs to rely on these two threads, so they need to be turned on.

 c. Start the IO thread and SQL thread

Connect to MySQL and enter start slave to start 2 threads

#可以在后面跟线程名,开启具体线程;不跟线程名,则俩个线程都开启
start slave [IO_THREAD|SQL_THREAD];

Then enter show slave status\G to check whether the thread is started successfully. Here it is yes, indicating that the thread is started successfully.

Note: If the value of Slave_IO_Running here is connecting, it may be because the firewall of your main server is not turned off. Just enter systemctl stop firewalld.service on the main server. Then enter show slave status\G on the slave server to check whether the value is Yes.

 d. Check the main server dump thread

The dump thread does not require us to configure it manually. At this point, the master-slave configuration has actually been completed. Enter the main server MySQL and enter show processlist; view the dump thread:

show processlist;

5. Verify master-slave synchronization

The main server creates data:

#创建数据库test
create database test;

#进入test
use test;

#创建表
CREATE TABLE student (
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
age tinyint UNSIGNED,
height DECIMAL(5,2),
gender ENUM('M','F') default 'M'
)ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;

#插入数据
insert into student values(1,'tom',10,135,'M');

#查看插入的数据
select * from student;

View data from the server:

#查看所有数据库,发现test已经同步过来
show databases;

#进入test库
use test;

#查看student表
select * from student;

Master-slave comparison:

4、Freedom

I won’t go into details about the detailed process. Most of the configuration process is consistent with Rocky. You need to pay attention to the following points:

1. apparmor service

apparmor It can restrict a program's access to a set of listed files and permissions.

If you specify a new path and authorize it when modifying the configuration file, such as log_bin=/data/mysql/logbin/mysql-index, the permissions of this path will not be released by apparmor, so you will Report an error.

Solution:

#修改apparmor配置文件
vim /etc/apparmor.d/usr.sbin.mysqld

#加入下面俩行内容,位置随意(注意格式,逗号不要省略)
/data/mysql/logbin/ r,
/data/mysql/logbin/** rwk,

#重启apparmor
systemctl restart apparmor.service

2、caching_sha2_password

Starting from MySQL 8.0.4, the MySQL default authentication plug-in has been changed from mysql_native_password to caching_sha2_password  . The characteristic of mysql_native_password is that it does not require encryption verification, so its verification speed is very fast, but it is not safe.

So based on the above information, if the mysql version you are using is greater than 8.0.4, then when configuring the MySQL master-slave in Ubuntu, an error will probably be reported after turning on the slave node:

Last_IO_Error: Error connecting to source '[email protected]:3306'. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

The error report also explains the reason. The 'caching_sha2_password' authentication plug-in requires a secure connection. The implication is that the connection is not secure now.

Then change the identity authentication plug-in to mysql_native_password? This approach is probably possible.

However, it is not recommended because it is unsafe, so do this;

Add one more parameter when entering mysql on the slave server:

mysql -uroot -p123456 --get-server-public-key

Then add one more line when changing master to (reset slave all first):

CHANGE MASTER TO
MASTER_HOST='10.0.0.161',
MASTER_USER='slavebak',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=157,
get_master_public_key=1;

Then start slave.

3、max_connect_errors

max_connect_errors is used to specify the maximum number of unsuccessful connection attempts allowed. The default is 100. You can view it with the following command:

show global variables like '%max_connect_errors%';

Because I tried too many times to solve the problem in 2, probably more than 100 times, the following error occurred after starting slave:

Last_IO_Error: Error connecting to source '[email protected]:3306'. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Host '10.0.0.162' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
translates the following error: The host 10.0.0.162 is locked due to too many connection errors. Use the mysqladmin flush-hosts command to unlock it.

So I enter both databases:

flush hosts;

After restarting the slave, the IO thread is normal and no error is reported.

The above content is for reference only. If you have any questions, please send a private message or comment, thanks~

Guess you like

Origin blog.csdn.net/qq_54381110/article/details/131708243