Redis.conf 详解

启动的时候,就通过配置文件来启动;

单位

image-20220721132156605

1、配置文件 unit 单位对大小写不敏感

包含

image-20220721132424961

就比如 Spring 中的 Import、Include

网络
bind 127.0.0.1  # 绑定的 ip
protected-mode yes    # 保护模式
port 6379    # 端口设置
通用 GENERAL
daemonize yes    # 以守护进程的方式运行,默认是no 我们需要手动开启为 yes    windows 中不支持

pidfile /var/run/redis.pid    # 如果以后台的方式运行,我们就需要指定一个 pid 文件    windows 中不支持

# 日志
# 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 个

always-show-logo yes    # 是否总是显示 LOGO
快照 SNAPSHOTTING

持久化:在规定时间内,执行了多少次操作,则会持久化到文件 .rdb .aof

redis 是内存数据库,如果没有持久化,那么数据断电即失。

# 如果 900s 内,如果至少有一个 key 进行了修改,我们就进行持久化操作
save 900 1
# 如果 300s 内,如果至少有 10 个 key 进行了修改,我们就进行持久化操作
save 300 10
# 如果 60s 内,如果至少有 10000 个 key 进行了修改,我们就进行持久化操作
save 60 10000
# 我们之后学习持久化,会自己定义这个测试

stop-writes-on-bgsave-error yes    # 持久化如果出错,是否还需要继续工作

rdbcompression yes    # 是否压缩 rdb 文件,需要消耗一些 cpu 资源

rdbchecksum yes    # 保存 rdb 文件的时候 是否进行错误的检查校验

dir ./    # 默认保存的目录

dbfilename dump.rdb
REPLICATION 复制
SECURITY 安全

可以在这里设置 redis 的密码,默认是没有密码的。

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> CONFIG GET requirepass    # 获取 redis 的密码
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass root    # 设置 redis 的密码
OK
127.0.0.1:6379> CONFIG GET requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth root    # 使用密码进行登录
OK
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "root"
限制 CLIENTS
maxclients 10000    # 设置能连接上 reids 的最大客户端的数量

maxmemory <bytes>   # redis 配置最大的内存容量

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 配置
appendonly no    # 默认是不开启 aof 模式,默认是使用 rdb 方式持久化    在大部分所有的情况下,rdb 完全够用

appendfilename "appendonly.aof"    # 持久化文件的名字

# appendfsync always    # 每次修改都会 sync。消耗性能
appendfsync everysec    # 每秒执行一次 sync同步,可能会丢失这1s的数据
# appendfsync no    # 不执行 sync,这个时候才做系统自己同步数据,速度最快,但一般不用