1 单位
# 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.
redis 的单位不区分大小写
2 包含(配置文件的引用)
################################## 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.
#
# Notice 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
表示可以使用多个配置文件,类似于 Java 中的 import java.util.List 等。
3 网络(IP、端口)
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
...
...
bind 127.0.0.1
protected-mode yes
port 6379
- bind 绑定的 IP,默认是本地,可以指定 IP,表示只有指定的 IP 才可访问,注释掉的话则全部 IP 都可访问。
- protected-mode 保护模式,默认开启
- port 端口
4 通用配置(启动方式、日志等)
################################ GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
...
supervised no
...
pidfile /var/run/redis_6379.pid
...
# 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
...
logfile
...
databases 16
...
always-show-logo yes
- daemonize 表示以守护进程的方式运行,默认 no,需要改为 yes,避免关闭客户端后,redis 也跟着关闭。
- pidfile 表示如果以后台方式运行,则需要指定 pid 文件。
- loglevel 日志级别,debug 用户测试和开发阶段;notice 重要信息日志,生产环境使用。
- logfile 日志文件路径。
databases 数据库数量,默认 16 个。
always-show-logo 表示启动时是否显示 logo。
5 快照(持久化)
redis 是内存数据库,断电或宕机重启导致数据丢失,因此需要持久化。持久化的文件为 xxx.rdb、xxx.aof。
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
...
save 900 1
save 300 10
save 60 10000
...
stop-writes-on-bgsave-error yes
...
rdbcompression yes
...
rdbchecksum yes
...
dir ./
- save 900 1 表示如果在 900 秒内,至少有 1 个 key 进行了修改,则进行持久化操作。
- save 300 10 表示如果在 300秒内,至少有 10个 key 进行了修改,则进行持久化操作。
- stop-writes-on-bgsave-error 表示持久化出错时,是否继续作业
- rdbcompression 表示是否压缩 rdb 文件,需要消耗一些 cpu 资源
- rdbchecksum 表示保存 rdb 文件时,是否进行校验
- dir 表示 rdb 文件的保存目录
6 主从复制
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
...
# replicaof <masterip> <masterport>
#
# masterauth <master-password>
redis 默认为 master,因此需要配置主从复制的话,只需要将以下信息修改即可,修改完后则变为了从机(slave)
- relicaof :为 master 的 ip 和端口。
- masterauth : 为 master 的密码。
7 安全(密码)
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
- requirepass 密码,默认没有密码,如果需要则设置密码
[root@VM-0-4-centos ~]# redis-cli -p 6379 # 获取密码 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" # 设置密码 >127.0.0.1:6379> config set requirepass 12455 OK # 重新关闭客户端再进入时,输入一下命令,则显示无权限 127.0.0.1:6379> keys * (error) NOAUTH Authentication required. # 输入密码 127.0.0.1:6379> auth 12455 OK
8 客户端
################################### CLIENTS ####################################
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 10000
maxclients 表示最大客户端数量
9 内存
############################## MEMORY MANAGEMENT ################################
# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
...
# maxmemory <bytes>
...
# maxmemory-policy noeviction
- maxmemory 表示最大的内存容量,默认字节
- maxmemory-policy 表示内存达到设置上限后的处理策略,如下:
- volatile-lru 利用 LRU 算法移除设置过过期时间的 key (LRU:最近使用 Least Recently Used ) 。
- allkeys-lru 利用 LRU 算法移除任何 key (和上一个相比,删除的 key 包括设置过期时间和不设置过期时间的)。通常使用该方式。
- volatile-random 移除设置过过期时间的随机 key 。
- allkeys-random 无差别的随机移除。
- volatile-ttl 移除即将过期的 key(minor TTL)
- noeviction 不移除任何 key,只是返回一个写错误 ,默认选项,一般不会选用。
10 AOF 持久化配置
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
...
appendonly no
...
appendfilename "appendonly.aof"
...
# appendfsync always
appendfsync everysec
# appendfsync no
- appendonly 表示启动 AOF 模式,默认使用 rdb 方式持久化,大部分情况,rdb 够用。
- appendfilename 表示持久化文件名。
- appendfsync 表示持久化频率:
- always 表示每次修改都会持久化一次,消耗性能。
- everysec 表示每秒持久化一次,可能丢失持久化这 1 秒的数据。
- no 表示不执行。