Redis配置文件详解
大约 3 分钟数据库技术Redis
启动的时候,就通过配置文件来启动。
单位

配置文件 unit单位 对大小写不敏感。
包含

就是好比我们学习Spring的import标签。
网络
# 绑定IP地址
bind 127.0.0.1
# 保护模式
protected-mode yes
# 端口设置
port 6379
GENERAL
# 以守护进程的方式进行。默认是no。我们需要自己开启为yes
daemonize yes
# 如果以后台的方式运行,我们就需要指定一个pid文件
pidfile "/var/run/redis_6379.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
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "" # 日志的文件位置名
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16 # 数据库数量,默认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 # 是否总是显示logo
SNAPSHOTTING 快照
持久化,在规定的时间内,执行了多少次操作,则会持久化到文件 .rdb .aof。
# 900s内,至少有一个key进行了修改,就进行持久化操作
save 900 1
# 300s内,至少有10个key进行了修改,就进行持久化操作
save 300 10
# 60s内,至少有10000个key进行了修改,就进行持久化操作
save 60 10000
# 我们之后学习持久化,会自己设置
# 持久化出错,是否还需要继续工作
stop-writes-on-bgsave-error yes
# 是否压缩rdb文件,需要消耗CPU资源
rdbcompression yes
# 保存rdb文件的时候,进行检查校验
rdbchecksum yes
# The filename where to dump the DB
dbfilename "dump.rdb"
# rdb文件保存的目录
dir "/usr/local/bin"
REPLICATION 复制
SECURITY 安全
Redis密码设置
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass ****** # 设置密码
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "******"
127.0.0.1:6379> config rewrite # 将连接密码写进当前使用的redis.conf。不然下一次重启会报错
OK
CLIENTS 限制
# 设置能连接上redis客户端的最大连接数
maxclients 10000
MEMORY MANAGEMENT
# redis配置最大的内存容量
maxmemory <bytes>
# 内存到达上限之后的处理策略:移除一些过期的key、报错
maxmemory-policy noeviction
# 1、volatile-lru:只对设置了过期时间的key进行LRU(默认值)
# 2、allkeys-lru : 删除lru算法的key
# 3、volatile-random:随机删除即将过期key
# 4、allkeys-random:随机删除
# 5、volatile-ttl : 删除即将过期的
# 6、noeviction : 永不过期,返回错误
APPEND ONLY MODE 模式 AOF配置
# 默认不开启AOF模式。默认是使用RDB方式持久化的。在大部分所有的情况下,RDB完全够用
appendonly no
# 持久化的文件名
appendfilename "appendonly.aof"
# appendfsync always # 每次修改都会 sync。消耗性能
appendfsync everysec # 每秒执行一次 sync,可能会丢失这1秒的数据
# appendfsync no # 不执行 sync,这个时候操作系统自己同步数据,速度最快