首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Redis搭建主从

Redis搭建主从

作者头像
HaC
发布于 2020-12-30 17:45:42
发布于 2020-12-30 17:45:42
8440
举报
文章被收录于专栏:HaC的技术专栏HaC的技术专栏

Redis高可用,一般都是一主二从三哨兵。 假如当主master挂掉了,哨兵就会选举一个leader出来,这样就变成了一主一从三哨兵了。

作为从库,需要设置slaveof

代码语言:javascript
复制
#slaveof表示作为从库的配置
slaveof 192.168.0.174 6379

搭建开始:

  1. 先配置主的配置,新建一个redis_6379.cofig的文件
代码语言:javascript
复制
#允许所以ip连接
bind 0.0.0.0
#后台运行
daemonize yes
#pid文件
pidfile /var/run/redis_6379.pid
#数据库
dbfilename dump-6379.rdb
#日志文件
logfile "6379.log"
  1. 搭建从的配置,新建一个redis_6380.cofig的文件
代码语言:javascript
复制
bind 0.0.0.0
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
dbfilename dump-6380.rdb
logfile "6380.log"
#slaveof表示作为从库的配置
slaveof 192.168.0.174 6379
#从库只能读操作
slave-read-only yes
  1. 开启主从
代码语言:javascript
复制
[root@localhost redis-5.0.5]# ./src/redis-server redis_6379.conf 
[root@localhost redis-5.0.5]# ./src/redis-server redis_6380.conf
[root@localhost redis-5.0.5]# ./src/redis-cli -p 6379
127.0.0.1:6379> info

输入info命令,如下图,表示主从搭建成功了

connected_slaves表示有一个从节点

代码语言:javascript
复制
connected_slaves:1
slave0:ip=192.168.0.174,port=6380,state=online,offset=112,lag=1

再进入从看看:

测试一下数据,在主设置一个值,然后切换到从,获取key,也在可以获取到的。 从库设置了只能读,是不能set值的。

然后再设置多一个从

代码语言:javascript
复制
[root@localhost redis-5.0.5]# cp -v redis_6380.conf redis_6381.conf
"redis_6380.conf" -> "redis_6381.conf"

设置redis_6381.conf

代码语言:javascript
复制
bind 0.0.0.0
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
dbfilename dump-6381.rdb
logfile "6381.log"
#slaveof表示作为从库的配置
slaveof 192.168.0.174 6379
#从库只能读操作
slave-read-only yes
代码语言:javascript
复制
#sentinel启动命令
redis-sentinel sentinel.conf

设置哨兵: 新建哨兵配置文件sentinel_26379.conf

代码语言:javascript
复制
port 26379
daemonize yes
# 目录文件,如果没有启动不了
dir /software/redis-5.0.5/data
logfile "26379.log"
# 最后表示2个哨兵选择通过则选定为leader
sentinel monitor mymaster 192.168.0.174  6379 2
#宕机30秒还没有启动则哨兵选举
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

再建两个sentinel_26380.conf、sentinel_26381.conf:

代码语言:javascript
复制
port 26380
daemonize yes
dir /software/redis-5.0.5/data
logfile "26380.log"
# 最后表示2个哨兵选择通过则选定为leader
sentinel monitor mymaster 192.168.0.174  6379 2
#宕机30秒还没有启动则哨兵选举
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

开启哨兵:

代码语言:javascript
复制
[root@localhost redis-5.0.5]# ./src/redis-sentinel sentinel_26379.conf 

成功:

代码语言:javascript
复制
[root@localhost redis-5.0.5]# ps -ef | grep redis
root      28575      1  0 18:28 ?        00:00:04 ./src/redis-server 0.0.0.0:6379
root      28580      1  0 18:29 ?        00:00:03 ./src/redis-server 0.0.0.0:6380
root      28598      1  0 18:50 ?        00:00:01 ./src/redis-server 0.0.0.0:6381
root      28625      1  0 19:09 ?        00:00:00 ./src/redis-sentinel *:26379 [sentinel]
root      28630  23897  0 19:10 pts/1    00:00:00 grep --color=auto redis

进入:

代码语言:javascript
复制
[root@localhost redis-5.0.5]# ./src/redis-cli -p 26379
127.0.0.1:26379> info

再启动另外两个哨兵:

遇到的几个棘手问题:

  1. 搭建了三个哨兵,在主挂了之后并没有推举新的主,看了日志一直在选举挂掉的那个库;后面重新把三个主从,三个哨兵重新启动了,再测试又可以了。同时还加上了以下配置:
代码语言:javascript
复制
protected-mode no
  1. 哨兵选举会把主从的conf文件修改。

遇到写失败,后来把三个哨兵重新启动了又可以了,不知道是不是选举的时候把原来的主变成了从。

代码语言:javascript
复制
(error) READONLY You can't write against a read only replica.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档