Linux MYSQL8.0 database installation --> MYSQL master-slave node configuration --> MYSQL master-slave switching detailed tutorial

Table of contents

1. Preparation

1. Turn off the firewall and SELINUX safe mode

2. Upload the installation package to /usr/local/mysql

2. Install MYSQL database

1. Unzip the package and start installation

2. Install in sequence

 3. Start and enter the database configuration

3. MYSQL master-slave node configuration

1. Host configuration (master)

2. Configure the slave library (slave) 

3. Master-slave association configuration

3. MySQL master-slave switching


1. Preparation

I am using the RPM package to install it. You can go to the mysql official website to download it or use the package from my network disk.

Download the installation package from the official website: MySQL :: Download MySQL Community Server (Archived Versions)

Baidu network disk link: https://pan.baidu.com/s/1vyHmyJNojTetuFZKnLNYPw?pwd=aqws 
Extraction code: aqws

1. Turn off the firewall and SELINUX safe mode

#关闭防火墙并设置开机不启动
systemctl stop firewalld
 
systemctl disable firewalld
 
#关闭SElinux安全模式
setenforce 0
    
#设置永久关闭selinux
 
vi /etc/selinux/config
 
#修改SELINUX:
SELINUX=disabled
 
#重启生效
reboot

2. Upload the installation package to /usr/local/mysql

#创建一个放包的路径
mkdir -p /usr/local/mysql

2. Install MYSQL database

1. Unzip the package and start installation

#进入包在的路径
cd  -p /usr/local/mysql

#解压
tar -xvf mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar

2. Install in sequence

rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm

#安装此包时会报错(看下图)
#是原来自带的,卸了即可
rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm

#卸载
yum remove mysql-libs

#再次安装
rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm

#把一些依赖安装一下
sudo yum install -y libaio

yum install -y net-tools.x86_64


#继续安装包
rpm -ivh mysql-community-libs-compat-8.0.28-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm

#最后一一个包 要语句后面加上 --nodeps --force,安装时忽
略依赖关系
rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm --nodeps --force

 3. Start and enter the database configuration

#启动
systemctl start mysqld

#查看初始密码(要先启动才有)
cat /var/log/mysqld.log |grep password

#登陆并修改密码(密码自取,有大小写字令母符号数字)
mysql -u root -p

alter user 'root'@'localhost' identified by 'password';

#配置远程连接
use mysql;

update user set host='%' where user='root';

#刷新才生效
flush privileges;

#最好再重启一下数据库
systemctl restart mysqld

3. MYSQL master-slave node configuration

Prepare two stand-alone MYSQL databases. If the slave database is cloned from the main database, the IP addresses of the two databases and auto.cnf
The UUID in must be changed to be different.
If it's not a clone, ignore it.
Just modify the UUID of one library, as long as the UUIDs of the two libraries are different.
#执行命令查看 auto.cnf 文件的位置
find -name auto.cnf  #执行命令后没有的话,看最后一行
vi 到上面命令查询到的地址,修改 uuid 的值,随便改,只要不一样就可以了
#server-uuid=sgsgsg455sfsfgsg4
#修改后,进库查看主从两库的 UUID 是不是不一样
mysql -u root -p
show variables like '%server_uuid%';


#如改后还是一样,在/var/lib/mysql 可能也有一个 auto.cnf,修改一下即可
vi /var/lib/mysql/auto.cnf

#修改后,进库查看主从两库的 UUID 是不是不一样
mysql -u root -p
show variables like '%server_uuid%';

1. Host configuration (master)

进入 /etc/my.cnf 添加或修改配置
vi /etc/my.cnf

#添加配置如下
#在[mysqld]下添加
server-id = 1 #节点 ID,确保唯一。建议放在[mysqld]下的首行,不然可能会有报错
log-bin = mysql-bin #binlog 日志位置

#以下参数选填(可不填)
binlog_format = mixed #binlog 日志格式,mysql 默认采用 statement,建议使用 mixed
expire_logs_days = 7 #binlog 过期清理时间
max_binlog_size = 100m #binlog 每个日志文件大小
binlog_cache_size = 5m #binlog 缓存大小
max_binlog_cache_size= 256m #最大 binlog 缓存大
binlog-ignore-db=mysql #不生成日志文件的数据库,可用逗号拼接
binlog-do-db= 数据库名 #需要同步的数据库
binlog-ignore-db = 数据库名 #不需要同步的数据库

Restart MYSQL service

service mysqld restart

Enter the database and view the master information. Note down the File and Position information, which will be used later. You can copy them and put them in the txt document first.

mysql -u root -p

show master status;

2. Configure the slave library (slave) 

vi /etc/my.cnf

#在[mysqld]下添加
server-id=2 #节点 ID,确保唯一。建议放在[mysqld]下的首行,不然可能会有报错
log-bin= mysql-bin #binlog 日志位置
relay-log= slave-relay-bin #必须开启,从主数据库同步的 binlog 会写入到该目录下
Restart MYSQL service
service mysqld restart

3. Master-slave association configuration

#从库进入数据库
mysql -u root -p

#停掉从库的服务,一定要停掉服务再去执行下面的命令
stop slave;

#配置关联主库(master),
#MASTER_HOST : 要连接的主服务器的 ip 地址
#MASTER_USER : 要连接的主服务器的用户名
#MASTER_PASSWORD : 设置要连接的主服务器的密码
#以下两个填主库执行 show master status; 得到的信息
#MASTER_LOG_FILE :要连接的主服务器的 bin 日志的日志名称
#MASTER_LOG_POS :要连接的主服务器的 bin 日志的记录位置

change master to master_host='主节点IP', master_port=3306,master_user='root',master_password='数据库密码',master_log_file='mysql-bin.000001',master_log_pos=157;

#启动
start slave

#查看主从同步状态,没有报错启动成功
show slave status \G;
The following two options must be Yes

 Test: Connect to the main database, build database and tables to see if it is synchronized to the slave database.

3.  MySQL master-slave switching

1. Manual switching
#切断对主库的数据传输,进入主库备库设置只读

mysql -u root -p

set global read_only=ON;
set global super_read_only=ON;

#从库查看主从同步状态

show slave status \G

The two places in the picture are also Yes

#比对主备两边的 GTID 是否一致(要是一样的)

select @@global.gtid_executed;

These are all empty. After testing, I found no impact on master-slave library switching and data synchronization.

#从库停掉复制进程,并清空主从信息

stop slave;
reset slave all;

#从库关闭只读开启读写,转为新主库

set global read_only=off;
set global super_read_only=off;

#查看新主库信息, File 和 Position 信息要记下,后面要用,可复制出来先放 txt 文档中先

show master status;

#停掉原主库的服务
stop slave;

#配置关联主库(master),
#MASTER_HOST : 要连接的主服务器的 ip 地址
#MASTER_USER : 要连接的主服务器的用户名
#MASTER_PASSWORD : 设置要连接的主服务器的密码
#以下两个填主库执行 show master status; 得到的信息
#MASTER_LOG_FILE :要连接的主服务器的 bin 日志的日志名称
#MASTER_LOG_POS :要连接的主服务器的 bin 日志的记录位置

change master to master_host='主节点IP', master_port=3306,master_user='root',master_password='数据库密码',master_log_file='mysql-bin.000001',master_log_pos=1736;


#启动
start slave;

#查看主从同步状态,没有报错启动成功
show slave status \G;

The two places in the picture should be Yes

#旧主库关闭只读,开启读写,转为从库

set global read_only=off;
set global super_read_only=off;

Test: Connect to the new primary database and create tables to see if they are synchronized to the slave database.

Guess you like

Origin blog.csdn.net/weixin_68547003/article/details/131510934