MySQL 8.3.0 主从热备

|-----------------|----|-------|
| IP | 角色 | 版本 |
| 192.168.140.153 | 主 | 8.3.0 |
| 192.168.140.159 | 从 | 8.3.0 |

一、准备环境

1、卸载mariadb
bash 复制代码
rpm -qa | grep mariadb
rpm -e mariadb-libs --nodeps
2、安装依赖
bash 复制代码
yum -y install perl

二、安装MySQL

1、下载安装包
bash 复制代码
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.3.0-1.el7.x86_64.rpm-bundle.tar
2、解压
bash 复制代码
tar -xvf mysql-8.3.0-1.el7.x86_64.rpm-bundle.tar
3、安装
bash 复制代码
rpm -ivh mysql-community-common-8.3.0-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-plugins-8.3.0-1.el7.x86_64.rpm 
rpm -ivh mysql-community-libs-8.3.0-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-8.3.0-1.el7.x86_64.rpm 
rpm -ivh mysql-community-icu-data-files-8.3.0-1.el7.x86_64.rpm 
rpm -ivh mysql-community-server-8.3.0-1.el7.x86_64.rpm
4、启动服务
bash 复制代码
systemctl start mysqld
systemctl status mysqld

三、配置

1、修改密码并授权
bash 复制代码
# 查找初始密码
grep password /var/log/mysql.log

# 登录mysql
mysql -u root -p

# 重置密码(必须先重置,才允许执行其它操作)
mysql> alter user root@'localhost' identified with mysql_native_password by 'new_password';

# 授权
mysql> use mysql;
mysql> update user set host="%" where user="root";
mysql> select user,host from user where user='root';
+------+------+
| user | host |
+------+------+
| root | %    |
+------+------+
1 row in set (0.00 sec)
mysql> grant all on *.* to root@'%';
mysql> flush privileges;
2、修改配置文件
bash 复制代码
# 主服务器配置文件添加下面内容
server-id=1
log-bin=master_bin

# 从服务器配置文件添加下面内容
server-id=2

# 修改配置后重启服务
systemctl restart mysqld
3、创建同步账号(主服务器执行)
bash 复制代码
mysql -u root -p
mysql> use mysql;
# 创建同步使用的账号
mysql> create user master@'192.168.140.159' identified with mysql_native_password by 'password';
# 授予权限
mysql> grant replication slave on *.* to master@'192.168.140.159';
mysql> flush privileges;

# 查看master信息
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master_bin.000001 |     1599 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set, 1 warning (0.00 sec)
4、开启同步(从服务器执行)
bash 复制代码
# 关闭同步
mysql> stop slave;
# 修改同步信息
mysql> change master to master_host="192.168.140.153",master_user="master",master_password="password",master_log_file="master_bin.000001",master_log_pos=1599;
# 开启同步
mysql> start slave;
# 查看同步状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.140.153
                  Master_User: master
                  Master_Port: 3306
              Master_Log_File: master_bin.000001
                Relay_Log_Pos: 1371
        Relay_Master_Log_File: master_bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
                   Last_Errno: 0
             Master_Server_Id: 1
                  Master_UUID: 9ce1fe4b-1c9c-11ef-9e0a-000c2961bacc
             Master_Info_File: mysql.slave_master_info
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates

四、测试

1、新建数据库和表,并新增数据(主服务器执行)
bash 复制代码
# 创建库
mysql> create database test;
# 创建表
mysql> use test;
mysql> create table student ( id int auto_increment primary key, name varchar(50) not null, age int);
# 新增数据
mysql> insert into student (name,age) values ("final",15);
mysql> insert into student (name,age) values ("zhangsan",16);
mysql> insert into student (name,age) values ("lisi",19);
2、查询是否同步(从服务器查看)
bash 复制代码
mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | final    |   15 |
|  2 | zhangsan |   16 |
|  3 | lisi     |   19 |
+----+----------+------+
3 rows in set (0.00 sec)
相关推荐
wrx繁星点点6 分钟前
事务的四大特性(ACID)
java·开发语言·数据库
小小娥子36 分钟前
Redis的基础认识与在ubuntu上的安装教程
java·数据库·redis·缓存
DieSnowK37 分钟前
[Redis][集群][下]详细讲解
数据库·redis·分布式·缓存·集群·高可用·新手向
计算机学姐1 小时前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
-XWB-1 小时前
【MySQL】数据目录迁移
数据库·mysql
老华带你飞1 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
我明天再来学Web渗透2 小时前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
Data 3172 小时前
Hive数仓操作(十一)
大数据·数据库·数据仓库·hive·hadoop
吱吱鼠叔2 小时前
MATLAB数据文件读写:2.矩阵数据读取
数据库·matlab·矩阵
掘根2 小时前
【MySQL】Ubuntu环境下MySQL的安装与卸载
数据库·mysql·centos