本节对Redis的配置文件进行讲解,redis.conf ,Redis 版本6.0.8
1. Units 单位
- 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不知吃bit。
- 对大小写不敏感
1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 1024*1024 bytes
1g => 1000000000 bytes
1gb => 1024*1024*1024 bytes
# units are case insensitive so 1GB 1Gb 1gB are all the same.
2. GENERAL
# 如果值是“yes”,则启动服务的时候是后台守护进程形式,如果值是“no”,则相反
daemonize no
# 当redis以守护进程启动的时候,redis会默认把pid写入指定存储Redis进程号的文件路径
pidfile /var/run/redis_6379.pid
# redis 的日志级别
# debug (a lot of information, useful for development/testing) -----开发阶段,调试使用
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# 日志记录方式,如果以守护进程启动,且使用默认的标志输出模式,则日志将会发送到 /dev/null
# Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
# 是否输出系统日志,默认no 不输出
syslog-enabled no
# 指定系统日志的标识
syslog-ident redis
# 指定syslog设备,值可以是USER 或者 LOCAL0-LOCAL7
syslog-facility local0
# redis的库数量
databases 16
# 指定本地数据库的存放目录
dir./
3. Network
# 绑定的主机地址
bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
tcp-backlog
设置tcp的backlog,backlog 是一个连接队列,backlog队列总和=未完成的三次握手队列+已经完成的三次握手队列。
在高并发条件下需要一个高backlog值来避免慢客户端连接问题。注意Linux 内核会将这个值减小到 /proc/sys/net/core/somaxconn 的值,所以需要确认增大somaxconn和tcp_max_syn_backlog 这两个值来达到想要的效果。
tcp-keepalive
单位为秒,如果设置为0,则不会进行Keepalive 检测,建议设置成60。
4. Security
starting with Redis 6 "requirepass" is just a compatiblity
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
查看当前redis是否需要密码
config get requirepass
# 设置密码
config set requirepass "123456"
# 登入时验证密码
auth 123456
5. MEMORY MANAGEMENT 内存管理(缓存淘汰/过期策略)
maxmemory <bytes>
# Redis 缓存过期策略 默认是永不过期生产上不能这样设置
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select one from the following behaviors:
#
# volatile-lru -> Evict using approximated LRU, only keys with an expire set. -> 使用最近最少使用(LRU)策略,只移除设置了过期时间的Key。
# allkeys-lru -> Evict any key using approximated LRU. -> 使用最近最少使用(LRU策略,移除所有的Key。
# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. -> 从所有配置了过期时间的键中驱逐使用频率最少的键。
# allkeys-lfu -> Evict any key using approximated LFU. -> 从所有键中驱逐使用频率最少的键。
# volatile-random -> Remove a random key having an expire set. -> 在过期集合中移除随机的Key,只对设置了过期时间的Key。
# allkeys-random -> Remove a random key, any key. -> -> 在过期集合中移除随机的Key。
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) -> 移除那些TTL值最小的Key,即那些最近要过期的Key。
# noeviction -> Don't evict anything, just return an error on write operations. -> 永不过期,针对写操作只是返回错误码。
maxmemory-policy noeviction
# 设置样本的数量
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
maxmemory-samples 5
**注释:**LFU是在Redis4.0后出现的,LRU的最近最少使用实际上并不精确,考虑下面的情况,如果在|处删除,那么A距离的时间最久,但实际上A的使用频率要比B频繁,所以合理的淘汰策略应该是淘汰B。LFU就是为应对这种情况而生的。
6. 配置RDB持久化
命令: save 秒钟 写操作次数
################################ SNAPSHOTTING ################################
# Save the DB on disk:
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed -> 15 分钟以内 一个key内 就存成一个 dump.rdb
# after 300 sec (5 min) if at least 10 keys changed -> 5 分钟以内有10个key以上被改变就存成 dump.rdb
# after 60 sec if at least 10000 keys changed -> 1 分钟内改变了 10000次
# 如果想禁用RDB持久化策略,只要不设置任何SAVE指令,或者给save设置成空字符串也可以。
save 900 1
save 300 10
save 60 10000
# 出错了的话就停止,如果是no的话表示不在乎数据的一致性或者有其他手段发现和控制
stop-writes-on-bgsave-error yes
# 对于磁盘中的快照,设置是否进行压缩存储,如果是的话会采用 LZF 算法进行压缩。
rdbcompression yes
# 存储快照之后设置redis是否使用 CRC64 算法进行数据校验。
rdbchecksum yes
# 快照文件的名称
dbfilename dump.rdb
# dump文件的目录
dir ./
7.配置AOF持久化
# 默认是 no 关闭状态的
appendonly yes
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
# 可配置一下方式
# always : 同步持久化 每次发生数据变化会被立即记录到磁盘,性能可能差点,但是数据完整性高
# everysec : 出厂默认推荐,异步操作,每秒记录 如果一秒内宕机,有数据会丢失
# no : 关闭
appendfsync everysec
# 重写的时候 是否可以用 appendsync,默认用no,保证数据的安全性
no-appendfsync-on-rewrite no
# 触发重写的默认配置是当AOF文件大小是上次rewrite后大小的一倍
auto-aof-rewrite-percentage 100
# 触发重写的默认配置且文件大于64M的时候触发。生产的时候一般是几个G以上
auto-aof-rewrite-min-size 64mb