zhangshaohan
V2EX  ›  Redis

Redis 运行一段时间后报错导致挂掉,有什么办法解决呢?

  •  
  •   zhangshaohan · Mar 31, 2025 · 4361 views
    This topic created in 411 days ago, the information mentioned may be changed or developed.
    使用 docker 安装的 Redis ,版本 7.4.1 ,挂载 config 和 data 文件夹到宿主机,运行一段时间(一个月)后突然发现挂掉了,查看日志有如下信息,看上去像是 aof 文件损坏,如何避免这个问题呢?

    1:C 31 Mar 2025 10:15:53.909 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    1:C 31 Mar 2025 10:15:53.909 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 31 Mar 2025 10:15:53.909 * Redis version=7.4.1, bits=64, commit=00000000, modified=0, pid=1, just started
    1:C 31 Mar 2025 10:15:53.909 * Configuration loaded
    1:M 31 Mar 2025 10:15:53.909 * monotonic clock: POSIX clock_gettime
    1:M 31 Mar 2025 10:15:53.910 # Failed to write PID file: Permission denied
    1:M 31 Mar 2025 10:15:53.910 * Running mode=standalone, port=6379.
    1:M 31 Mar 2025 10:15:53.910 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1:M 31 Mar 2025 10:15:53.910 * Server initialized
    1:M 31 Mar 2025 10:15:53.910 * Reading RDB base file on AOF loading...
    1:M 31 Mar 2025 10:15:53.910 * Loading RDB produced by version 7.4.1
    1:M 31 Mar 2025 10:15:53.910 * RDB age 159448 seconds
    1:M 31 Mar 2025 10:15:53.910 * RDB memory usage when created 3.08 Mb
    1:M 31 Mar 2025 10:15:53.910 * RDB is base AOF
    1:M 31 Mar 2025 10:15:53.915 * Done loading RDB, keys loaded: 1103, keys expired: 0.
    1:M 31 Mar 2025 10:15:53.916 * DB loaded from base file appendonly.aof.12754.base.rdb: 0.006 seconds
    1:M 31 Mar 2025 10:15:54.042 # Bad file format reading the append only file appendonly.aof.12754.incr.aof: make a backup of your AOF file, then use ./redis-check-aof --fix <filename.manifest>
    Supplement 1  ·  Mar 31, 2025
    感谢大家,在大家的提示下,我找到了问题原因:

    Redis 依赖 Linux 的内存过量提交机制( Memory Overcommit ),但当前系统 未启用 该机制,导致:

    后台保存( BGSAVE )或 AOF 持久化 可能失败。

    主从复制 可能失败。

    即使内存充足,也可能出现 OOM ( Out of Memory )相关错误,导致 Redis 启动失败或进程崩溃。


    具体解决方案 gpt 有提供
    10 replies    2025-03-31 12:04:31 +08:00
    opengps
        1
    opengps  
       Mar 31, 2025
    这提示看起来很明白了
    fcten
        2
    fcten  
       Mar 31, 2025
    显示 aof 损坏是 redis 挂了的结果不是原因吧
    你找找挂掉时的日志,是不是 OOM 了
    zhangshaohan
        3
    zhangshaohan  
    OP
       Mar 31, 2025
    @fcten redis 也会 OOM 吗?是通过 docker logs 看日志吗?日志好像没持久化到宿主机
    freemoon
        4
    freemoon  
       Mar 31, 2025
    1. 你这台机运行了其他服务占用过多内存,把 redis 挤崩了。
    2. 文中提供的 issue 导致的。
    zhangshaohan
        5
    zhangshaohan  
    OP
       Mar 31, 2025
    @lasuar 这台服务器 128g 内存,总内存只用了十多 G 呢?
    zhangshaohan
        6
    zhangshaohan  
    OP
       Mar 31, 2025
    @fcten
    @lasuar
    @opengps 感谢,找到问题原因了
    SKYNE
        7
    SKYNE  
       Mar 31, 2025
    看起来是 redis 在持久化 AOF 时异常停掉了,导致的 AOF 文件不完整,启动时就是这样的报错,查一下系统 message 中有没有有用的信息
    zhangshaohan
        8
    zhangshaohan  
    OP
       Mar 31, 2025
    @SKYNE 我看报错的提示查了一下是因为操作系统没开启 Linux 的内存过量提交机制,导致 AOF 文件写失败
    SKYNE
        9
    SKYNE  
       Mar 31, 2025
    看来 redis 日志的 WARNING 还是非常有用的
    julyclyde
        10
    julyclyde  
       Mar 31, 2025
    overcommit 这一句是本轮启动开头的内容
    而 AOF 文件损坏其实是上一轮损坏的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2954 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 05:57 · PVG 13:57 · LAX 22:57 · JFK 01:57
    ♥ Do have faith in what you're doing.