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

  • 相关阅读:
    2015 年最受 Linux 爱好者欢迎的软硬件大盘点
    Java 9终于要包含Jigsaw项目了
    Linux 容器技术史话:从 chroot 到未来
    开发者最常用的 8 款 Sublime Text 3 插件
    60,000毫秒内对Linux的性能诊断效的方法
    bzoj 2595 [Wc2008]游览计划(斯坦纳树)
    bzoj 3997 [TJOI2015]组合数学(DP)
    bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
    bzoj 1090 [SCOI2003]字符串折叠(区间DP)
    bzoj 1537 [POI2005]Aut- The Bus(DP+BIT)
  • 原文地址:https://www.cnblogs.com/qlshine/p/5962172.html
Copyright © 2011-2022 走看看