Redis 配置文件
启动时,Redis通过配置文件来启动!
配置文件单位
(单元,unit)大小写不敏感:
# 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 ###################################
# 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 c:\path\to\other.conf
网络
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
bind 127.0.0.1 #绑定的IP
protected-mode yes #保护模式,默认开启
port 6379 #端口号
通用配置
daemonize yes #以守护进程方式运行,默认为no,需自己开启为yes
pidfile /var/run/redis.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
logfile "" #日志的文件位置
databases 16 #默认的数据库数量16
快照snapshot
持久化,在规定的时间内,执行了多少次操作,则会持久化到 .rdb .aof 文件
Redis是内存数据库,易丢失,需要做持久化
#900秒内,至少进行了一次修改,则进行持久化操作
save 900 1
#300秒内,至少进行了10次修改,则进行持久化操作
save 300 10
#60秒内,至少进行了10000次修改,则进行持久化操作
save 60 10000
stop-writes-on-bgsave-error yes #持久化出错,是否还需要继续操作,默认yes
rdbcompression yes #是否压缩rdb文件,压缩会消耗cpu资源
rdbchecksum yes #保存rdb文件时,进行错误的校验
dir ./ #rdb文件保存的目录
REPLICATION 主从复制
SECURITY 安全
requirepass foobared #通过修改配置文件设置密码
config set requirepass 123456 #也可通过命令来设置密码
config set requirepass #发现所有的命令都没有权限了
auth 123456 #用密码进行验证,之后会获得权限
LIMITS / Clients
maxclients 10000 #设置能连接上redis的最大客户端的数量
maxmemory <bytes> #配置Redis的最大内存容量
maxmemory-policy noeviction #内存达到上限之后的处理策略
redis提供了以下6种淘汰策略:
1,noeviction:不执行任何淘汰策略,当达到内存限制的时候客户端执行命令会报错。
2,allkeys-lru:从所有数据范围内查找到最近最少使用的数据进行淘汰,直到有足够的内存来存放新数据。
3,volatile-lru:从所有的最近最少访问数据范围内查找设置到过期时间的数据进行淘汰,如果查找不到数据,则回退到noeviction。
4,allkeys-random:从所有数据范围内随机选择key进行删除。
5,volatile-random:从设置了过期时间的数据范围内随机选择key进行删除。
6,volatile-ttl:从设置了过期时间的数据范围内优先选择设置了TTL的key进行删除。
APPEND ONLY 模式 aof配置
appendonly no #默认是不开启aof模式的,默认是使用rdb方式持久化的,大部分情况下,rdb完全够用
appendfilename "appendonly.aof" #持久化文件的名字
# appendfsync always #每次修改都会同步,速度较慢
appendfsync everysec #每秒执行一次同步,,可能会丢失这一秒的数据
# appendfsync no #不执行同步,这个时候操作系统自己同步数据,速度最快