一:说明
1、准备两台服务器
master:192.168.0.128
slave:192.168.0.129
2、关闭防火墙
iptables -F
# getenforce
Disabled
二、安装配置
1、分别都安装redis
参考我之前写的文章:Redis数据库的安装与配置
http://msiyuetian.blog.51cto.com/8637744/1717983
2、master配置文件不用动,slave配置文件上加一行
slaveof 192.168.0.128 6379 //主IP masterauth 123456 //如果主上设置了密码,若密码为123456,则要加这行 |
3、分别启动master和slave上的redis
三、验证主从
在从上查看日志
[root@slave ~]# tail /usr/local/redis/var/redis.log
[6117] 03 Dec 11:52:12.034 * Full resync from master: 37031d932827c403dfd2be6df445901ae3b67a65:1 [6117] 03 Dec 11:52:12.385 * MASTER <-> SLAVE sync: receiving 18 bytes from master [6117] 03 Dec 11:52:12.385 * MASTER <-> SLAVE sync: Flushing old data [6117] 03 Dec 11:52:12.385 * MASTER <-> SLAVE sync: Loading DB in memory [6117] 03 Dec 11:52:12.385 * MASTER <-> SLAVE sync: Finished with success [6117] 03 Dec 11:52:17.108 - 1 clients connected (0 slaves), 276592 bytes in use [6117] 03 Dec 11:52:22.169 - 1 clients connected (0 slaves), 276592 bytes in use [6117] 03 Dec 11:52:27.219 - 1 clients connected (0 slaves), 276592 bytes in use [6117] 03 Dec 11:52:32.308 - 1 clients connected (0 slaves), 276592 bytes in use [6117] 03 Dec 11:52:37.390 - 1 clients connected (0 slaves), 276592 bytes in use |
若出现上面类似的信息,说明主从配置成功了。下面要进行示例验证
示例验证:
在主上创建数据
[root@master ~]# redis-cli -a 123456
127.0.0.1:6379> set key1 123 OK 127.0.0.1:6379> set key2 456 OK 127.0.0.1:6379> sadd set1 aaa (integer) 1 127.0.0.1:6379> sadd set1 bbb (integer) 1 127.0.0.1:6379> sadd set1 ccc (integer) 1 127.0.0.1:6379> keys * 1) "set1" 2) "key2" 3) "key1" |
在从上查看
[root@slave ~]# redis-cli
127.0.0.1:6379> keys * 1) "key2" 2) "set1" 3) "key1" 127.0.0.1:6379> get key1 "123" 127.0.0.1:6379> get key2 "456" 127.0.0.1:6379> smembers set1 1) "ccc" 2) "aaa" 3) "bbb" |
由上可知,实现了主从同步。
四、主从其他相关配置
1)slave-read-only yes //让从只读
2)repl-ping-slave-period 10 //设置slave向master发起ping的频率,每10s发起一次
3)repl-timeout 60 //设置slave ping不通master多少秒后就超时
4)repl-disable-tcp-nodelay no //是否开启tcp_nodelay,开启后将会使用更少的带宽,但会有延迟,所以建议关闭
5)repl-backlog-size 1mb //同步队列的长度,backuplog是master的一个缓冲区,主从断开后,master会先把数据写入到缓冲区,slave再次连接会从缓冲区中同步数据
6)repl-backlog-ttl 3600 //主从断开后,缓冲区的有效期,默认1小时
7)slave-priority 100 //多个slave是可以设置优先级的,数值越小优先级越高,应用于集群中,支持slave切换为master,优先级最高的才会切换
8)min-slaves-to-write 3 //和下面的一起使用,它的意思是master发现有超过3个slave的延迟高于10s,那么master就会暂停写操作。这两个数值任何一个为0,则关闭该功能,默认第一个数值是0
9)min-slaves-max-lag 10
本文出自 “M四月天” 博客,请务必保留此出处http://msiyuetian.blog.51cto.com/8637744/1719176
原文地址:http://msiyuetian.blog.51cto.com/8637744/1719176