20160908_Redis主从复制Replication

时间:2022-02-24 04:48:40

1、主从redis,安装配置都是一样的。下面开始从服务器的配置。

参考的网址为:http://yanliu.org/2015/08/27/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E7%9A%84%E5%AE%9E%E7%8E%B0/

2、配置文件"/etc/redis/6379.conf"

  2.1、“# slaveof <masterip> <masterport>” 的下面写上

    “# slaveof 192.168.1.235 6379”

  2.2、“# masterauth <master-password>” 的下面写上

    “# masterauth 123456”

3、客户端是运行状态的话,配置修改后 需要 执行操作 "/etc/init.d/redis_6379 restart"

  ZC: 貌似这一步不是必须的...

4、主服务器的“/var/log/redis_6379.log”文件中,会出现 类似如下的信息:

2169:M 08 Sep 15:48:15.331 * Slave 192.168.1.17:6379 asks for synchronization
2169:M 08 Sep 15:48:15.331 * Full resync requested by slave 192.168.1.17:6379
2169:M 08 Sep 15:48:15.331 * Starting BGSAVE for SYNC with target: disk
2169:M 08 Sep 15:48:15.332 * Background saving started by pid 4167
4167:C 08 Sep 15:48:15.517 * DB saved on disk
4167:C 08 Sep 15:48:15.517 * RDB: 6 MB of memory used by copy-on-write
2169:M 08 Sep 15:48:15.577 * Background saving terminated with success
2169:M 08 Sep 15:48:15.577 * Synchronization with slave 192.168.1.17:6379 succeeded

5、测试操作

  5.1、Mater端

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.17,port=6379,state=online,offset=505,lag=1
master_repl_offset:505
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:504
127.0.0.1:6379> set testMasterKey01 "testMasterValue01"
OK
127.0.0.1:6379> get testMasterKey01
"testMasterValue01"
127.0.0.1:6379>

  5.2、Slave端(没有设置密码)

[root@CentOS64x64 redis]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.1.235
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:713
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> get testMasterKey01
"testMasterValue01"
127.0.0.1:6379> get testMasterKey02
(nil)
127.0.0.1:6379>

    5.2.1、此时,在Slave端 设置key-value,就报错了,说是只读的,不让往里面写

127.0.0.1:6379> set testSlaveKey01 "testSlaveValue01"
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379>

      ZC: 貌似,也可以配置 使得Slave能够写,但是 有一些说法和限制,具体参看 http://redis.io/topics/replication 中的相关章节("Read-only slave"、"Allow writes only with N attached replicas"等)

6、

7、

8、