Redis 11 配置

参考源

【狂神说Java】Redis最新超详细版教程通俗易懂_哔哩哔哩_bilibili

版本

本文章基于 Redis 6.2.6


基本配置

Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf

在 Linux 中,可以使用 whereis redis 查找 Redis 的安装目录

[root@sail]# whereis redis
redis: /usr/local/redis
[root@sail]# cd /usr/local/redis
[root@sail redis]# ls
00-RELEASENOTES  BUGS     CONTRIBUTING  deps      INSTALL   MANIFESTO  redis.conf  runtest-cluster    runtest-sentinel  src    TLS.md
bin              CONDUCT  COPYING       dump.rdb  Makefile  README.md  runtest     runtest-moduleapi  sentinel.conf     tests  utils

命令查看所有配置

127.0.0.1:6379> config get *
  1) "rdbchecksum"
  2) "yes"
  3) "daemonize"
  4) "no"
  5) "io-threads-do-reads"
  6) "no"
  7) "lua-replicate-commands"
  8) "yes"
  9) "always-show-logo"
 10) "no"
 11) "protected-mode"
 12) "yes"
 13) "rdbcompression"
 14) "yes"
 15) "rdb-del-sync-files"
 16) "no"
 17) "activerehashing"
 18) "yes"
 19) "stop-writes-on-bgsave-error"
 20) "yes"
 21) "set-proc-title"
 22) "yes"
 23) "dynamic-hz"
 24) "yes"
 25) "lazyfree-lazy-eviction"
 26) "no"
 27) "lazyfree-lazy-expire"
 28) "no"
 29) "lazyfree-lazy-server-del"
 30) "no"
 31) "lazyfree-lazy-user-del"
 32) "no"
 33) "lazyfree-lazy-user-flush"
 34) "no"
 35) "repl-disable-tcp-nodelay"
 36) "no"
 37) "repl-diskless-sync"
 38) "no"
 39) "gopher-enabled"
 40) "no"
 41) "aof-rewrite-incremental-fsync"
 42) "yes"
 43) "no-appendfsync-on-rewrite"
 44) "no"
 45) "cluster-require-full-coverage"
 46) "yes"
 47) "rdb-save-incremental-fsync"
 48) "yes"
 49) "aof-load-truncated"
 50) "yes"
 51) "aof-use-rdb-preamble"
 52) "yes"
 53) "cluster-replica-no-failover"
 54) "no"
 55) "cluster-slave-no-failover"
 56) "no"
 57) "replica-lazy-flush"
 58) "no"
 59) "slave-lazy-flush"
 60) "no"
 61) "replica-serve-stale-data"
 62) "yes"
 63) "slave-serve-stale-data"
 64) "yes"
 65) "replica-read-only"
 66) "yes"
 67) "slave-read-only"
 68) "yes"
 69) "replica-ignore-maxmemory"
 70) "yes"
 71) "slave-ignore-maxmemory"
 72) "yes"
 73) "jemalloc-bg-thread"
 74) "yes"
 75) "activedefrag"
 76) "no"
 77) "syslog-enabled"
 78) "no"
 79) "cluster-enabled"
 80) "no"
 81) "appendonly"
 82) "no"
 83) "cluster-allow-reads-when-down"
 84) "no"
 85) "crash-log-enabled"
 86) "yes"
 87) "crash-memcheck-enabled"
 88) "yes"
 89) "use-exit-on-panic"
 90) "no"
 91) "disable-thp"
 92) "yes"
 93) "cluster-allow-replica-migration"
 94) "yes"
 95) "replica-announced"
 96) "yes"
 97) "aclfile"
 98) ""
 99) "unixsocket"
100) ""
101) "pidfile"
102) ""
103) "replica-announce-ip"
104) ""
105) "slave-announce-ip"
106) ""
107) "masteruser"
108) ""
109) "cluster-announce-ip"
110) ""
111) "syslog-ident"
112) "redis"
113) "dbfilename"
114) "dump.rdb"
115) "appendfilename"
116) "appendonly.aof"
117) "server_cpulist"
118) ""
119) "bio_cpulist"
120) ""
121) "aof_rewrite_cpulist"
122) ""
123) "bgsave_cpulist"
124) ""
125) "ignore-warnings"
126) ""
127) "proc-title-template"
128) "{title} {listen-addr} {server-mode}"
129) "masterauth"
130) ""
131) "requirepass"
132) ""
133) "supervised"
134) "no"
135) "syslog-facility"
136) "local0"
137) "repl-diskless-load"
138) "disabled"
139) "loglevel"
140) "notice"
141) "maxmemory-policy"
142) "noeviction"
143) "appendfsync"
144) "everysec"
145) "oom-score-adj"
146) "no"
147) "acl-pubsub-default"
148) "allchannels"
149) "sanitize-dump-payload"
150) "no"
151) "databases"
152) "16"
153) "port"
154) "6379"
155) "io-threads"
156) "1"
157) "auto-aof-rewrite-percentage"
158) "100"
159) "cluster-replica-validity-factor"
160) "10"
161) "cluster-slave-validity-factor"
162) "10"
163) "list-max-ziplist-size"
164) "-2"
165) "tcp-keepalive"
166) "300"
167) "cluster-migration-barrier"
168) "1"
169) "active-defrag-cycle-min"
170) "1"
171) "active-defrag-cycle-max"
172) "25"
173) "active-defrag-threshold-lower"
174) "10"
175) "active-defrag-threshold-upper"
176) "100"
177) "lfu-log-factor"
178) "10"
179) "lfu-decay-time"
180) "1"
181) "replica-priority"
182) "100"
183) "slave-priority"
184) "100"
185) "repl-diskless-sync-delay"
186) "5"
187) "maxmemory-samples"
188) "5"
189) "maxmemory-eviction-tenacity"
190) "10"
191) "timeout"
192) "0"
193) "replica-announce-port"
194) "0"
195) "slave-announce-port"
196) "0"
197) "tcp-backlog"
198) "511"
199) "cluster-announce-bus-port"
200) "0"
201) "cluster-announce-port"
202) "0"
203) "cluster-announce-tls-port"
204) "0"
205) "repl-timeout"
206) "60"
207) "repl-ping-replica-period"
208) "10"
209) "repl-ping-slave-period"
210) "10"
211) "list-compress-depth"
212) "0"
213) "rdb-key-save-delay"
214) "0"
215) "key-load-delay"
216) "0"
217) "active-expire-effort"
218) "1"
219) "hz"
220) "10"
221) "min-replicas-to-write"
222) "0"
223) "min-slaves-to-write"
224) "0"
225) "min-replicas-max-lag"
226) "10"
227) "min-slaves-max-lag"
228) "10"
229) "maxclients"
230) "10000"
231) "active-defrag-max-scan-fields"
232) "1000"
233) "slowlog-max-len"
234) "128"
235) "acllog-max-len"
236) "128"
237) "lua-time-limit"
238) "5000"
239) "cluster-node-timeout"
240) "15000"
241) "slowlog-log-slower-than"
242) "10000"
243) "latency-monitor-threshold"
244) "0"
245) "proto-max-bulk-len"
246) "536870912"
247) "stream-node-max-entries"
248) "100"
249) "repl-backlog-size"
250) "1048576"
251) "maxmemory"
252) "0"
253) "hash-max-ziplist-entries"
254) "512"
255) "set-max-intset-entries"
256) "512"
257) "zset-max-ziplist-entries"
258) "128"
259) "active-defrag-ignore-bytes"
260) "104857600"
261) "hash-max-ziplist-value"
262) "64"
263) "stream-node-max-bytes"
264) "4096"
265) "zset-max-ziplist-value"
266) "64"
267) "hll-sparse-max-bytes"
268) "3000"
269) "tracking-table-max-keys"
270) "1000000"
271) "client-query-buffer-limit"
272) "1073741824"
273) "repl-backlog-ttl"
274) "3600"
275) "auto-aof-rewrite-min-size"
276) "67108864"
277) "logfile"
278) ""
279) "watchdog-period"
280) "0"
281) "dir"
282) "/usr/local/redis"
283) "save"
284) "3600 1 300 100 60 10000"
285) "client-output-buffer-limit"
286) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
287) "unixsocketperm"
288) "0"
289) "slaveof"
290) ""
291) "notify-keyspace-events"
292) ""
293) "bind"
294) ""
295) "oom-score-adj-values"
296) "0 200 800"

单位

Units

# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 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.

开头定义了一些基本的度量单位,只支持 bytes,不支持 bit

这些单位对大小写是不敏感的。

包含

INCLUDES

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf

和 Spring 配置文件类似,可以通过 include 包含,redis.conf 可以作为总文件,可以包含其他文件。

网络配置

NETWORK

# 绑定的 IP
bind 127.0.0.1 
# 保护模式
protected-mode yes 
# 默认端口
port 6379

通用

GENERAL

# 默认情况下,Redis 不作为守护进程运行。需要开启的话,改为 yes
daemonize yes 
# 可通过 upstart 和 systemd 管理 Redis 守护进程
supervised no 
# 以后台进程方式运行 redis,则需要指定 pid 文件
pidfile /var/run/redis_6379.pid 
# 日志级别。可选项有:
# debug(记录大量日志信息,适用于开发、测试阶段);
# verbose(较多日志信息);
# notice(适量日志信息,使用于生产环境);
# warning(仅有部分重要、关键信息才会被记录)。
loglevel notice 
# 日志文件的位置,当指定为空字符串时,为标准输出
logfile "" 
# 设置数据库的数目。默认的数据库是 0
databases 16 
# 是否总是显示 logo
always-show-logo yes

快照

SNAPSHOPTING

# 900秒(15分钟)内至少 1 个 key 值改变(则进行数据库保存--持久化)
save 900 1
# 300秒(5分钟)内至少 10 个 key 值改变(则进行数据库保存--持久化)
save 300 10
# 60秒(1分钟)内至少 10000 个 key 值改变(则进行数据库保存--持久化)
save 60 10000
# 持久化出现错误后,是否依然进行继续进行工作
stop-writes-on-bgsave-error yes 
# 使用压缩 rdb 文件 yes:压缩,但是需要一些 cpu 的消耗;no:不压缩,需要更多的磁盘空间
rdbcompression yes 
# 是否校验 rdb 文件,更有利于文件的容错性,但是在保存 rdb 文件的时候,会有大概 10% 的性能损耗
rdbchecksum yes 
# dbfilenamerdb 文件名称
dbfilename dump.rdb 
# dir 数据目录,数据库的写入会在这个目录。rdb、aof 文件也会写在这个目录
dir ./

安全

SECURITY

访问密码的查看,设置和取消。

# 获取密码
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
# 设置密码
127.0.0.1:6379> config set requirepass "123456"
OK
# 重新连接
[root@sail redis]# redis-cli
# 发现使用不了了
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
# 验证
127.0.0.1:6379> auth 123456
OK
# 验证后可以正常使用了
127.0.0.1:6379> ping
PONG

限制

MEMORY

# 设置能连上 redis 的最大客户端连接数量
maxclients 10000 
# redis 配置的最大内存容量
maxmemory <bytes> 
# maxmemory-policy 内存达到上限的处理策略:
# 	volatile-lru:利用 LRU 算法移除设置过过期时间的 key。
# 	volatile-random:随机移除设置过过期时间的 key。
# 	volatile-ttl:移除即将过期的 key,根据最近过期时间来删除(辅以 TTL)
# 	allkeys-lru:利用 LRU 算法移除任何 key。
# 	allkeys-random:随机移除任何 key。
# 	noeviction:不移除任何 key,只是返回一个写错误。
maxmemory-policy noeviction

仅追加模式

APPEND ONLY MODE

# 是否以 append only 模式作为持久化方式,默认使用的是 rdb 方式持久化,这种方式在许多应用中已经足够用了
appendonly no 
# appendfilename AOF 文件名称
appendfilename "appendonly.aof" 
# appendfsync aof 持久化策略的配置:
# 	no:不执行 fsync,由操作系统保证数据同步到磁盘,速度最快。
# 	always:每次写入都执行 fsync,以保证数据同步到磁盘。
# 	everysec:每秒执行一次 fsync,可能会导致丢失这1s数据。
appendfsync everysec

配置介绍

daemonize no

Redis 默认不是以守护进程的方式运行,可以通过该配置项修改,使用 yes 启用守护进程。

pidfile /var/run/redis.pid

当 Redis 以守护进程方式运行时,Redis 默认会把 pid 写入 /var/run/redis.pid 文件,可以通过 pidfile 指定。

port 6379

指定 Redis 监听端口,默认端口为 6379。

作者在自己的一篇博文中解释了为什么选用 6379 作为默认端口,因为 6379 在手机按键上 MERZ 对应的号码,而 MERZ 取自意大利歌女 Alessia Merz 的名字。

bind 127.0.0.1

绑定的主机地址。

timeout 300

当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能。

loglevel verbose

指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为 verbose。

  • debug:记录大量日志信息,适用于开发、测试阶段

  • verbose:较多日志信息

  • notice:适量日志信息,使用于生产环境

  • warning:仅有部分重要、关键信息才会被记录

logfile stdout

日志记录方式,默认为标准输出,如果配置 Redis 为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给 /dev/null。

databases 16

设置数据库的数量,默认数据库为 0,可以使用 SELECT 命令在连接上指定数据库 id。

save 900 1
save 300 10
save 60 10000

指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合。

Redis 默认配置文件中提供了三个条件:

  • 900 秒(15 分钟)内有 1 个更改

  • 300 秒(5 分钟)内有 10 个更改

  • 60 秒(1 分钟)内有 10000 个更改

rdbcompression yes

指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大。

dbfilename dump.rdb

指定本地数据库文件名,默认值为 dump.rdb。

dir ./

指定本地数据库存放目录。

slaveof

设置当本机为 slav 服务时,设置 master 服务的 IP 地址及端口,在 Redis 启动时,它会自动从 master 进行数据同步。

masterauth

当 master 服务设置了密码保护时,slav 服务连接 master 的密码。

requirepass foobared

设置Redis连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 auth 命令提供密码,默认关闭。

maxclients 128

设置同一时间最大客户端连接数,默认无限制。

Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数。

如果设置 maxclients 0,表示不作限制。

当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。

maxmemory

指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中。

达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key。

当此方法处理后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。

Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区。

appendonly no

指定是否在每次更新操作后进行日志记录,Redis 在默认情况下是异步的把数据写入磁盘。

如果不开启,可能会在断电时导致一段时间内的数据丢失。

因为 redis 本身同步数据文件是按上面 save 条件来同步的,所以有的数据会在一段时间内只存在于内存中。

默认为 no。

appendfilename appendonly.aof

指定更新日志文件名,默认为 appendonly.aof。

appendfsync everysec

指定更新日志条件,共有 3 个可选值:

  • no:表示等操作系统进行数据缓存同步到磁盘(快)
  • always:表示每次更新操作后手动调用 fsync() 将数据写到磁盘(慢,安全)
  • everysec:表示每秒同步一次(折衷,默认值)

vm-enabled no

指定是否启用虚拟内存机制,默认值为 no。

VM 机制将数据分页存放,由 Redis 将访问量较少的页即冷数据 swap 到磁盘上,访问多的页面由磁盘自动换出到内存中。

vm-swap-file /tmp/redis.swap

虚拟内存文件路径,默认值为 /tmp/redis.swap,不可多个 Redis 实例共享。

vm-max-memory 0

将所有大于 vm-max-memory 的数据存入虚拟内存。

无论 vm-max-memory 设置多小,所有索引数据都是内存存储的(Redis 的索引数据 就是 keys)。

也就是说,当 vm-max-memory 设置为 0 的时候,其实是所有 value 都存在于磁盘。

默认值为 0。

vm-page-size 32

Redis swap 文件分成了很多的 page,一个对象可以保存在多个 page 上面,但一个 page 上不能被多个对象共享。

vm-page-size 是要根据存储的数据大小来设定的,作者建议如果存储很多小对象。

page 大小最好设置为 32 bytes 或者 64 bytes。

如果存储很大大对象,则可以使用更大的 page,如果不确定,就使用默认值。

vm-pages 134217728

设置 swap 文件中的 page 数量。

由于页表(一种表示页面空闲或使用的 bitmap)是在放在内存中的。

在磁盘上每 8 个 pages 将消耗 1 byte 的内存。

vm-max-threads 4

设置访问 swap 文件的线程数,最好不要超过机器的核数。

如果设置为 0,那么所有对 swap 文件的操作都是串行的,可能会造成比较长时间的延迟。

默认值为 4。

glueoutputbuf yes

设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启。

hash-max-zipmap-entries 64
hash-max-zipmap-value 512

指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法。

activerehashing yes

指定是否激活重置哈希,默认为开启。

include /path/to/local.conf

指定包含其它的配置文件,可以在同一主机上多个 Redis 实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天航星

感谢你的鼓励和认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值