2023-03-24

Redis配置详解

获取配置文件

docker配置文件需要下载至对应位置

1
2
3
cd /opt/redis //进入目录 
wget http://download.redis.io/redis-stable/redis.conf //下载redis配置文件
vim redis.conf //修改配置文件

单位

  • 对大小写不敏感
1
2
3
4
5
6
7
8
9
10
11
# 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.

包含

  • 可以包含其他配置文件
1
2
3
4
5
#
# include /path/to/local.conf
# include /path/to/other.conf
# include /path/to/fragments/*.conf
#

网络

1
2
3
bind 127.0.0.1 -::1   # 绑定的ip
protected-mode yes # 保护模式
port 6379 # 端口

通用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
daemonize no     # 以守护进程的方式运行,默认no
pidfile /var/run/redis_6379.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 # 数据库数量
always-show-logo no # logo展示

快照

  • 持久化,在规定时间内执行了多少次则会持久化到文件
  • redis是内存数据库,如果没有持久化,断电丢失
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Unless specified otherwise, by default Redis will save the DB:
# * After 3600 seconds (an hour) if at least 1 change was performed
# * After 300 seconds (5 minutes) if at least 100 changes were performed
# * After 60 seconds if at least 10000 changes were performed
#
# You can set these explicitly by uncommenting the following line.
#
# save 3600 1 300 100 60 10000

# 3600s内如果至少有一个key进行了修改则进行持久化操作
# 300s内如果至少有100个key进行了修改则进行持久化操作
# 60s内如果至少有10000个key进行了修改则进行持久化操作

stop-writes-on-bgsave-error yes # 持久化出错是否继续进行
rdbcompression yes # 是否压缩rdb文件
rdbchecksum yes # 是否检验检查
dir ./ # 保存文件位置

REPLICATION 主从复制

1
2
3
# replicaof <masterip> <masterport>    # 配置主机ip端口
# masterauth <master-password> # 配置主机密码
replica-read-only yes # 从机只读

安全

1
2
3
4
5
6
7
8
9
# requirepass foobared    # 密码设置

# 一般用命令行设置

config get requirepass

config set requirepass "111111"

auth 111111

限制

1
2
3
4
5
6
7
8
9
10
# maxclients 10000   # 链接最大客户端数量
# maxmemory <bytes> # 最大内存容量
# maxmemory-policy noeviction # 内存满了后的处理策略
# 六种方式:
volatile-lru:只对设置了过期时间的key进行LRU(默认值)
allkeys-lru : 删除lru算法的key
volatile-random:随机删除即将过期key
allkeys-random:随机删除
volatile-ttl : 删除即将过期的
noeviction : 永不过期,返回错误

AOF模式

1
2
3
4
5
6
appendonly no  # 默认不开启,默认使用rdb进行持久化
appendfilename "appendonly.aof" # 文件名

# appendfsync always # 每次修改就会同步,消耗性能
appendfsync everysec # 每秒执行一次sync,可能会丢失这1s的数据
# appendfsync no # 不执行同步,交给操作系统会自动同步