Redis的配置文件redis.conf详解
阅读原文时间:2023年07月08日阅读:1

Redis的配置文件位于redis的安装目录下,一般不要直接操作出厂设置的配置文件,需要对其进行备份。# Redis的配置文件样例:

# Redis configuration file example.# 
# 请注意,为了读取到配置文件,Redis必须文件路径作为第一个参数来启动:

./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.

1.INCLUDE部分:

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

在配置多个服务器时很有用,比如,多个服务器的很多个配置项是相同的,但是少部分配

置又要不相同,例如:bind等配置,这时可以将相同的配置抽取出来,作为一个单独文件.

其他配置项根据不同的服务器做不同的配置,最后通过include引用;如果include放在文件

的开头,那么redis.conf的配置会覆盖引入文件中的配置,所以如果想要使用引入文件中的配置

可以将include放在最后

include /path/to/local.conf

include /path/to/other.conf

2.MODULES部分:

################################## MODULES #####################################

Load modules at startup. If the server is not able to load modules

it will abort. It is possible to use multiple loadmodule directives.

loadmodule /path/to/my_module.so

loadmodule /path/to/other_module.so

NETWORK部分:

################################## NETWORK ###################################### 
# 默认情况下,如果未指定“ bind”配置指令,则Redis监听服务器上所有可用网络接口的连接。
# 可以使用“ bind”配置指令侦听一个或多个选定接口,然后侦听一个 或更多IP地址

Examples:

bind 192.168.1.100 10.0.0.1

bind 127.0.0.1 ::1

~ WARNING ~

# 如果运行Redis的电脑直接暴露在互联网中,那么就会将实例暴露给所有人,这是很危险的。

指定 redis 只接收来自于该 IP 地址的请求,如果不进行设置,那么将处理所有请求

bind 127.0.0.1

Protected mode is a layer of security protection, in order to avoid that

Redis instances left open on the internet are accessed and exploited.

当开启保护模式时,即使没有绑定任何IP,也没有配置密码,Redis仍然只接受本机的访问

The server only accepts connections from clients connecting from the

IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain

sockets.

By default protected mode is enabled. You should disable it only if

you are sure you want clients from other hosts to connect to Redis

even if no authentication is configured, nor a specific set of interfaces

# are explicitly listed using the "bind" directive.
protected-mode yes

redis监听的端口号。

port 6379

# 此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度,
# 当然此值必须不大于Linux系统定义的/proc/sys/net/core/somaxconn值
# 默认是511,而Linux的默认参数值是128。
# 当系统并发量大并且客户端速度缓慢的时候,可以将这二个参数一起参考设定。
# 该内核参数默认值一般是128,对于负载很大的服务程序来说大大的不够。
# 一般会将它修改为2048或者更大。
# 在/etc/sysctl.conf中添加:net.core.somaxconn = 2048,
# 然后在终端中执行sysctl -p。

tcp-backlog 511

_# 配置unix socket来让redis支持监听本地连接。
# unixsocket /var/run/redis/redis.sock
# 配置unix socket使用文件的权限

unixsocketperm 700

此参数为设置客户端空闲超过timeout,服务端会断开连接,为0则服务端不会主动断开连接,不能小于0。

timeout 0_

# tcp keepalive参数。如果设置不为0,就使用配置的SO_KEEPALIVE值,使用keepalive有两个好处:检测挂掉的对端。降低中间设备出问题而

# 导致网络看似连接却已经与对端端口的问题。

# 在Linux内核中,设置了keepalive,redis会定时给对端发送ack。检测到对端关闭需要两倍的设置值。

# 此选项的合理值是300秒,这是从Redis 3.2.1开始的新Redis默认值

tcp-keepalive 300

GENERAL部分:

################################# GENERAL #####################################

# 默认情况下,Redis不会作为守护程序运行。 如果需要,请使用“yes”。# 请注意,Redis守护进程将在/var/run/redis.pid中写入一个pid文件

daemonize yes

_# If you run Redis from upstart or systemd, Redis can interact with your

supervision tree. Options:

supervised no - no supervision interaction

supervised upstart - signal upstart by putting Redis into SIGSTOP mode

supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET

supervised auto - detect upstart or systemd method based on

UPSTART_JOB or NOTIFY_SOCKET environment variables

Note: these supervision methods only signal "process is ready."

They do not enable continuous liveness pings back to your supervisor.

supervised no___

# 进程管道id文件,如果指定了pid文件,则Redis会在启动时将其写入指定位置,然后在退出时将其删除。 _# 当服务器在非守护进程下运行时,如果没有pid文件,则不会创建在配置中指定。 # 当服务器是后台进程时,pidfile是不被指定也会被使用,默认为/var/run/redis.pid
# 如果Redis无法创建它,则不会发生任何不良情况,服务器将正常启动并运行。_

____pidfile /var/run/redis_6379.pid

日志级别:

debug (开发/测试阶段)

verbose (有用信息比较少,但不会像debug这么混乱)

notice (中等冗长,您可能想在生产中使用)

warning (用于生产环境)

loglevel notice

用于指定记录日志的文件,空字符串的话,日志会打印到标准输出设备中。后台运行的redis的标准输出是/dev/null

logfile ""

是否打开记录syslog的功能

syslog-enabled no

syslog的标识符

syslog-ident redis

指定syslog的设备,值可以是user或者是local0-local7

syslog-facility local0

数据库的数量,默认使用的是数据库0,可以使用select命令来切换数据库

databases 16

By default Redis shows an ASCII art logo only when started to log to the

standard output and if the standard output is a TTY. Basically this means

that normally a logo is displayed only in interactive sessions.

However it is possible to force the pre-4.0 behavior and always show a

ASCII art logo in startup logs by setting the following option to yes.

always-show-logo yes____

SNAPSHOTTING部分(Redis持久化之rdb):

_################################ SNAPSHOTTING ################################
# Redis的持久化

Save the DB on disk:

保存格式:

save

# 如果同时发生了给定的秒数和给定的针对数据库的写操作,那么就会保存到数据库。

在下面的实例中就会出现持久化:

在900s(15min)后,至少有1个键值发生变化

在300s(5min)后,至少修改了10个键值发生了变化

在60s(1min)后,至少修改了10000个键值发生了变化

注意:你可以注释掉所有的save命令来禁用save

也可以通过配置带有单个空字符串参数的save命令来删除所有先前配置的save指令

如下所示:

save ""

save 900 1
save 300 10
save 60 10000

默认情况下,如果启用rdb快照保存失败,那么Redis将停止接受写入

这就会使用户能够意识到数据不能正确地持久化存储在磁盘上,否则很可能会没人注意到并发生一些灾难

如果后台保存过程将再次开始工作,那么Redis就会自动允许再次写入

当rdb出现问题时,是否依然继续进行工作,yes:不能进行工作,no:可以进行工作

# 如果配成no的话,表示你不在乎数据的不一致或者有别的办法来控制或者维护

stop-writes-on-bgsave-error yes

对于存储到磁盘中的快照,你可以设置是和否进行压缩存储。如果是的话,Redis会使用LZF压缩算法进行压缩,

如果不想消耗CPU来进行压缩的话,可以关闭此功能。其实影响不大。

rdbcompression yes

在存储和加载rdb文件的时候是否使用CRC64校验和来进行校验,如果开启,将消耗一定的性能,保持默认设置即可。

在禁用校验和的情况下创建rdb文件,校验和为0,这将指示加载代码跳过校验。

rdbchecksum yes

rdb的文件的名称

dbfilename dump.rdb

工作目录:

在哪个目录下启动Redis,那么这个路径就是工作目录,那么redis的日志就是生成在这个目录下

数据目录,数据库的写入就是在这个目录下。aof和rdb文件也会写入这个文件夹中。

请注意,您必须在此指定路径而不是文件名。

# 可以使用config get dir命令来获取工作目录。

dir ./_

如何触发RDB快照:

1.配置文件中的默认快照设置(冷拷贝之后重新使用 copy dump.rdb dump_new.rdb,最好主机和备份机是两台服务器)

2.命令save和bgsave都可以立即生成dump.rdb文件然后之前的旧的rdb文件

  save:save只管保存,即当之前save命令的时候,就无法存储数据了

  bgsave:Redis会在后台异步进行快照操作。

  执行flushall命令也会立即生成dump.rdb文件但是此文件为空。

如何恢复Redis中的数据:

  将备份文件复制到Redis的暗安装目录下,然后重新启动服务。

RDB的优势:
  适合大数据内容的存储和恢复

  相较于AOF,RDB更适合大数据集的恢复

RDB的劣势:

  容易丢失最后一次数据的快照

  在fork一个子进程的时候,接下来的工作全部由子进程来操作,父进程不进行任何IO操作,所以内存中的数据被克隆了一份,内存的膨胀需要考虑。

如何停止:

  动态停止所有保存rdb保存规则的方法:redis-cli config save ""

SECUTITY部分:

################################## SECURITY ###################################

# 需要用户在执行任何命令之前先输入AUTH

为了保持向后兼容,应该注释掉该命令,因为大部分的用户也不要认证。

警告:在使用requirepass的时候,由于redis实在是太快了,所以因为设置一个更加安全的密码

requirepass foobared

命令重命名。

可以在共享环境中更改危险命令的名称。 例如,可以将CONFIG命令重命名为一些难以猜测的名称,以便它仍可用于内部使用的工具,但不适用于一般客户。

例如:

rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52

通过将命令重命名为空字符串也可以完全取消该命令:

rename-command CONFIG ""

请注意,更改登录到AOF文件或传输到副本的命令的名称可能会导致问题。

secutity部分是全部被注释掉的,所以Redis默认是不需要输入密码的,因为Redis是在Linux环境下的服务器中运行,那么安全要求肯定是很高的。

Clients部分:

################################### CLIENTS ####################################

设置同时连接的最大客户端数量。默认情况下,此限制设置为10000个客户端连接,

但是,如果Redis服务器无法配置进程文件限制以允许指定的限制,则允许的最大客户端数将设置为当前文件限制减去32(因为Redis保留了 内部使用的文件描述符很少)

# 一旦达到限制,Redis将关闭所有新连接,并发送错误消息“已达到最大客户端数”。
# maxclients 10000

内存管理MEMORY MANAGEMENT部分:

############################## MEMORY MANAGEMENT ################################

# 设置能连上redis的最大客户端连接数量。默认是10000个客户端连接。由于redis不区分连接是客户端连接还是内部打开文件或者和slave连接等,所以maxclients最小建议设置到      # 32。如果超过了maxclients,redis会给新的连接发送’max number of clients reached’,并关闭连接。

# 当达到内存限制时,Redis将尝试根据所选的逐出策略(请参见maxmemory-policy)删除key。

当Redis无法根据策略删除key时,或者如果策略被设置为“noeviction”时,Redis会对set、push这些指令返回错误消息,而对get之类的指定继续回复

_# 当将Redis用作LRU或LFU缓存,或为实例设置硬盘限制(使用“ noeviction”策略)时,此选项通常很有用。

注意slave的输出缓冲区是不计算在maxmemory内的。所以为了防止主机内存使用完,建议设置的maxmemory需要更小一些。

maxmemory

MAXMEMORY POLICY: 当内存容量超过设置的maxmemory后的处理策略:

volatile-lru -> 利用LRU算法移除设置过过期时间的key

allkeys-lru -> Evict any key using approximated LRU.

volatile-lfu -> Evict using approximated LFU among the keys with an expire set.

allkeys-lfu -> Evict any key using approximated LFU.

volatile-random -> 随机移除设置过过期时间的key。

allkeys-random -> Remove a random key, any key.

volatile-ttl -> Remove the key with the nearest expire time (minor TTL)

noeviction -> Don't evict anything, just return an error on write operations.

LRU means Least Recently Used

LFU means Least Frequently Used

Both LRU, LFU and volatile-ttl are implemented using approximated

randomized algorithms.

Note: with any of the above policies, Redis will return an error on write

operations, when there are no suitable keys for eviction.

At the date of writing these commands are: set setnx setex append

incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd

sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby

zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby

getset mset msetnx exec sort

The default is:

maxmemory-policy noeviction

LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated

algorithms (in order to save memory), so you can tune it for speed or

accuracy. For default Redis will check five keys and pick the one that was

used less recently, you can change the sample size using the following

configuration directive.

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

Starting from Redis 5, by default a replica will ignore its maxmemory setting

(unless it is promoted to master after a failover or manually). It means

that the eviction of keys will be just handled by the master, sending the

DEL commands to the replica as keys evict in the master side.

This behavior ensures that masters and replicas stay consistent, and is usually

what you want, however if your replica is writable, or you want the replica to have

a different memory setting, and you are sure all the writes performed to the

replica are idempotent, then you may change this default (but be sure to understand

what you are doing).

Note that since the replica by default does not evict, it may end using more

memory than the one set via maxmemory (there are certain buffers that may

be larger on the replica, or data structures may sometimes take more memory and so

forth). So make sure you monitor your replicas and make sure they have enough

memory to never hit a real out-of-memory condition before the master hits

the configured maxmemory setting.

replica-ignore-maxmemory yes_

APPEND ONLY MODE(Redis持久化之aof)

############################## APPEND ONLY MODE ###############################

默认情况下,redis异步将数据写入磁盘中。此模式在许多应用中已经足够好了,但是redis进程问题或者电源中断可能会导致

几分钟的写入丢失(取决于配置的保存点)

可以同时启用aof和rdb,并且redis先加载aof

aof是默认关闭的,需要手动开启

appendonly no

aof文件的名称为appendonly.aof

appendfilename "appendonly.aof"

Redis支持三种模式:

appendfsync always: 持续持久化,每次数据发生变更就会记录到磁盘中,数据完整性较好但性能较差

appendfsync everysec: 出厂默认推荐,每秒记录一次,如果一秒内出现宕机,有数据丢失

appendfsync no :从不同步

如果不确定,就是用默认出厂推荐everysec

appendfsync always

appendfsync everysec

appendfsync no

重写数据的时候能否使用appendfsync对aof文件进行数据的追加。

默认使用no,保证数据的一致性。

no-appendfsync-on-rewrite no

自动重写aof文件

当AOF文件大小增加指定百分比时,Redis会自动调用BGREWRITEAOF 进行重写

触发机制:Redis会记住最近一次重写后文件的大小(如果自重新启动以来,没有发生过重写,那么就是用启动时aof文件的大小)

将当前文件的大小和之间记录的文件的大小进行比较,如果超过了指定的百分比就会进行重写。另外,需要指定文件的最小大小,即使超过了指定的百分比,但是没有超过最小的文件大小,也是不会重写的。

这对于重写aof文件非常有用。

# 100%就是一倍

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

If aof-load-truncated is set to yes, a truncated AOF file is loaded and

the Redis server starts emitting a log to inform the user of the event.

Otherwise if the option is set to no, the server aborts with an error

and refuses to start. When the option is set to no, the user requires

to fix the AOF file using the "redis-check-aof" utility before to restart

the server.

Note that if the AOF file will be found to be corrupted in the middle

the server will still exit with an error. This option only applies when

Redis will try to read more data from the AOF file but not enough bytes

will be found.

aof-load-truncated yes

When rewriting the AOF file, Redis is able to use an RDB preamble in the

AOF file for faster rewrites and recoveries. When this option is turned

on the rewritten AOF file is composed of two different stanzas:

[RDB file][AOF tail]

When loading Redis recognizes that the AOF file starts with the "REDIS"

string and loads the prefixed RDB file, and continues loading the AOF

tail.

aof-use-rdb-preamble yes

  相同数据集的数据要远大于rdb文件,恢复速度较慢,所以导致aof的运行效率较慢。

AOF总结:

  1.RDB持久化方式能够在指定的时间间隔内对数据进行快照存储

  2.AOF持久化操作记录每次对数据的写的操作,当服务器重启的时候就会重新执行这些命令来恢复原始的数据。AOF命令还能以Redis追加协议追加保存每次写的操作到文件的末尾。

  3.当两种持久化方式都出现的时候,先加载aof文件恢复原始数据。因为AOF文件保存的数据要比RDB文件来的完整。建议不要只使用AOF

  4.性能推荐:

    因为RDB只用作后备用途,建议只在Slave上保存RDB文件,而且只要15分钟备份一次就够了。只保留save 900 1这条命令就够了。

    如果Enable AOF,好处是在最坏的情况下不会丢失超过两秒的数据,启动脚本比较简单,只要load自己的aof文件就行了。代价一是带来了持续的IO,二是AOF rewrite的最后将rewrite过程中产生的新数据写入到新文件中造成的阻塞几乎是不可避免的。只要硬盘许可,应该尽量减少AOF rewrite的频率,AOF重写的基础大小是64M,太小了,可以设置到5G以上。默认超过原大小的100%时就会触发重写机制。

    如果不Enable AOF,只靠Master-slave Replication实现高可用性也可以。能省掉一大笔IO也减少了rewrite时带来的系统波动。代价是如果Master Slave同时倒掉,会丢失十几分钟的数据;载入脚本也要比较Master和Slave的RDB文件,加载较新的那个。