IP | 节点 | 配置 | 系统版本 |
---|---|---|---|
191.168.117.143 | master | 2c2g40g | centos 7.9 |
192.168.117.142 | slave | 2c2g40g | centos 7.9 |
关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
安装常用软件
yum install -y wget vim net-tools bash-completion
更新软件
yum update
https://dev.mysql.com/downloads/repo/yum/
根据系统版本点击Download
点击下载rpm包 或者右键复制链接
wget https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm
rpm -ivh mysql80-community-release-el7-10.noarch.rpm
cd /etc/yum.repos.d/
vim mysql-community.repo
根据需求版本号配置
我这里直接装8.0 所以不需要修改
yum -y install mysql-community-server
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
systemctl start mysqld && systemctl enable mysqld
grep password /var/log/mysqld.log
登录数据库
mysql -p'密码' ##注意带特殊字符的必须要加单引号
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;
参数解释
with mysql_native_password 修改用户的身份验证插件,防止slave节点登录同步用户无法连接
show master status \G
vim /etc/hosts
192.168.117.143 mysql-master
ping一下测试是否联通
ping mysql-master
修改my.cnf配置
vim /etc/my.cnf
server-id=2
systemctl start mysqld && systemctl enable mysqld
grep password /var/log/mysqld.log
登录数据库
mysql -p'密码' ##注意带特殊字符的必须要加单引号
配置密码及权限
alter user 'root'@'localhost' identified by 'Jianren@123';
grant all privileges on *.* to 'root'@'localhost' ;
flush privileges;
exit ;
测试是否可以登录master节点的用户
mysql -uslave -pJianren@123 -h mysql-master
show databases ;
exit;
登录root用户
mysql -p'Jianren@123'
编辑
\e
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='slave',
MASTER_PASSWORD='Jianren@123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=157;
参数解释
MASTER_HOST='mysql-master' master的主机IP 也可以本地域名
MASTER_USER='slave' master创建的同步用户
MASTER_USER='slave' master创建同步用户密码
MASTER_LOG_FILE='mysql-bin.000003' master查看的binlog文件名
MASTER_LOG_POS=157; masetr查看binlog的日志位置
保存
;
启动主从复制
start slave ;
查看状态
show slave stauts \G
两个线程均为 yes 代表成功
master节点 mysq中输入
create database test1;
slave节点 mysq中输入
show databases ;