CentOS 1908 配置 Redis 5.0.7 三(哨兵模式)

CentOS 下载:http://mirrors.aliyun.com/centos/

Redis:http://download.redis.io/releases/

CentOS 1908 配置 Redis 5.0.7 一(单节点)

CentOS 1908 配置 Redis 5.0.7 二(主从复制)

CentOS 1908 配置 Redis 5.0.7 四(集群配置)

CentOS 1908 配置 Redis 5.0.7 五(集群新增 主从节点)​​​​​​​


  1. 在 Redis 文件夹中,复制一份 redis.conf:
    cp redis.conf redis6379.conf
    cp redis.conf redis6380.conf
    cp redis.conf redis6381.conf
  2. 配置 redis*.conf
    1. redis6379.conf:
      # 注释 bind
      # bind 127.0.0.1 
      protected-mode no
      port 6379
      daemonize yes
    2. redis6380.conf:
      # 注释 bind
      # bind 127.0.0.1 
      protected-mode no
      port 6380
      slaveof 127.0.0.1 6379
      daemonize yes
    3. redis6381.conf:
      # 注释 bind
      # bind 127.0.0.1 
      protected-mode no
      port 6381
      slaveof 127.0.0.1 6379
      daemonize yes
  3. 在 Redis 文件夹中,复制一份 redis.conf:
    cp sentinel.conf sentinel26379.conf
    cp sentinel.conf sentinel26380.conf
    cp sentinel.conf sentinel26381.conf
  4. 配置 sentinel26379.conf:
    1. sentinel.conf:
      port 26379
      sentinel monitor mymaster 127.0.0.1 6379 2
    2. sentinel26380.conf:
      port 26380
      sentinel monitor mymaster 127.0.0.1 6379 2
    3. sentinel26381.conf:
      port 26381
      sentinel monitor mymaster 127.0.0.1 6379 2
  5. 启动 Redis(按照主从顺序):
    src/redis-server redis6379.conf
    src/redis-server redis6380.conf
    src/redis-server redis6381.conf
  6. 启动哨兵(按照主从顺序):
    src/redis-sentinel sentinel26379.conf
    src/redis-sentinel sentinel26380.conf
    src/redis-sentinel sentinel26381.conf
  7. 查看Redis状态:
    1. 查看 redis 6379(主节点):
      从下面的 role:master 可以看出,redis 6379 为主节点:
      [root@192 redis-5.0.7]# src/redis-cli -h 127.0.0.1 -p 6379
      127.0.0.1:6379> info replication
      # Replication
      role:master
      connected_slaves:2
      slave0:ip=127.0.0.1,port=6380,state=online,offset=26140,lag=0
      slave1:ip=127.0.0.1,port=6381,state=online,offset=26140,lag=1
      master_replid:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
      master_replid2:0000000000000000000000000000000000000000
      master_repl_offset:26140
      second_repl_offset:-1
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:1
      repl_backlog_histlen:26140
      127.0.0.1:6379> 
      
    2. 查看 redis 6380(从节点):
      从下面的 role:slave 可以看出,redis 6380 为从节点:
      [root@192 redis-5.0.7]# src/redis-cli -h 127.0.0.1 -p 6380
      127.0.0.1:6380> info replication
      # Replication
      role:slave
      master_host:127.0.0.1
      master_port:6379
      master_link_status:up
      master_last_io_seconds_ago:1
      master_sync_in_progress:0
      slave_repl_offset:37795
      slave_priority:100
      slave_read_only:1
      connected_slaves:0
      master_replid:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
      master_replid2:0000000000000000000000000000000000000000
      master_repl_offset:37795
      second_repl_offset:-1
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:1
      repl_backlog_histlen:37795
      127.0.0.1:6380> 
      
    3. 查看 redis 6381(从节点):
      从下面的 role:slave 可以看出,redis 6381 为从节点:
      [root@192 redis-5.0.7]# src/redis-cli -h 127.0.0.1 -p 6381
      127.0.0.1:6381> info replication
      # Replication
      role:slave
      master_host:127.0.0.1
      master_port:6379
      master_link_status:up
      master_last_io_seconds_ago:1
      master_sync_in_progress:0
      slave_repl_offset:46230
      slave_priority:100
      slave_read_only:1
      connected_slaves:0
      master_replid:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
      master_replid2:0000000000000000000000000000000000000000
      master_repl_offset:46230
      second_repl_offset:-1
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:1
      repl_backlog_histlen:46230
      127.0.0.1:6381> 
      
  8. 故障转移:
    1. 将 redis 6379 强制停止:
      [root@192 redis-5.0.7]# ps -ef | grep redis
      root       2703      1  0 16:18 ?        00:00:00 src/redis-server *:6379
      root       2708      1  0 16:18 ?        00:00:00 src/redis-server *:6380
      root       2714      1  0 16:18 ?        00:00:00 src/redis-server *:6381
      root       2810      1  0 16:21 ?        00:00:00 src/redis-sentinel *:26379 [sentinel]
      root       2815      1  0 16:21 ?        00:00:00 src/redis-sentinel *:26380 [sentinel]
      root       2820      1  0 16:21 ?        00:00:00 src/redis-sentinel *:26381 [sentinel]
      root      51237  42180  0 16:23 pts/1    00:00:00 src/redis-cli -h 127.0.0.1 -p 6379
      root      51296  51258  0 16:24 pts/2    00:00:00 src/redis-cli -h 127.0.0.1 -p 6380
      root      51352  51316  0 16:25 pts/3    00:00:00 src/redis-cli -h 127.0.0.1 -p 6381
      root      51412   2368  0 16:29 pts/0    00:00:00 grep --color=auto redis
      [root@192 redis-5.0.7]# kill -9 2703
      [root@192 redis-5.0.7]# 
      
    2. 查看主从节点:
      1. redis 6379 已停止。
      2. 查看 redis 6380:
        从下面的 role:slave 可以看出,redis 6380 为从节点:
        127.0.0.1:6380> info replication
        # Replication
        role:slave
        master_host:127.0.0.1
        master_port:6381
        master_link_status:up
        master_last_io_seconds_ago:0
        master_sync_in_progress:0
        slave_repl_offset:112137
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_replid:764de6af1521b0204f26d230547986bc2d629efb
        master_replid2:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
        master_repl_offset:112137
        second_repl_offset:102882
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:1
        repl_backlog_histlen:112137
        127.0.0.1:6380> 
        
      3. 查看 redis 6381:
        从下面的 role:master 可以看出,redis 6381 为主节点(故障产生时,使用投票机制选举中的主节点):
        127.0.0.1:6381> info replication
        # Replication
        role:master
        connected_slaves:1
        slave0:ip=127.0.0.1,port=6380,state=online,offset=112536,lag=1
        master_replid:764de6af1521b0204f26d230547986bc2d629efb
        master_replid2:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
        master_repl_offset:112669
        second_repl_offset:102882
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:1
        repl_backlog_histlen:112669
        127.0.0.1:6381> 
        
  9. 故障修复:
    1. 重新启动 redis 6379:
      src/redis-server redis6379.conf
    2. 查看 redis 6379:
      从下面的 role:slave 可以看出,redis 6379 为从节点(主节点故障修复后,转为从节点):
      127.0.0.1:6379> info replication
      # Replication
      role:slave
      master_host:127.0.0.1
      master_port:6381
      master_link_status:up
      master_last_io_seconds_ago:0
      master_sync_in_progress:0
      slave_repl_offset:154482
      slave_priority:100
      slave_read_only:1
      connected_slaves:0
      master_replid:764de6af1521b0204f26d230547986bc2d629efb
      master_replid2:0000000000000000000000000000000000000000
      master_repl_offset:154482
      second_repl_offset:-1
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:146690
      repl_backlog_histlen:7793
      127.0.0.1:6379> 
      
    3. 查看 redis 6380:
      从下面的 role:slave 可以看出,redis 6380 为从节点:
      127.0.0.1:6380> info replication
      # Replication
      role:slave
      master_host:127.0.0.1
      master_port:6381
      master_link_status:up
      master_last_io_seconds_ago:0
      master_sync_in_progress:0
      slave_repl_offset:160376
      slave_priority:100
      slave_read_only:1
      connected_slaves:0
      master_replid:764de6af1521b0204f26d230547986bc2d629efb
      master_replid2:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
      master_repl_offset:160376
      second_repl_offset:102882
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:1
      repl_backlog_histlen:160376
      127.0.0.1:6380> 
      
    4. 查看 redis 6381:
      从下面的 role:master 可以看出,redis 6381 为主节点(故障产生时,使用投票机制选举中的主节点,除发生故障外,主节点将保持不变):
      127.0.0.1:6381> info replication
      # Replication
      role:master
      connected_slaves:2
      slave0:ip=127.0.0.1,port=6380,state=online,offset=166949,lag=1
      slave1:ip=127.0.0.1,port=6379,state=online,offset=167082,lag=0
      master_replid:764de6af1521b0204f26d230547986bc2d629efb
      master_replid2:4333b9916945c9fc15b003ce0ff61ec7455cc0e8
      master_repl_offset:167082
      second_repl_offset:102882
      repl_backlog_active:1
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:1
      repl_backlog_histlen:167082
      127.0.0.1:6381> 
      
  10. 只能在主节点增加修改数据。
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值