环境:CENTOS7.4
Redis版本:4.0.8
1、安装依赖包
yum -y install gcc tcl
2、解压redis源码,并进行安装
上传源码包 redis-4.0.8.tar.gz,拷贝到/usr/local/src目录下
cd /usr/local/src
tar -xzvf redis-4.0.8.tar.gz
cd redis-4.0.8/
2.1 make
遇到警告信息如下:????
ldo.c: In function ‘f_parser’:
ldo.c:496:7: warning: unused variable ‘c’ [-Wunused-variable]
int c = luaZ_lookahead(p->z);
[root@redis1 redis-4.0.8]# make
2.2 make test进行验证
[root@redis1 redis-4.0.8]# make test
…..省略
198 seconds - integration/replication
146 seconds - unit/obuf-limits
\o/ All tests passed without errors!
Cleanup: may take some time... OK
make[1]: Leaving directory `/usr/local/src/redis-4.0.8/src'
2.3 make install
[root@redis1 redis-4.0.8]# make install
注:该步骤实际是把src目录下可执行文件拷贝到/usr/local/bin
- redis-benchmark
- redis-check-aof
- redis-check-rdb
- redis-cli
- redis-sentinel
- redis-server
3、redis配置(192.168.56.4)
mkdir -p /opt/redis/6379 && cp redis.conf /opt/redis/6379/6379.conf
mkdir -p /opt/redis/6380 && cp redis.conf /opt/redis/6380/6380.conf
mkdir -p /opt/redis/6381 && cp redis.conf /opt/redis/6381/6381.conf
vim /opt/redis/6379/6379.conf
vim /opt/redis/6380/6380.conf
vim /opt/redis/6381/6381.conf
配置文件内容:
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/6379/redis_6379.pid"
loglevel notice
logfile "/opt/redis/6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis"
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100 ###当发生主从切换时,哨兵根据优先级来选主
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
slaveof 192.168.56.5 6379
1.bind 1270.0.1 (注销掉该语句则侦听所有的IP,或者改为服务器侦听的IP地址)
2.port 6380(端口号)
3.daemonize yes(守护进程)
4.pidfile "/var/run/redis_6380.pid"(进程的目录)
5.slaveof 192.168.200.108 6380(设置为此redis为slave,端口号是6380) 此只需要在从库上配置即可
cd /usr/local/src/redis-4.0.8/src
/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6379/6379.conf
/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6380/6380.conf
/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6381/6381.conf
4、哨兵配置
port 26379
dir "/tmp"
sentinel myid 446bca53fde07f5d6df5624a7d691f2acdf07023
sentinel monitor mymaster 192.168.56.5 6379 1
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 2
sentinel leader-epoch mymaster 2
daemonize yes
sentinel known-slave mymaster 192.168.56.4 6379
sentinel current-epoch 2
5、验证
[root@redis1 redis]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.56.5,port=6379,state=online,offset=252,lag=0
master_replid:d0529fe95b618ea03a7771296c06eaa8fd0f511c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
127.0.0.1:6379>
MASTER宕机,主从切换后,即使原来主恢复正常,也不会变成主,角色会变成从。
SALVE节点宕机,不影响主和其他从,恢复正常后可自动加入集群