zoukankan      html  css  js  c++  java
  • Redis-简单实现星形主从配置

    高级参考(https://www.zhihu.com/question/21419897)

    简单应用场景

    现在配置redis 星形 集群, 有三台服务器, 怎样实现?
    复制redis.conf两份, 分别命名为redis6380.conf, redis6381.conf
    master指向redis.conf, slave1指向6380.conf, slave2指向redis6381.conf

    master关闭rdb, 开启aof
    slave1开启rdb, 关闭aof
    slave2关闭rdb和aof

    配置redis6380.conf

    #pidfile /var/run/redis.pid    改为
    pidfile /var/run/redis6380.pid
    ...
    #port 6379    改为
    port 6380
    ...
    #dbfilename dump.rdb    改为
    dbfilename dump6380.rdb    #让slave1执行rdb工作
    ...
    # slaveof <masterip> <masterport>    改为
    slaveof localhost 6379    #表示作为6379的slave
    ...
    appendonly no    #aof也不用产生, 因此关闭
    ...
    slave-read-only yes    #只读
    

    配置redis6381.conf

    pidfile /var/run/redis6381.pid
    ...
    port 6381
    ...
    #save 900 1    #两台从服务器其中一台产生rdb就可以了, 另一台没必要再次产生rdb, 因此注释掉
    #save 300 10
    #save 60 10000
    ...
    appendonly no    #aof也不用产生, 因此关闭
    ...
    slaveof localhost 6379
    ...
    slave-read-only yes    #只读
    

    配置redis.conf

    #save 900 1    #因为slave1已经存在rdb了, 所以master不在需要rdb
    #save 300 10
    #save 60 10000
    ...
    appendonly yes    #master的aof可以打开, 因为主服务器的aof最全最快
    

    启动, 分别在终端打开:

    ql@ql:~$ redis-server /usr/local/etc/redis/redis.conf
    ql@ql:~$ redis-server /usr/local/etc/redis/redis6380.conf
    ql@ql:~$ redis-server /usr/local/etc/redis/redis6381.conf
    

    启动master的客户端

    ql@ql:~$ redis-cli
    127.0.0.1:6379> set title sunshine
    OK
    127.0.0.1:6379> 
    

    启动slave1的客户端

    ql@ql:~$ redis-cli -p 6380
    127.0.0.1:6380> keys *    #可以看到master中的内容
    1) "title"
    127.0.0.1:6380> get title
    "sunshine"
    127.0.0.1:6380> 
    

    启动slave2的客户端

    ql@ql:~$ redis-cli -p 6381
    127.0.0.1:6381> keys *    #也能看到master中的内容
    1) "title"
    127.0.0.1:6381> get title
    "sunshine"
    127.0.0.1:6381> 
    

    现在要为master设置密码, 即redis.conf

    # If the master is password protected (using the "requirepass" configuration
    # directive below) it is possible to tell the slave to authenticate before
    # starting the replication synchronization process, otherwise the master will
    # refuse the slave request.
    #
    # masterauth <master-password>
    requirepass admin123    #新加的一行
    

    再次打开终端
    打开reids-server
    打开reids-cli

    ql@ql:~$ redis-cli
    127.0.0.1:6379> keys *
    (error) NOAUTH Authentication required.    #没有输入密码进行认证
    127.0.0.1:6379> 
    127.0.0.1:6379> auth admin123    #auth+密码进行认证
    OK
    127.0.0.1:6379> keys *
    1) "title"
    127.0.0.1:6379> get title
    "sunshine"
    127.0.0.1:6379> 
    

    此时从服务器连不上主服务器, 因为有密码

    修改redis6380.conf

    # masterauth <master-password>    改为
    masterauth admin123
    

    redis6381.conf的修改如上


    redis主从复制的缺陷

    每次slave断开后(无论是主动断开还是网络故障), 再连接master,
    都要 master 全部 dump 出来 rdb 再 aof,
    即同步的过程都要执行一遍
    所以: 多台slave不要一下同时启动起来, 否则master可能IO剧增, 拖垮master

  • 相关阅读:
    ueditor 后端配置项没有正常加载,上传插件不能正常使用 UTF8 PHP
    dedecms 后台栏目全部展开 包括三级栏目
    修改DedeCMS图片上传路径命名规则的具体方法步骤
    dedecms织梦副栏目名称和链接调用
    当位于顶级栏目显示下级栏目,当位于二级栏目显示同级栏目,当位于三级目录,显示上级栏目
    织梦多个栏目arclist调用副栏目不显示的解决办法
    PL/SQL连接64位Oracle配置方法
    U盘分区之后如何恢复
    Myeclipse 的使用随笔
    eclipse和myeclipse的差别问题
  • 原文地址:https://www.cnblogs.com/qlshine/p/5962172.html
Copyright © 2011-2022 走看看