MySQL主从同步-binlog

MySQL主从同步-binlog

服务器准备

IP 节点 配置 系统版本
191.168.117.143 master 2c2g40g centos 7.9
192.168.117.142 slave 2c2g40g centos 7.9

环境准备

下面操作需要在两台机器都操作

关闭防火墙

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

安装常用软件

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

更新软件

1
yum update

下载mysql安装源

1
https://dev.mysql.com/downloads/repo/yum/

image-20230919092155207

根据系统版本点击Download

点击下载rpm包 或者右键复制链接

image-20230919092456556

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

image-20230919093121744

配置安装源

1
2
cd /etc/yum.repos.d/
vim mysql-community.repo

image-20230919093455820

根据需求版本号配置

image-20230919093759357

我这里直接装8.0 所以不需要修改

安装mysql

1
yum -y install mysql-community-server

image-20230919094404877

Master节点操作

修改my.cnf配置

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

image-20230919111025313

启动数据库并配置开机自启

1
systemctl start mysqld && systemctl enable mysqld

获取MySQL初始化密码

1
grep password  /var/log/mysqld.log

image-20230919094601606

登录数据库

1
mysql -p'密码'   ##注意带特殊字符的必须要加单引号

image-20230919094745766

修改用户权限及slave复制用户

1
2
3
4
5
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;
1
2
参数解释 
with mysql_native_password 修改用户的身份验证插件,防止slave节点登录同步用户无法连接

image-20230919100317410

查看binlog开始日志

1
show master status \G

image-20230919111807361

Slave从节点操作

配置本地域名解析

1
vim /etc/hosts
1
192.168.117.143 mysql-master

image-20230919101555481

ping一下测试是否联通

1
ping mysql-master

image-20230919101713673

修改my.cnf配置

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

image-20230919110352842

启动mysql并配置开机自启

1
systemctl start mysqld && systemctl enable mysqld

修改mysql初始化密码及权限

1
grep password  /var/log/mysqld.log

image-20230919101925512

登录数据库

1
mysql -p'密码'   ##注意带特殊字符的必须要加单引号

配置密码及权限

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

image-20230919102313151

测试是否可以登录master节点的用户

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

image-20230919102551422

配置主从复制

登录root用户

1
mysql -p'Jianren@123'

编辑

1
edit
1
2
3
4
5
6
7
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_PORT=3306,
MASTER_USER='slave',
MASTER_PASSWORD='Jianren@123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=157;
1
2
3
4
5
6
7
参数解释
MASTER_HOST='mysql-master' master的主机IP 也可以本地域名
MASTER_PORT=3306 master端口
MASTER_USER='slave' master创建的同步用户
MASTER_PASSWORD='Jianren@123' master创建同步用户密码
MASTER_LOG_FILE='mysql-bin.000003' master查看的binlog文件名
MASTER_LOG_POS=157; masetr查看binlog的日志位置

保存

1
;

image-20230919112341626

image-20230919112246046

启动主从复制

1
start slave ;

image-20230919112557064

查看状态

1
show slave status \G 

两个线程均为 yes 代表成功

image-20230919114209901

测试状态

master节点 mysq中输入

1
create database test1;

slave节点 mysq中输入

1
show databases ;

可以看到已经同步过来了


MySQL主从同步-binlog
http://ziiix.cn/2024/11/08/MySQL主从同步-binlog/
作者
John Doe
发布于
2024年11月8日
许可协议