Redis-简单实现星形主从配置

原创 2016-11-09 13:54:27 505
摘要:简单应用场景现在配置redis 星形 集群, 有三台服务器, 怎样实现?复制redis.conf两份, 分别命名为redis6380.conf, redis6381.confmaster指向redis.conf, slave1指向6380.conf, slave2指向redis6381.confmaster关闭rdb, 开启aofslave1开启rdb, 关闭aofslave2

简单应用场景

现在配置redis 星形 集群, 有三台服务器, 怎样实现?
复制redis.conf两份, 分别命名为redis6380.conf, redis6381.conf
master指向redis.conf, slave1指向6380.conf, slave2指向redis6381.conf

master关闭rdb, 开启aof
slave1开启rdb, 关闭aof
slave2关闭rdb和aof

配置redis6380.conf

#pidfile /var/run/redis.pid    改为
pidfile /var/run/redis6380.pid
...
#port 6379    改为
port 6380
...
#dbfilename dump.rdb    改为
dbfilename dump6380.rdb    #让slave1执行rdb工作
...
# slaveof <masterip> <masterport>    改为
slaveof localhost 6379    #表示作为6379的slave
...
appendonly no    #aof也不用产生, 因此关闭
...
slave-read-only yes    #只读

配置redis6381.conf

pidfile /var/run/redis6381.pid
...
port 6381
...
#save 900 1    #两台从服务器其中一台产生rdb就可以了, 另一台没必要再次产生rdb, 因此注释掉
#save 300 10
#save 60 10000
...
appendonly no    #aof也不用产生, 因此关闭
...
slaveof localhost 6379
...
slave-read-only yes    #只读

配置redis.conf

#save 900 1    #因为slave1已经存在rdb了, 所以master不在需要rdb
#save 300 10
#save 60 10000
...
appendonly yes    #master的aof可以打开, 因为主服务器的aof最全最快

启动, 分别在终端打开:

ql@ql:~$ redis-server /usr/local/etc/redis/redis.conf
ql@ql:~$ redis-server /usr/local/etc/redis/redis6380.conf
ql@ql:~$ redis-server /usr/local/etc/redis/redis6381.conf

启动master的客户端

ql@ql:~$ redis-cli
127.0.0.1:6379> set title sunshine
OK
127.0.0.1:6379>

启动slave1的客户端

ql@ql:~$ redis-cli -p 6380
127.0.0.1:6380> keys *    #可以看到master中的内容
1) "title"
127.0.0.1:6380> get title
"sunshine"
127.0.0.1:6380>

启动slave2的客户端

ql@ql:~$ redis-cli -p 6381
127.0.0.1:6381> keys *    #也能看到master中的内容
1) "title"
127.0.0.1:6381> get title
"sunshine"
127.0.0.1:6381>

现在要为master设置密码, 即redis.conf

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
requirepass admin123    #新加的一行

再次打开终端
打开reids-server
打开reids-cli

ql@ql:~$ redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.    #没有输入密码进行认证
127.0.0.1:6379> 
127.0.0.1:6379> auth admin123    #auth+密码进行认证
OK
127.0.0.1:6379> keys *
1) "title"
127.0.0.1:6379> get title
"sunshine"
127.0.0.1:6379>

此时从服务器连不上主服务器, 因为有密码

修改redis6380.conf

# masterauth <master-password>    改为
masterauth admin123

redis6381.conf的修改如上

redis主从复制的缺陷

每次slave断开后(无论是主动断开还是网络故障), 再连接master,
都要 master 全部 dump 出来 rdb 再 aof,
即同步的过程都要执行一遍
所以: 多台slave不要一下同时启动起来, 否则master可能IO剧增, 拖垮master


发布手记

热门词条