Mysql主从复制

 一、在主数据库中的配置文件中增加

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');

 从库同样会创建一个数据库一个表并插入一条数据为成功。

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值