1、主从redis,安装配置都是一样的。下面开始从服务器的配置。
参考的网址为:
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 synchronization2169:M 08 Sep 15:48:15.331 * Full resync requested by slave 192.168.1.17:63792169:M 08 Sep 15:48:15.331 * Starting BGSAVE for SYNC with target: disk2169:M 08 Sep 15:48:15.332 * Background saving started by pid 41674167:C 08 Sep 15:48:15.517 * DB saved on disk4167:C 08 Sep 15:48:15.517 * RDB: 6 MB of memory used by copy-on-write2169:M 08 Sep 15:48:15.577 * Background saving terminated with success2169:M 08 Sep 15:48:15.577 * Synchronization with slave 192.168.1.17:6379 succeeded
5、测试操作
5.1、Mater端
[root@localhost ~]# redis-cli127.0.0.1:6379> auth 123456OK127.0.0.1:6379> info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.17,port=6379,state=online,offset=505,lag=1master_repl_offset:505repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:504127.0.0.1:6379> set testMasterKey01 "testMasterValue01"OK127.0.0.1:6379> get testMasterKey01"testMasterValue01"127.0.0.1:6379>
5.2、Slave端(没有设置密码)
[root@CentOS64x64 redis]# redis-cli127.0.0.1:6379> pingPONG127.0.0.1:6379> info replication# Replicationrole:slavemaster_host:192.168.1.235master_port:6379master_link_status:upmaster_last_io_seconds_ago:7master_sync_in_progress:0slave_repl_offset:713slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0127.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能够写,但是 有一些说法和限制,具体参看 中的相关章节("Read-only slave"、"Allow writes only with N attached replicas"等)
6、
7、
8、