mysql(8.0)_master-slave replication

1. Environment introduction

自己的主机--master
同学的主机--slave

2. Install mysql

https://blog.csdn.net/weixin_45955039/article/details/130144515?spm=1001.2014.3001.5501

3. Preparations

3.1 Add the port number on the cloud server

insert image description here
insert image description here

3.2 Turn off the firewall

systemctl stop firewalld
setenforce 0

4. Configuration on the master

4.1 Add the following content in the file /etc/my.cnf, remember to add it under [mysqld]

vim /etc/my.cnf
server-id=1
log-bin=mysql-bin

insert image description here

4.2 Restart the mysql service

systemctl stop mysqld
systemctl start mysqld

4.3 Check if the service id is 1

登录mysql
mysql -uroot -p123456
查看id
SHOW VARIABLES LIKE 'server_id';

insert image description here

4.4 Give permission to copy from the host

更改密码,先修改密码规则
set global validate_password.policy=0;
set global validate_password.length=1;
创建用户(这里不要建议使用root用户)
CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;

insert image description here

4.5 View the status of the main library

Be sure to remember the name and location of the log file here

show master status;

insert image description here

5.slave installation and configuration

5.1 Add the following content in the file /etc/my.cnf, remember to add it under [mysqld]

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

insert image description here

5.2 Start mysql service

systemctl stop mysqld
systemctl start mysqld

5.3 Check if the service id is 2

登录mysql
 mysql -uroot -p123456
SHOW VARIABLES LIKE 'server_id';

insert image description here

5.4 Specify master IP, user name, password, bin-log file name and position for slave

I don’t know the file name and position here to see 3.5

更改密码,先修改密码规则
set global validate_password.policy=0;
set global validate_password.length=1;
制定主节点
CHANGE MASTER TO
MASTER_HOST='101.43.248.178',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=849;

illustrate:

MASTER_HOST/SOURCE_HOST: the host ip of the master database
MASTER_PORT/SOURCE_PORT: the port of the master database, if not set, the default is 3306
MASTER_USER/SOURCE_USER: the user name of the master database granted synchronous replication permission
MASTER_PASSWORD/SOURCE_PASSWORD: the corresponding user password
MASTER_LOG_FILE/SOURCE_LOG_FILE: The name of the binary log file
MASTER_LOG_POS/SOURCE_LOG_POS queried by executing the command show master status on the master database : the value of the position Position queried by executing the command show master status on the master database
insert image description here

5.5 Start the slave service

start slave;

insert image description here

5.6 Check whether the slave service is started successfully

Both threads are yes to succeed

show slave status\G

insert image description here

6. Test

6.1 Create a tongbu library on the master and create a master table in the tongbu library

create database if not exists tongbu;
use tongbu;
CREATE TABLE IF NOT EXISTS master(
   info varchar(20) 
);

6.2 Go to the server to see if there is a synchronization library

insert image description here

6.3 Go to the server to see if there is a table master in the tongbu library

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45955039/article/details/130170116