1. Redis主从配置过程
在本机启动三个redis实例,分别为:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
其中,6379为Master,6380和6381为Slave。
1.1. copy一份redis
$ sudo cp -r redis-3.2.9 redis-3.2.9-6380
$ sudo cp -r redis-3.2.9 redis-3.2.9-6381
$ cd ..
$ sudo ln -s source/redis-3.2.9-6380 redis-6380
$ sudo ln -s source/redis-3.2.9-6381 redis-6381
$ ls -al
total 40
drwxr-xr-x 20 root wheel 680 10 11 11:33 .
drwxr-xr-x@ 11 root wheel 374 11 9 2016 ..
lrwxr-xr-x 1 root wheel 29 5 19 13:19 redis -> /usr/local/source/redis-3.2.9
lrwxr-xr-x 1 root wheel 23 10 11 11:33 redis-6380 -> source/redis-3.2.9-6380
lrwxr-xr-x 1 root wheel 23 10 11 11:33 redis-6381 -> source/redis-3.2.9-6381
1.2. 更改配置文件
分别进入redis-6380目录和redis-6381目录,修改其etc目录下的redis.conf配置文件,以6380为例,修改内容如下:
# 更改端口号为6380
port 6380
# 更改pidfile路径
pidfile /var/run/redis_6380.pid
# 更改日志文件路径
logfile "/usr/local/redis-6380/log-redis.log"
# 更改rdb和aof目录路径
dir /usr/local/redis-6380/db/
# 更改master服务器IP和端口号
slaveof 127.0.0.1 6379
# 如果master设置了访问密码,添加
masterauth 123456
requirepass 123456
1.3. 启动redis
$ cd /usr/local/redis
$ sudo bin/redis-server etc/redis.conf
$ cd /usr/local/redis-6380
$ sudo bin/redis-server etc/redis.conf
$ cd /usr/local/redis-6381
$ sudo bin/redis-server etc/redis.conf
$ ps -ef | grep redis
0 8224 1 0 11:44上午 ?? 0:00.13 bin/redis-server 127.0.0.1:6379
0 8249 1 0 11:46上午 ?? 0:00.06 bin/redis-server 127.0.0.1:6380
0 8262 1 0 11:46上午 ?? 0:00.05 bin/redis-server 127.0.0.1:6381
1.4. 测试
在master插入一个key:
$ bin/redis-cli
127.0.0.1:6379> set test1 value1
OK
127.0.0.1:6379>
打开另一个控制台,分别连接6380和6381,查看key为test1的值:
$ redis-6380 bin/redis-cli -p 6380
127.0.0.1:6380> get test1
"value1"
127.0.0.1:6380>
$ redis-6381 bin/redis-cli -p 6381
127.0.0.1:6381> get test1
"value1"
127.0.0.1:6381>
1.5. 查看日志
查看一下redis日志文件,日志文件路径为上面我们配置的 logfile 路径:
1.5.1. master日志
12956:M 12 Oct 10:38:32.224 # Server started, Redis version 3.2.9
12956:M 12 Oct 10:38:32.225 * The server is now ready to accept connections on port 6379
12956:M 12 Oct 10:39:13.386 * Slave 127.0.0.1:6380 asks for synchronization
12956:M 12 Oct 10:39:13.386 * Full resync requested by slave 127.0.0.1:6380
12956:M 12 Oct 10:39:13.386 * Starting BGSAVE for SYNC with target: disk
12956:M 12 Oct 10:39:13.386 * Background saving started by pid 12992
12992:C 12 Oct 10:39:13.388 * DB saved on disk
12956:M 12 Oct 10:39:13.389 * Background saving terminated with success
12956:M 12 Oct 10:39:13.389 * Synchronization with slave 127.0.0.1:6380 succeeded
12956:M 12 Oct 10:42:43.055 * Slave 127.0.0.1:6381 asks for synchronization
12956:M 12 Oct 10:42:43.055 * Full resync requested by slave 127.0.0.1:6381
12956:M 12 Oct 10:42:43.055 * Starting BGSAVE for SYNC with target: disk
12956:M 12 Oct 10:42:43.055 * Background saving started by pid 13068
13068:C 12 Oct 10:42:43.056 * DB saved on disk
1.5.2. Slave日志
13065:S 12 Oct 10:42:43.054 # Server started, Redis version 3.2.9
13065:S 12 Oct 10:42:43.054 * The server is now ready to accept connections on port 6381
13065:S 12 Oct 10:42:43.054 * Connecting to MASTER 127.0.0.1:6379
13065:S 12 Oct 10:42:43.054 * MASTER <-> SLAVE sync started
13065:S 12 Oct 10:42:43.054 * Non blocking connect for SYNC fired the event.
13065:S 12 Oct 10:42:43.054 * Master replied to PING, replication can continue...
13065:S 12 Oct 10:42:43.055 * Partial resynchronization not possible (no cached master)
13065:S 12 Oct 10:42:43.056 * Full resync from master: 62a809fabb384640d7ce03b3053b305f65e6abe1:339
13065:S 12 Oct 10:42:43.070 * MASTER <-> SLAVE sync: receiving 94 bytes from master
13065:S 12 Oct 10:42:43.070 * MASTER <-> SLAVE sync: Flushing old data
13065:S 12 Oct 10:42:43.070 * MASTER <-> SLAVE sync: Loading DB in memory
13065:S 12 Oct 10:42:43.071 * MASTER <-> SLAVE sync: Finished with success
1.6. 查看状态
使用INFO命令查看复制状态。
1.6.1. MASTER状态
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=1054,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=1054,lag=0
master_repl_offset:1054
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1053
1.6.2. Slave状态
127.0.0.1:6380> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:1180
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
2. FAQ
2.1. slave只读
从redis2.6开始slaves可以支持只读模式,这个行为是用slave-read-only参数来控制,也可以使用命令CONFIG SET 来开启或关闭 只读模式将会拒绝所有写的命令,所以对slave进行写是不可能的。如果希望slave示例可以进行修改操作,就需要设置slave-read-only参数为no。
127.0.0.1:6380> set keykey kkkkk
(error) READONLY You can't write against a read only slave.
2.2. 设置slave到master的认证
如果master有是通过密码登陆requirepass,那么在slave下也需要使用密码来同步数据 运行一个redis-cli实例然后输入 config set masterauth
2.3. 常用配置参数
2.3.1. slaveof
设置master ip 与端口
2.3.2. masterauth
设置mstater 密码如果master是有密登录
2.3.3. slave-serve-stale-data yes
当复制正在进行时slave与master链接断掉,有两种方式 yes是slave会继续回应客户端的请求,可能有过时的数据,no slave就会对除了INFO SLAVEOF的其它命令回复一个eroor 给客户端SYNC with master in progress
2.3.4. slave-read-only yes
slave只允许读取数据
2.3.5. repl-diskless-sync no
不通过磁盘来同步(试验阶段)
2.3.6. repl-diskless-sync-delay 5
当无磁盘同步开启后需要配置一个延迟时间,以保证接受多个slave的同步请求,默认为5秒
2.3.7. repl-ping-slave-period 10
多少秒ping一次master
2.3.8. repl-timeout 60
复制的超时时间,这个时间一定要大于ping的时间
2.3.9. repl-disable-tcp-nodelay no
2.3.10. TCP_NODELAY,yes表示使用非常小的TCP包数和非常小的宽带来发送数据。slave那边可以增加一个延迟时间
2.3.11. repl-backlog-size 1mb
backlog的大小,backlog是一个用来积累当slave没有链接后数据的缓存,重新链接后就不需要完整同步啦。- backlog越大,slave断开的时间就越长,再次同步的便会越迟
2.3.12. repl-backlog-ttl 3600
断开链接多少秒后释放backlog,0表示永远不释放
2.3.13. slave-priority 100
slave优先级是当master down了之后被redis哨兵拿来从slave中晋先新的master,数字越小优先级越高,0表示永远不晋选为master
2.3.14. min-slaves-to-write 3
最小slave链接数默认为0
2.3.15. min-slaves-max-lag 10
最小的slave,最大延迟数默认为10