两台服务器: master (192.168.0.196) 和 slave (192.168.31.102)
分别按照之前介绍的步骤安装好redis并启动
master 配置文件不用动
slave 配置文件上加一行
slaveof 192.168.31.105 6379
masterauth passwd //如果主上设置了密码,要加这行
分别启动master和slave
从redis的配置文件
[root@slave ~]# vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /usr/local/redis/var/redis.pidport 6379timeout 300loglevel debuglogfile /usr/local/redis/var/redis.logdatabases 16save 900 1save 300 10save 60 10000rdbcompression yesdbfilename dump.rdbdir /usr/local/redis/var/appendonly noappendfsync alwaysslaveof 192.168.0.196 6379masterauth caimz查看从redis的log日志
[root@slave ~]# tail /usr/local/redis/var/redis.log
[4760] 28 Feb 18:41:13.358 * Full resync from master: be15a09aa9c862e88b19b2ca8177dbe24db12bac:1[4760] 28 Feb 18:41:13.438 * MASTER <-> SLAVE sync: receiving 118 bytes from master[4760] 28 Feb 18:41:13.438 * MASTER <-> SLAVE sync: Flushing old data[4760] 28 Feb 18:41:13.438 * MASTER <-> SLAVE sync: Loading DB in memory[4760] 28 Feb 18:41:13.439 * MASTER <-> SLAVE sync: Finished with success[4760] 28 Feb 18:41:16.217 - Accepted 127.0.0.1:56003[4760] 28 Feb 18:41:18.403 - DB 0: 8 keys (0 volatile) in 8 slots HT.[4760] 28 Feb 18:41:18.403 - 2 clients connected (0 slaves), 487504 bytes in use[4760] 28 Feb 18:41:23.458 - DB 0: 8 keys (0 volatile) in 8 slots HT.[4760] 28 Feb 18:41:23.458 - 2 clients connected (0 slaves), 487512 bytes in use[root@slave ~]#红色部分就可以判断是主从同步了。
测试主从同步:
主:新建一个参数。
[root@master ~]# /usr/local/redis/bin/redis-cli -a caimz
127.0.0.1:6379> set nanjing KOOK127.0.0.1:6379> get nanjing"KO"从:登陆查看
[root@slave ~]# /usr/local/redis/bin/redis-cli -a caimz
127.0.0.1:6379> get nanjing
"KO"#此时和主的数值一致。
查看库里所有的key对应的value
主:
127.0.0.1:6379> keys *
1) "nanjing"2) "key1"3) "leco"4) "key3"5) "caiting"6) "zmj"7) "key2"8) "key"9) "k1"从:
127.0.0.1:6379> keys *
1) "key2"2) "k1"3) "key"4) "leco"5) "caiting"6) "nanjing"7) "key1"8) "zmj"9) "key3"主从对应一样.同步。
主:
127.0.0.1:6379> sadd set1 111
(integer) 1127.0.0.1:6379> sadd set1 22(integer) 1127.0.0.1:6379> sadd set1 333(integer) 1从:
127.0.0.1:6379> keys *
1) "set1"2) "key2"3) "k1"4) "key"5) "leco"6) "caiting"7) "nanjing"8) "key1"9) "zmj"10) "key3"127.0.0.1:6379> smembers set11) "22"2) "111"3) "333"#就是主上设置的参数
slave-read-only yes //让从只读
repl-ping-slave-period 10 //设置slave向master发起ping的频率,每10s发起一次
repl-timeout 60 //设置slave ping不通master多少s后就超时
repl-disable-tcp-nodelay no //是否开启tcp_nodelay,开启后将会使用更少的带宽,但会有延迟,所以建议关闭
repl-backlog-size 1mb //同步队列的长度,backuplog是master的一个缓冲区,主从断开后,master会先把数据写到缓冲区,slave再次连接会从缓冲区中同步数据
repl-backlog-ttl 3600 //主从断开后,缓冲区的有效期,默认1小时
slave-priority 100 //多个slave是可以设置优先级的,数值越小优先级越高,应用于集群中,支持slave切换为master,优先级最高的才会切换
min-slaves-to-write 3 //和下面的一起使用,它的意思是master发现有超过3个slave的延迟高于10s,那么master就会暂时停止写操作。这两个数值任何一个为0,则关闭该功能,默认第一数值是0。
min-slaves-max-lag 10