塞個資料,沒想到 Redis 一直炸,看 log 才發現有設定要調整:
356416:C 15 Feb 2022 14:53:43.009 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 356416:C 15 Feb 2022 14:53:43.009 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=356416, just started 356416:C 15 Feb 2022 14:53:43.009 # Configuration loaded 356416:M 15 Feb 2022 14:53:43.010 * monotonic clock: POSIX clock_gettime 356416:M 15 Feb 2022 14:53:43.012 * Running mode=standalone, port=6379. 356416:M 15 Feb 2022 14:53:43.012 # Server initialized 356416:M 15 Feb 2022 14:53:43.012 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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. 356416:M 15 Feb 2022 14:53:43.017 * Loading RDB produced by version 6.2.6 356416:M 15 Feb 2022 14:53:43.017 * RDB age 1475 seconds 356416:M 15 Feb 2022 14:53:43.017 * RDB memory usage when created 5243.48 Mb 356416:signal-handler (1644908113) Received shutdown signal during loading, exiting now. 356427:C 15 Feb 2022 14:55:14.014 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
注意到錯誤訊息中間有個關鍵字 vm.overcommit_memory,如果遇到 sysctl 或 vm 相關的,大多是與 Linux 系統參數有關係,這邊有 overcommit_memory 的設定說明,該設定會保護系統不會接受大型記憶體 allocate 的請求。
但 Redis 本來就是使用記憶體做服務,因此需要去 /etc/sysctl.conf 新增下面這段設定:
vm.overcommit_memory = 1
重新開機後即可套用設定。
另外 Redis 會在機器啟動時,從硬碟將 persistent data 載入記憶體中,若資料不少,載入會花掉相當的時間,因此 systemd 預設的設定,可能會誤以為 Redis 啟動失敗:
redis-server.service: start operation timed out. Terminating.
依據資料多寡,會影響 Redis 啟動的時間,所以需要調整 systemd 的 timeout。
開啟 /etc/systemd/system/redis.service 注意下面幾個 timeout 相關的參數:
TimeoutStartSec=300 TimeoutStopSec=300
參考 systemd 的文件,TimeoutStartSec=300
也可以改成可讀格式 TimeoutStartSec="5min"
。
2022-02-18 更新:
感謝 JoeHorn 提醒,直接修改 /etc/systemd/
底下的設定檔是有風險的,建議透過 systemctl edit xxx.service
的方式 override 預設參數值。