美文网首页
Redis-2 Replication(主从复制)

Redis-2 Replication(主从复制)

作者: rommy020 | 来源:发表于2018-09-05 16:09 被阅读0次

    官网: https://redis.io/topics/replication
    参考: https://redis.io/topics/config

    什么是主从复制

    Redis的主从复制, 将Master数据自动同步到多个Slave. 一个Master允许多个Slave, 而且自动同步数据, Master负责写操作, Slave负责读操作(GET, LRANGE, SMEMMBERS, HGET, ZRANGE), . 每次链接断开时, 从站将自动重新连接到主站, 并且无论主站发生什么情况, 它都将尝试成为它的精确副本.
    好处: 降低主服务器的读压力, 防止数据丢失

    下面介绍2种方式(a:简易演示方式 b:配置文件方式)

    A: 简易演示方式

    该方式不能设置密码,不能bind..., 否则同步会报错, 只能做演示用途, 不能作为生产解决方案.
    1: 启动Master(默认端口6379)

    redis-server
    

    2: 启动3个Slave(端口6380, 6381, 6382)做为6379的从, 默认是非守护进程(如果想设置成守进程, 则设置 --daemonize yes)

    redis-server --port 6380 --slaveof 127.0.0.1 6379
    redis-server --port 6381 --slaveof 127.0.0.1 6379 
    redis-server --port 6382 --slaveof 127.0.0.1 6379
    

    除了上面的方法可以启动slave外, 还有一个方法(只不过步骤多一些)

    redis-server --port 6381
    redis-cli -p 6381
    slaveof localhost 6379
    

    启动后就可以看到日志, 从Master中同步数据到Slave中

    5885:S 05 Sep 07:19:58.072 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    5885:S 05 Sep 07:19:58.072 # Server initialized
    5885:S 05 Sep 07:19:58.072 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    5885:S 05 Sep 07:19:58.072 * Ready to accept connections
    5885:S 05 Sep 07:19:58.073 * Connecting to MASTER 10.0.0.99:6379
    5885:S 05 Sep 07:19:58.074 * MASTER <-> SLAVE sync started
    5885:S 05 Sep 07:19:58.074 * Non blocking connect for SYNC fired the event.
    5885:S 05 Sep 07:19:58.074 * Master replied to PING, replication can continue...
    5885:S 05 Sep 07:19:58.074 * Partial resynchronization not possible (no cached master)
    5885:S 05 Sep 07:19:58.075 * Full resync from master: 1042fc5f2b9d3c4bb3d2647f0ef0fb4f018ef441:0
    5885:S 05 Sep 07:19:58.114 * MASTER <-> SLAVE sync: receiving 187 bytes from master
    5885:S 05 Sep 07:19:58.114 * MASTER <-> SLAVE sync: Flushing old data
    5885:S 05 Sep 07:19:58.114 * MASTER <-> SLAVE sync: Loading DB in memory
    5885:S 05 Sep 07:19:58.114 * MASTER <-> SLAVE sync: Finished with success
    

    3: 往Master中写入数据

    redis-cli -p 6379 -h 10.0.0.99
    > set name redis
    OK
    > get name
    "redis"
    

    4: 往Slave中读/写入数据(能读不能写)

    redis-cli -p 6380
    > set name redis
    (error) READONLY You can't write against a read only slave.
    > get name
    "redis"
    

    5: 停掉Master
    kill xxx
    马上Slave(6380就报错)

    * Connecting to MASTER 127.0.0.1:6379
    * MASTER <-> SLAVE sync started
    # Error condition on socket for SYNC: Connection refused
    

    6: 再次启动Master

    redis-server
    

    7: 观察Slave(6380), 自动重新连接

    * MASTER <-> SLAVE sync started
    * Non blocking connect for SYNC fired the event.
    * Master replied to PING, replication can continue...
    * Trying a partial resynchronization (request 1adf769b9882a70820a896e5ee0848489368a8f6:450).
    * Full resync from master: e7f922f348748315c038847773ce5fca788e3f72:0
    * Discarding previously cached master state.
    * MASTER <-> SLAVE sync: receiving 199 bytes from master
    * MASTER <-> SLAVE sync: Flushing old data
    * MASTER <-> SLAVE sync: Loading DB in memory
    * MASTER <-> SLAVE sync: Finished with success
    

    B: 配置文件方式

    1: 新建配置文件:redis6380.conf

    port 6380
    
    protected-mode no
    daemonize yes
    
    logfile "/var/log/redis/redis-server6380.log"
    
    dir "/var/lib/redis"
    dbfilename "dump6380.rdb"
    
    slaveof 10.0.0.99 6379
    masterauth "111111"
    appendfilename "appendonly6380.aof"
    
    save 900 1
    save 300 10
    save 60 10000
    databases 16
    

    2: 启动

    redis-server redis6380.conf
    

    3: 往Master中写入数据

    redis-cli -p 6379 -h 10.0.0.99
    > set name redis
    OK
    > get name
    "redis"
    

    4: 往Slave中读/写入数据(能读不能写)

    redis-cli -p 6380
    > set name redis
    (error) READONLY You can't write against a read only slave.
    > get name
    "redis"
    

    5.6.7 同上.

    相关文章

      网友评论

          本文标题:Redis-2 Replication(主从复制)

          本文链接:https://www.haomeiwen.com/subject/wvwawftx.html