MySQL master-slave synchronization-binlog

MySQL master-slave synchronization-binlog

Server preparation

IP Node Configuration System version
191.168.117.143 master 2c2g40g centos 7.9
192.168.117.142 slave 2c2g40g centos 7.9

Environment preparation

The following operations need to be performed on both machines

Turn off firewall

systemctl stop firewalld & amp; & amp; systemctl disable firewalld
setenforce 0 & amp; & amp; sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

Install commonly used software

yum install -y wget vim net-tools bash-completion

Update software

yum update
Download mysql installation source
https://dev.mysql.com/downloads/repo/yum/

image-20230919092155207

Click Download according to the system version

Click to download the rpm package or right-click to copy the link

image-20230919092456556

wget https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm
rpm -ivh mysql80-community-release-el7-10.noarch.rpm

image-20230919093121744

Configure installation source
cd /etc/yum.repos.d/
vim mysql-community.repo

image-20230919093455820

Configure according to the required version number

image-20230919093759357

I installed 8.0 directly here, so there is no need to modify it.

Install mysql
yum -y install mysql-community-server

image-20230919094404877

Master node operation

Modify my.cnf configuration
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1

image-20230919111025313

Start the database and configure auto-start at boot
systemctl start mysqld & amp; & amp; systemctl enable mysqld
Get MySQL initialization password
grep password /var/log/mysqld.log

image-20230919094601606

Login to database

mysql -p'password' ##Note that special characters must be enclosed in single quotes

image-20230919094745766

Modify user permissions and slave copy users
alter user 'root'@'localhost' identified with mysql_native_password by 'Jianren@123';
grant all privileges on *.* to 'root'@'localhost';
create user 'slave'@'%' identified with mysql_native_password by 'Jianren@123';
grant replication slave on *.* to 'slave'@'%';
flush privileges;
Parameter explanation
with mysql_native_password Modify the user's authentication plug-in to prevent slave node login synchronization users from being unable to connect

image-20230919100317410

View binlog start log
show master status \G

image-20230919111807361

Slave slave node operation

Configure local domain name resolution
vim /etc/hosts
192.168.117.143 mysql-master

image-20230919101555481

Ping to test whether there is connectivity

ping mysql-master

image-20230919101713673

Modify my.cnf configuration

 vim /etc/my.cnf
server-id=2

image-20230919110352842

Start mysql and configure auto-start at boot
systemctl start mysqld & amp; & amp; systemctl enable mysqld
Modify mysql initialization password and permissions
grep password /var/log/mysqld.log

image-20230919101925512

Login to database

mysql -p'password' ##Note that special characters must be enclosed in single quotes

Configure passwords and permissions

alter user 'root'@'localhost' identified by 'Jianren@123';
grant all privileges on *.* to 'root'@'localhost' ;
flush privileges;
exit ;

image-20230919102313151

Test whether the user can log in to the master node

mysql -uslave -pJianren@123 -h mysql-master
show databases;
exit;

image-20230919102551422

Configuring master-slave replication

Log in as root user

mysql -p'Jianren@123'

edit

\e
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='slave',
MASTER_PASSWORD='Jianren@123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=157;
Parameter explanation
MASTER_HOST='mysql-master' The host IP of the master can also be a local domain name
MASTER_USER='slave' Synchronization user created by master
MASTER_USER='slave' master creates synchronized user password
MASTER_LOG_FILE='mysql-bin.000003' The binlog file name viewed by the master
MASTER_LOG_POS=157; masetr checks the log location of binlog

save

;

image-20230919112341626

image-20230919112246046

Start master-slave replication

start slave ;

image-20230919112557064

View status

 show slave stauts \G

Both threads are yes, indicating success.

image-20230919114209901

Test status

Enter in the master node mysq

create database test1;

Enter in slave node mysq

show databases;

You can see that it has been synchronized image-20230919114419085