2MUCH

Redis-配置文件

2022-07-18


Redis-配置文件

路径:原先位于/opt/redis-6.2.1/redis.conf,后面我们是copy一份到etc/redis.conf作为redis后台启动的配置文件。

配置文件中每条配置都给了充足的注释或例子

以下按照课程介绍配置文件中一些常用的点:

单位

配置大小单位,开头定义了一些基本的度量单位。只支持bytes,不支持bit。大小写不敏感

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

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

网络相关配置

bind

bind 127.0.0.1 -::1

以上表示只能接受本机发出的请求,若要接收来自其他网络的请求,则要注释掉这行(不写的情况下,无限制接受任何ip地址的访问)

生产环境肯定要写你应用服务器的地址;服务器是需要远程访问的,所以需要将其注释掉。

如果开启了protected-mode,那么在没有设定bind ip且没有设密码的情况下,Redis只允许接受本机的响应。

protected-mode

protected-mode yes

只有将这个模式设为no,外部服务器才能访问

port

port 6379

端口号默认6379

tcp-backlog

tcp-backlog 511

设置tcp的backlog,backlog其实是一个连接队列,backlog队列总和=未完成三次握手队列 + 已经完成三次握手队列。在高并发环境下你需要一个高backlog值来避免慢客户端连接问题。

timeout

timeout 0

一个空闲的客户端维持多少秒会关闭,0表示关闭该功能。即永不关闭。

tcp-keepalive

tcp-keepalive 300

设置心跳检测的时间,单位为秒,上面表示每300s检测一次。设置为0表示不检测。

通用设置

守护进程

daemonize yes

是否为后台进程。设置为yes表示:守护进程,后台启动

pid文件

pidfile /var/run/redis_6379.pid

存放pid文件的位置,每个实例会产生一个不同的pid文件

日志级别

# Specify the server verbosity level.
# This can be one of:
# 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

生产环境建议选择notice 或者warning

logfile

logfile ""

日志文件存放路径,默认无

database

databases 16

数据库数量默认设置为16

安全方面

# requirepass foobared

是否需要密码的配置行,默认是不需要密码

注意:在redis命令行中设置密码,当redis服务器重启就还原了。所以要想持久设置,要写到配置文件中。

LIMITS限制

# maxclients 10000  

设置redis同时可以与多少个客户端进行连接

# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5