《Redis 配置文件详解》

《Redis 配置文件详解》

Redis 通过 redis.conf 控制运行方式。大多数配置可直接用 CONFIG GET/SET 查看与修改,持久化配置则在启动时从文件读取。本文梳理关键配置项,并给出一个含主从与 socket 的完整示例。

1 masterauth 与 requirepass

配置主从同步并使用密码时,master 的 requirepass 和 slave 的 masterauth 必须保持一致,否则同步失败。

# master 上设置访问密码(客户端连接时使用) requirepass mypassword # slave 上设置访问 master 的密码(与 master 的 requirepass 一致) masterauth mypassword

2 关键配置项解释

# 是否作为守护进程运行 daemonize yes # 以后台进程运行时指定 pid 文件,默认 /var/run/redis.pid pidfile redis.pid # 绑定主机 IP,默认 127.0.0.1;注释掉或设为 0.0.0.0 表示监听所有网卡 #bind 127.0.0.1 # Redis 默认监听端口 port 6379 # 客户端闲置多少秒后断开连接,默认 300 秒 timeout 300 # 日志级别:debug / verbose(默认)/ notice / warning loglevel verbose # 日志输出文件名,默认 stdout;设为 /dev/null 可屏蔽日志 logfile stdout # 可用数据库数,默认 16,默认数据库为 0 databases 16 # 保存数据到磁盘(RDB)的策略 # 900 秒内至少有 1 个 key 改变,触发一次保存 save 900 1 # 300 秒内至少有 10 个 key 改变,触发一次保存 save 300 10 # 60 秒内至少有 10000 个 key 改变,触发一次保存 save 60 10000 # dump .rdb 时是否压缩数据对象 rdbcompression yes # 本地数据库文件名,默认 dump.rdb dbfilename dump.rdb # 本地数据库存放路径,默认 ./ dir ./

3 一份含主从的实用配置

port 6379 unixsocket /var/run/redis/redis.sock pidfile /var/run/redis/redis.pid logfile "/var/log/redis/redis.log" dbfilename redis.rdb dir /var/run/redis/ # 如果为 no,表示在 slave 模式下也可以写入数据 slave-read-only yes daemonize yes loglevel notice databases 16 bind 0.0.0.0 protected-mode yes tcp-backlog 511 unixsocketperm 766 timeout 0 tcp-keepalive 300 supervised no save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes replica-serve-stale-data yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes

4 注意事项

  • 高版本将 slave-read-only、slave-serve-stale-data 改为 replica-read-only、replica-serve-stale-data,老配置仍兼容。
  • protected-mode yes 下未设密码会拒绝局域网访问,检测时注意。
  • 修改 bind/port/protected-mode 后需重启或 CONFIG REWRITE 才生效。
  • 主从或多实例部署时,pidfile、logfile、dbfilename、unixsocket 务必按实例区分,避免互相覆盖。
阅读 — · 全站 —
🎸 我的歌单 0 首