一、在主数据库中的配置文件中增加
window下是my.ini文件一般在安装目录下
linux在/etc/my.conf
server-id=1
read-only=0
二、登录主数据库创建用户,分配主从复制权限
create user 'abc'@'%' IDENTIFIED WITH mysql_native_password BY 'Root@123456';
grant replication slave on *.* TO 'abc'@'%';
三、 配置从库配置文件
server-id=2
read-only=1
四、登录从库执行
show master status;
得到文件名和运行位置
mysql8.0.23之前使用
change master to master_host='xxx.xxx.xxx.xxx',master_user='xxx',master_password='xxx',master_log_file='xxx',master_log_pos=xxx;
之后使用
change replication source to source_host='xxx.xxx.xxx.xxx',source_user='xxx',source_password='xxx',source_log_file='xxx',source_log_pos=xxx;
source_host:主库服务器ip
source_user:赋权主从的用户('abc')source_password:赋权主从的用户的密码('Root@123456')
source_log_file:binlog文件名(mysql-bin.000008)source_log_pos:运行位置(6564104)
五、启动主从复制
mysql8.0.22之前使用
start slave;
之后使用
start replica;
六、检查
8.0.22前
show slave status\G;
后
show replica status\G;
查看主数据库信息
Slave_IO_Running为读取binlog二进制日志的
Slave_SQL_Running为读取relaylog执行sql操作到本地的
俩个都需要是YES
测试:
在主库中运行
create DATABASE db01;
use db01;
create table tb_user(
id int(11) PRIMARY key not null,
name VARCHAR(64) not null,
sex char(1) not null
);
INSERT into tb_user VALUES(1,'小明','0');
从库同样会创建一个数据库一个表并插入一条数据为成功。