Redis配置文件相当重要,里面涉及到Redis使用的各种条件配置,了解配置文件对于学习Redis至关重要。
在配置文件目录下打开终端,输入命令:sudo vim redis.conf,便可以看到配置文件内容,可以根据需求对配置文件进行修改。
1.units 单位,对大小写不敏感
2.可以包含其他文件,设置路径。
3.网络配置
bind 127.0.0.1 #绑定的Ip
protected-mode yes #受保护的模式
port 6379 #连接的端口
4.通用配置
daemonize yes #是否以守护进程开启,后台运行,默认是no,需要修改成yes
pidfile /var/run/redis_6379.pid #如果以后台方式运行,需要指定一个pid文件
5.日志
# 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 /usr/local/bin/redis-log.log #日志文件
databases 16 #默认16个数据库
6.快照(持久化)
# Save the DB to disk.
# save <seconds> <changes>
Redis will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
# 持久化,在规定时间内执行操作多少次,则会持久化到硬盘(.rdb .aof)。
# Redis是内存数据库,如果没有持久化,那么断电即失去
# Unless specified otherwise, by default Redis will save the DB:
# * After 3600 seconds (an hour) if at least 1 key changed
#如果1小时内,至少有一个key进行了修改,进行持久化操作
# * After 300 seconds (5 minutes) if at least 100 keys changed
#5分钟内有超过100次key修改
# * After 60 seconds if at least 10000 keys changed
#60秒内,至少10000次key修改
#可以根据需求设置自己的时间
stop-writes-on-bgsave-error yes #持久化如果错误,是否还要继续工作 ,默认yes
rdbcompression yes #是否压缩RDB文件,需要消耗一些CPU资源
rdbchecksum yes #保存RDB文件时,进行错误的检查校验
dbfilename dump.rdb #RDB文件名称
dir ./ #RDB文件所在目录,当前目录下
6.复制(主从复制在后续博客中单独详细介绍)
7.安全
可以设置密码
config get requirepass #获取密码
config set requirespass ****** #设置密码
auth ****** #登录验证
8.客户端
# maxclients 10000 #最大客户端连接数
9.内存
maxmemory<bytes>#redis配置的最大的内存容量
maxmemory-policy noeviction #内存满了后Redis的处理策略
1、volatile-lru:只对设置了过期时间的key进行LRU(默认值)
2、allkeys-lru : 删除lru算法的key
3、volatile-random:随机删除即将过期key
4、allkeys-random:随机删除
5、volatile-ttl : 删除即将过期的
6、noeviction : 永不过期,返回错误
10.APPEND ONLY模式 aof配置
appendonly no #默认是不开启aof模式的,默认是使用RDB方式持久化的,在大部分所有的情况下,RDB完全够用
appendfilename "appendonly.aof" #持久化的文件的名字
# appendfsync always ##每次修改都会sync(同步)消耗性能
appendfsync everysec #每秒执行一次sync,可能会丢失者1s的数据
# appendfsync no #不执行sync,这个时候操作系统自己同步数据,速度最快