首先参看一下redis 3.2.8的安装

传送门:biubiubiu 飞去吧:http://www.cnblogs.com/bing-yu12/p/6582086.html

我的主从搭建:

  redis 主环境:

    centos 6.7 

    ip:192.168.184.3

    redis 3.2.8

    redis.conf的重要配置:

    bind 127.0.0.1选项后面加上192.168.184.3 让外部机器也可以访问该ip

    iptables规则清除

    selinux关闭:setenforce 0 或者vim /etc/sysconfig/selinux 中设置为disabled 

  redis 从

    配置变化部分:

    bind 127.0.0.1 即可

    从只有读的权限,所以:

    slave-read-only no 打开

    配置文件修改后,主和从都要重启redis服务,然后在主上修改数据:

    

[root@alex redis]# ./bin/redis-cli 
127.0.0.1:6379> set key alexander
OK

  从上的操作读:

    

[root@alex redis]# ./bin/redis-cli 
127.0.0.1:6379> get key
"alexander"

查看日志:

  

tail -f  /var/log/redis/redis.log
5833:S 13 Mar 11:55:03.161 * Full resync from master: c95b2c82899b47186301e42c23dc7f53db8f48ab:970
5833:S 13 Mar 11:55:03.245 * MASTER <-> SLAVE sync: receiving 106 bytes from master
5833:S 13 Mar 11:55:03.246 * MASTER <-> SLAVE sync: Flushing old data
5833:S 13 Mar 11:55:03.246 * MASTER <-> SLAVE sync: Loading DB in memory
5833:S 13 Mar 11:55:03.247 * MASTER <-> SLAVE sync: Finished with success
5833:S 13 Mar 11:57:59.063 * 1 changes in 900 seconds. Saving...

说明主从搭建成功了。

注意,在上面的步骤中少了一个从数据上拉取主上的信息的命令:

127.0.0.1:6379> slaveof 192.168.184.3 6379
OK
127.0.0.1:6379> get key
"alex"

当在从上写数据的时候,会打破redis的主从关系,这个时候,你也可以使用该命令回复主从关系,你也可以在从的配置文件中指定:

slaveof 192.168.184.3 6379

对于该配置文件的解释:文档中有这样的说明:

################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves. 
# 2) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them. 
#
slaveof 192.168.184.3 6379
# slaveof <masterip> <masterport>

看第二条:大概的意思就是,如果主从关系紊乱,当主上的数据再修改以后,那么从上的数据会自动和主上的数据进行同步。

如有疑问,欢迎讨论QQ:1443353268