【Redis运维】Redis 5.x配置文件示例与说明

TangLu Redis 2022-01-05 1808 0

官方注释部分已经去掉,保留了线上常用配置

################################## NETWORK #####################################
bind 172.20.1.190
protected-mode yes
port 6379
tcp-backlog 2048
timeout 300                    #客户端连接最大空闲时间,超出这个空闲时长的连接将被自动关闭
tcp-keepalive 300              ##通过 TCP 层面保持连接有效性,防止网络不稳定导致连接中断
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/data/redis/logs/redis.log"
databases 16
always-show-logo yes
maxclients 10000
lua-time-limit 5000
slowlog-log-slower-than 100000
slowlog-max-len 1000
latency-monitor-threshold 0

################################ SNAPSHOTTING  ################################
#save 900 1
#save 300 10
#save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/rdb/
rdb-save-incremental-fsync yes

################################# REPLICATION #################################
# replicaof <masterip> <masterport>
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 100mb
repl-backlog-ttl 1800
replica-priority 100
# min-replicas-to-write 3
# min-replicas-max-lag 10
# replica-announce-ip 5.5.5.5
# replica-announce-port 1234

################################## SECURITY ###################################
requirepass 123456
#rename-command FLUSHALL ADMINFLUSHALL      #将FLUSHALL重命名为ADMINFLUSHALL
#rename-command FLUSHDB  ADMINFLUSHDB
#rename-command CONFIG   ADMINCONFIG            #经测试如果对config命令重命名会导致哨兵切换出现"next failover delay: I will not start a failover before"的异常
rename-command KEYS ADMINKEYS

############################## MEMORY MANAGEMENT ################################
maxmemory 8G
maxmemory-policy volatile-lfu
maxmemory-samples 5
replica-ignore-maxmemory yes              #从Redis5开始,从节点默认忽略自身maxmemory配置,除非发生故障转移后升级为主服务器。即只有主节点才会执行过期淘汰策略,并且master执行del操作后从节点也会执行相同操作,保证主从数据一致性

############################# LAZY FREEING ####################################
lazyfree-lazy-eviction yes                #内存达到maxmemory并设置了淘汰策略时是否尝试异步释放内存
lazyfree-lazy-expire yes                  #key在过期删除时是否尝试异步释放内存
lazyfree-lazy-server-del yes              #执行RENAME/MOVE等命令或需要覆盖一个key时,删除旧key是否尝试异步释放内存
replica-lazy-flush yes                    #主从执行全量同步时,从库在一开始清空当前数据库时是否异步释放内存

############################## APPEND ONLY MODE ###############################
appendonly no
# appendfilename "appendonly.aof"
# appendfsync everysec
# appendfsync no
# no-appendfsync-on-rewrite no
# auto-aof-rewrite-percentage 100
# auto-aof-rewrite-min-size 64mb
# aof-load-truncated yes
# aof-use-rdb-preamble yes
# aof-rewrite-incremental-fsync yes

############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""

############################### ADVANCED CONFIG ###############################
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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
hz 50
dynamic-hz yes

########################### ACTIVE DEFRAGMENTATION #######################
activedefrag yes                  #开启碎片动态整理,需要2个条件同时满足才会触发清理
active-defrag-ignore-bytes 1G            #条件1,碎片达到1G就开始清理
active-defrag-threshold-lower 10          #条件2,碎片空间占操作系统分配给 Redis 的总空间比例达到 10% 时,开始清理
active-defrag-threshold-upper 50          #内存碎片率超过50%,尽最大努力碎片整理
active-defrag-cycle-min 10             #清理过程所用CPU时间比例不低于10%,保证清理能正常开展
active-defrag-cycle-max 30             #清理过程所用CPU时间比例不高于30%,一旦超过就停止清理,避免清理时发生大量内存拷贝阻塞Redis
active-defrag-max-scan-fields 1000          #碎片整理期间,对于 List/Set/Hash/ZSet 类型元素一次 Scan 的数量


Redis 5.x哨兵配置

bind 172.20.1.192
protected-mode yes
port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/data/redis/logs/sentinel.log"
dir /data/redis/sentinel/

sentinel monitor mymaster 172.20.1.192 6379 2
sentinel auth-pass mymaster a5791E2a3043T
sentinel down-after-milliseconds mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000


通过命令 memory purge 手动清理内存碎片

 redis-cli -p 6389 memory purge

异步FLUSH清理整个实例或DB

127.0.0.1:6379> flushall async


评论