penven

博客园 首页 新随笔 联系 订阅 管理

本机Redis搭建主从哨兵

搭建主从

  • 先下载Redis服务

    1. 由于我是Mac所以安装命令brew install redis
    Updating Homebrew...
    ==> Auto-updated Homebrew!
    Updated 1 tap (homebrew/services).
    No changes to formulae.
    
    Warning: Treating redis as a formula. For the cask, use homebrew/cask/redis
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/redis-6.2.1.catalina.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring redis-6.2.1.catalina.bottle.tar.gz
    ==> Caveats
    To have launchd start redis now and restart at login:
      brew services start redis
    Or, if you don't want/need a background service you can just run:
      redis-server /usr/local/etc/redis.conf
    ==> Summary
    🍺  /usr/local/Cellar/redis/6.2.1: 13 files, 2.0MB
    
  • 复制出来两个从Redis.conf配置文件

    1. cd /usr/local/etc
    2. mkdir redis
    3. cp redis.conf redis/redis_6380.conf
    4. cp redis.conf redis/redis_6381.conf
    5. 修改 redis_6380.conf,和redis_6381.conf文件的端口号分别为6380和6381
    
  • 修改redis_6380.conf和redis_6381.conf 两个配置文件的replicaof <masterip> <masterport>配置

    replicaof 127.0.0.1 6379
    
  • 新开三个窗口,并进入可执行目录下面cd /usr/local/Cellar/redis/6.2.1/bin

    1. redis-server /usr/local/etc/redis.conf
    2. redis-server /usr/local/etc/redis/redis_6380.conf
    3. redis-server /usr/local/etc/redis/redis_6381.conf
    


    当看到上面的输出信息时,则说明已成功

  • 在Redis master服务上执行命令如下,


    1. 当看到上面信息的时候,则说明主从搭建成功并已成功通信
  • 可以执行如下命令查看主从信息info Replication

搭建哨兵

  • 复制 哨兵 redis-sentinel.conf 配置文件

    1. cd /usr/local/etc
    2. cp redis-sentinel.conf redis/redis-sentinel-26380.conf
    3. cp redis-sentinel.conf redis/redis-sentinel-26381.conf
    4. 修改 redis-sentinel-26380.conf,和redis-sentinel-26381.conf文件的端口号分别为26380和26381
    
  • 分别修改相对应的哨兵配置文件

    1. redis-sentinel.conf

      protected-mode no 
      port 26379
      sentinel monitor mymaster 127.0.0.1 6379 2 
      pidfile /var/run/redis-sentinel-26379.pid
      sentinel down-after-milliseconds mymaster 10000 
      sentinel parallel-syncs mymaster 1
      sentinel failover-timeout mymaster 60000
      
    2. redis-sentinel-26380.conf

      protected-mode no 
      port 26380
      sentinel monitor mymaster 127.0.0.1 6379 2 
      pidfile /var/run/redis-sentinel-26380.pid
      sentinel down-after-milliseconds mymaster 10000 
      sentinel parallel-syncs mymaster 1
      sentinel failover-timeout mymaster 60000
      
    3. redis-sentinel-26381.conf

      protected-mode no 
      port 26381
      sentinel monitor mymaster 127.0.0.1 6379 2 
      pidfile /var/run/redis-sentinel-26381.pid
      sentinel down-after-milliseconds mymaster 10000 
      sentinel parallel-syncs mymaster 1
      sentinel failover-timeout mymaster 60000
      
  • 启动相对应的哨兵

    redis-sentinel /usr/local/etc/redis-sentinel.conf
    redis-sentinel /usr/local/etc/redis/redis-sentinel-26380.conf
    redis-sentinel /usr/local/etc/redis/redis-sentinel-26381.conf
    

    这时候查看主从信息还是原来的

    这时候查看哨兵的信息

  • 模拟Redis master主服务挂掉后的

    1. 这时候查看Redis主从信息已经发生了变化

    2. 此时把6379端口的Redis服务起来后,再端口号为6380的客户端再次查看信息

    3. 此时再去6379的客户端再次查看信息时,发现该端口下的Redis服务已经降级为从服务

这个时候 Redis主从哨兵已经搭建完成!

posted on 2021-04-01 16:27  penven  阅读(169)  评论(0编辑  收藏  举报