主服务器配置(192.168.144.140)—china
先关闭防火墙
systemctl status firewalld.service # 查看状态
systemctl stop firewalld # 停止
systemctl disable firewalld # 移除
1.设置server-id值并开启binlog参数
vim /etc/my.cnf
# 添加如下内容
[mysqld]
# 主从双备配置
log-bin=mysql-bin #开启二进制日志
server-id=1 #设置server-id
## 重启mysqld服务,使配置生效
systemctl restart mysqld
2.建立同步账号
# 创建用来传输数据的账号
CREATE USER 'rep'@'192.168.144.%' IDENTIFIED BY 'rep123456';
# ps:如果提示密码简单
# 设置安全策略为低
set global validate_password_policy=0;
# 给这个账号赋予复制权限
grant replication slave on *.* TO 'rep'@'192.168.144.%';
# 也可以不限制ip地址范围和将所有权限都给(注意,生产环境下慎用!!)
grant replication slave on *.* to 'rep'@'%' identified by 'rep123456';
# 刷新权限表
flush privileges;
# 查看权限情况
show grants for 'rep';
# 查看主库的状态
show master status;
从服务器配置(192.168.144.142)—mini3
关闭防火墙
vim /etc/my.cnf
# 主从双备配置
server-id=2 #设置server-id,必须唯一
进入从库中,执行命令(切换到从服务器中的数据库中)
change master to
MASTER_HOST='192.168.144.140',
MASTER_USER='rep',
MASTER_PASSWORD='rep123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1365;
# 启动从库同步开关
start slave;
stop slave # 停止
# 检查状态
show slave status\G;
Slave_IO_Running: YES
Slave_SQL_Running: YES