Redis 主从复制-服务器搭建【薪火相传/哨兵模式】
  TEZNKK3IfmPf 2023年11月14日 14 0

Redis 安装参考文章:Centos7 安装并启动 Redis-6.2.6

注意:本篇文章操作,不能在 静态IP地址 下操作,必须是 动态IP地址,否则最后主从服务器配置不成功!

管道符查看所有redis进程:ps -ef|grep redis

杀死所有redis进程:killall redis-server

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】 

1. 首先修改 redis.conf 配置

[root@localhost ~]# cd /usr/local/redis-6.2.6
[root@localhost redis-6.2.6]# vim redis.conf

① 开启 daemonize yes

     此步骤是优化后端启动(启动不会弹出redis图标和大量繁琐信息)

进去编辑命令输入:/daemonize 回车直接跳转它的位置上

     将原先的注释掉,重新编辑开启 daemonize yes

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

​​​​​​​appendonly关掉:appendonly no

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

2. 在 cd /usr/local/redis-6.2.6/ 目录下拷贝三个 redis.conf

[root@localhost ~]# cd /usr/local/redis-6.2.6

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

① vim redis6379.conf 的编辑内容如下:

[root@localhost redis-6.2.6]# vim redis6379.conf
include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb

② 当前目录下复制 redis6379.conf 为 redis6380.conf

     然后修改 redis6380.conf

 Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

③ 批量替换  :%s/6379/6380

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb

④ 当前目录下复制 redis6379.conf 为 redis6381.conf

     然后修改 redis6381.conf

     批量替换  :%s/6379/6381

[root@localhost redis-6.2.6]# cp redis6379.conf redis6381.conf
[root@localhost redis-6.2.6]# vim redis6381.conf 
include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb

 3. 以拷贝的三个文件作为参照启动服务

[root@localhost src]# ./redis-server ../redis6379.conf 
[root@localhost src]# ./redis-server ../redis6380.conf 
[root@localhost src]# ./redis-server ../redis6381.conf 

 两种启动方式都可以执行成功!

[root@localhost redis-6.2.6]# redis-server redis6379.conf 
[root@localhost redis-6.2.6]# redis-server redis6380.conf 
[root@localhost redis-6.2.6]# redis-server redis6381.conf 

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

4. 分别指定端口号登录

[root@localhost redis-6.2.6]# cd src/
[root@localhost src]# ./redis-cli -p 6379
127.0.0.1:6379> info replication
[root@localhost ~]# cd /usr/local/redis-6.2.6/src
[root@localhost src]# ./redis-cli -p 6380
127.0.0.1:6380> info replication
[root@localhost ~]# cd /usr/local/redis-6.2.6/src
[root@localhost src]# ./redis-cli -p 6381
127.0.0.1:6381> info replication

① 输入命令 info replication 能看到三个都是主节点 master

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

5. 将 6380 和 6381 端口 指定为 6379 端口的仆从

127.0.0.1:6380> slaveof 192.168.230.130 6379
OK
127.0.0.1:6381> slaveof 192.168.230.130 6379
OK

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

 Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

6. 测试主从读写分离

① 主节点 6379 写入 h1:1

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

② 从节点 6380 和 6381 能获取到 h1 的值

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

从节点 6380  写入 h2:2 则报错,因为从节点只能读,不能写

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

7. 薪火相传 介绍

那我们可以 让 节点6381从主 节点6380 ,节点6380从主节点6379

也就是,6379是主,6380是从

              6380是主,6381是从

这就是薪火相传

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

8. 配置 哨兵模式

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

① 在 cd /usr/local/redis-6.2.6/ 目录下 配置 sentinel.conf 文件(没有新建)

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

vim sentinel.conf

② 在配置文件 sentinel.conf 中 84行 修改内容

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

sentinel monitor mymaster 192.168.230.128 6379 1

其中 mymaster 为监控对象起的服务器名称,1 为 至少有多少个哨兵 同意迁移的数量

③ 启动 哨兵

[root@localhost redis-6.2.6]# redis-sentinel sentinel.conf 

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

 ④ 测试:让大哥-master主节点 6379 挂掉

127.0.0.1:6379> shutdown

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

哨兵投票选择了节点 6381 作为大哥

Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

⑤ 重启 redis6379 服务器,登录端口

发现原来的大哥 6379节点 在宕机挂掉后,经过哨兵投票选择的 节点6381 作为大哥后,节点6379 作为 节点6381 的小弟了

 Redis 主从复制-服务器搭建【薪火相传/哨兵模式】

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   27   0   0 redis用户
  TEZNKK3IfmPf   2024年05月31日   29   0   0 dataredis
  TEZNKK3IfmPf   2024年05月31日   27   0   0 awkredis
  TEZNKK3IfmPf   2024年04月19日   39   0   0 javarediskey
TEZNKK3IfmPf