配置redis主从复制

1.首先安装redis

2.新建工作目录,并且拷贝三份配置文件
mkdir myredis

cd /etc

cp redis.conf /root/myredis/redis6379.conf
cp redis.conf /root/myredis/redis6380.conf
cp redis.conf /root/myredis/redis6381.conf
3.更改配置(以6379为例)
pidfile /var/run/reids_6379.pid
port 6379
dbfilename dump6379.rdb
4.启动服务
[root@tecent-xzx myredis]# redis-server redis6379.conf 
[root@tecent-xzx myredis]# redis-server redis6380.conf 
[root@tecent-xzx myredis]# redis-server redis6381.conf 
[root@tecent-xzx myredis]# ps -ef|grep redis
root      9668     1  0 Sep02 ?        00:22:21 redis-server 0.0.0.0:6379
root     14434     1  0 17:08 ?        00:00:00 redis-server 0.0.0.0:6380
root     14448     1  0 17:08 ?        00:00:00 redis-server 0.0.0.0:6381
root     14497  9446  0 17:08 pts/1    00:00:00 grep --color=auto redis
5.使用3个客户端连接
redis-cli -p 6379
redis-cli -p 6380
redis-cli -p 6381

#如果设置了密码,可以auth password验证一下
6.查看状态
info replication

请添加图片描述

7.在从服务器配置,使其变为6379主服务器的从属节点
REPLICAOF 127.0.0.1 6379

#旧版本的redis使用SLAVEOF命令
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
8.至此,主数据库可以读、写数据,而从数据库只能读数据
9.验证

先看看从数据库能不能写数据

127.0.0.1:6380> set a 1
(error) READONLY You can't write against a read only slave.

说明从数据库不能写数据

再看看主数据库

127.0.0.1:6379> set a 1
OK

从数据库查数据

127.0.0.1:6380> get a
"1"
10.从数据库shutdown再从主数据库写入数据

当从shutdown后,主写入数据,从重新启动后依然可以看到期间写入的数据

127.0.0.1:6380> shutdown
not connected> 

#此时,主写入数据 set b 2

从重新启动
[root@tecent-xzx myredis]# redis-server redis6380.conf 
[root@tecent-xzx myredis]# redis-cli -p 6380
127.0.0.1:6380> auth xzx527416
OK
127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> get b
"2"

tips:若主宕机,从依然可以读数据,但是此时在从数据库查看主数据库的状态,是down,当主重新恢复后,变为up

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值