【A Hundred Refinements to Become a Demon】MySQL master-slave synchronization-Gtid
Server preparation
IP | Node | Configuration | System version |
---|---|---|---|
191.168.117.143 | master | 2c2g40g | centos 7.9 |
192.168.117.142 | slave | 2c2g40g strong> | 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/
Click Download according to the system version
Click to download the rpm package or right-click to copy the link
wget https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm
rpm -ivh mysql80-community-release-el7-10.noarch.rpm
Configure installation source
cd /etc/yum.repos.d/ vim mysql-community.repo
Configure according to the required version number
I installed 8.0 directly here, so there is no need to modify it.
Install mysql
yum -y install mysql-community-server
Master node operation
Modify my.cnf configuration
vim /etc/my.cnf
[mysqld] log_bin=mysql-bin server_id=1 binlog_format=ROW gtid_mode=on enforce_gtid_consistency=on
Parameter explanation log_bin=mysql-bin This parameter enables binary logging server_id=1 per MySQL This parameter specifies the identifier of the current MySQL server. In MySQL replication and clustering environments, each server should have a unique identifier for data synchronization and identification of different servers. binlog_format=ROW This parameter defines the format of the binary log, "ROW". This means that the binary log will record row-level details for each change, including the actual contents of the data row that was modified. This format is commonly used for replication and advanced data recovery operations. gtid_mode=on This parameter enables GTID mode enforce_gtid_consistency=on This parameter is used to enforce GTID consistency. When enabled, MySQL will ensure that transactions submitted on the master server are executed on the slave server to ensure consistent data synchronization.
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
Login to database
mysql -p'password' ##Note that special characters must be enclosed in single quotes
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
Slave slave node operation
Configure local domain name resolution
vim /etc/hosts
192.168.117.143 mysql-master
Ping to test whether there is connectivity
ping mysql-master
Modify my.cnf configuration
vim /etc/my.cnf
server-id=2 gtid_mode=on enforce_gtid_consistency=on
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
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 ;
Test whether the user can log in to the master node
mysql -uslave -pJianren@123 -h mysql-master show databases; exit;
Configuring master-slave replication
Log in as root user
mysql -p'Jianren@123'
edit
edit
CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_USER='slave', MASTER_PASSWORD='Jianren@123', MASTER_AUTO_POSITION=1;
Parameter explanation CHANGE MASTER TO MASTER_HOST='mysql-master', master node IP or domain name MASTER_USER='slave', the synchronization user created by the master node MASTER_PASSWORD='Jianren@123', synchronize user passwords MASTER_AUTO_POSITION=1; Automatically locate the current binary log position of the master server.
save
;
Start master-slave replication
start slave ;
View status
show slave stauts \G
Both threads are yes, indicating success.
Test status
Enter in the master node mysq
create database test1; use test1; create table usertb (username varchar(10)not null, password varchar(20)not null); insert into usertb values ('user1','123'); select * from usertb;
Enter in slave node mysq
show databases; use test1; select * from usertb;
You can see that it has been synchronized
At this point, the gtid method has also been deployed.
Delete test library master node operation
drop database test1;