《Redis 常见问题排查》

《Redis 常见问题排查》

本文总结 Redis 运维中常见的高频问题与规避方式,包括 socket 文件、配置加载、Lua 连接、配置报错、主从断连等。

1 不要自己创建 socket 文件

/var/run/redis/redis.sock 这类 socket 文件是 Redis 启动时自动生成的,类型为 socket。不要手动创建,否则会与启动逻辑冲突,导致连接异常。

2 redis-server 启动时一定要指定配置文件

redis-server /etc/redis/redis.conf

默认不一定从 /etc/redis/redis.conf 加载配置,尤其是设置了自定义端口、socket、密码时,务必通过参数显式指定。

3 Lua 中 connect 必须使用 Redis 创建的 socket 文件

先用 netstat 确认 Redis 实际监听哪个 socket 文件:

netstat -anp | grep LISTEN | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 39757/redis-server unix 2 [ ACC ] STREAM LISTENING 1370670491 39757/redis-server /var/run/redis/redis.sock

Lua 脚本中连接时必须使用该 socket:

-- 注意:冒号之后是单个 / ,不是两个 local redis = require 'resty.redis' local red = redis:new() local ok, err = red:connect('unix:/var/run/redis/redis.sock')

4 Bad directive or wrong number of arguments

启动时报错:

*** FATAL CONFIG FILE ERROR *** Reading the configuration file, at line 80 >>> 'protected-mode yes' Bad directive or wrong number of arguments

原因:Redis 版本太低,不认识 protected-mode 等新指令。

解决办法:升级 Redis 版本,或先去掉该指令再启动。

5 Connection with master lost.

查看从实例日志:

1:S ... * MASTER <-> REPLICA sync: receiving 77178672 bytes from master 1:S ... # Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in redis.conf to a larger value.

原因:主从全量同步数据量大时超时,通常与 client-output-buffer-limit 和 repl-timeout 两个参数有关。

排查(通过 socket 读取当前值):

redis-cli -s /var/run/redis/redis.sock config get repl-timeout # 1) "repl-timeout" # 2) "60" redis-cli -s /var/run/redis/redis.sock config get client-output-buffer-limit # 1) "client-output-buffer-limit" # 2) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"

解决办法:

  • repl-timeout 默认 60 秒,可调大。主、从两端都要改。
  • 从库输出缓冲区可调大,或干脆设置为无限:
client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60
  • 也可以在线调整(例如 buffer 上限调到 512M,hard/soft 都为 0 表示不限制):
redis-cli -s /var/run/redis/redis.sock config set client-output-buffer-limit "slave 536870912 536870912 0" redis-cli -s /var/run/redis/redis.sock config get client-output-buffer-limit

4.1 通过 info replication 判断同步状态

redis-cli -s /var/run/redis/redis.sock info replication
# Replication role:slave master_host:192.168.1.10 master_port:6379 # 判断 slave 和 master 之间的同步状态:up 正常;down 异常 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:4009849715 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:d5c2f031fa2cf16f876c1b57b7d2a4e2a5f34f2a master_repl_offset:4009849715 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:4009732275 repl_backlog_histlen:117441

重点看 master_link_status:up 表示正常,down 表示同步异常。

阅读 — · 全站 —
🎸 我的歌单 0 首