生生不息

生生不息

Redis报错Background saving error

2025-04-18
Redis报错Background saving error

背景

最近使用docker安装应用,使用内置的redis,是docker启动的容器。不一会容器暂停了,参看日志是因为redis没有写入目录的权限,写入db没有权限。

解决方法

查看redis容器挂载的目录为
/www/wwwroot/xxx.com/.docker/.data/redis

ls -lth /www/wwwroot/xxx.com/.docker/.data/redis

输出如下:

srwxrwxrwx 1 www www 0 Apr 17 01:52 redis.sock

给予权限

chmod 2770 /www/wwwroot/xxx.com/.docker/.data/redis  # 设置setgid位继承组权限,防止新生成文件权限又被修改为www
chown -R 999:999 /www/wwwroot/xxx.com/.docker/.data/redis

查看

ls -lth /www/wwwroot/xxx.com/.docker/.data/redis

输出如下:

srwxrwxrwx 1 999 systemd-journal 0 Apr 17 01:52 redis.sock

重新看redis容器日志

1:M 17 Apr 2025 09:29:48.164 # Background saving error
1:M 17 Apr 2025 09:29:54.025 * 1 changes in 900 seconds. Saving...
1:M 17 Apr 2025 09:29:54.026 * Background saving started by pid 342
342:C 17 Apr 2025 09:29:54.027 # Failed opening the temp RDB file temp-342.rdb (in server root dir /data) for saving: Permission denied
1:M 17 Apr 2025 09:29:54.128 # Background saving error
1:M 17 Apr 2025 09:30:00.097 * 1 changes in 900 seconds. Saving...
1:M 17 Apr 2025 09:30:00.098 * Background saving started by pid 343
343:C 17 Apr 2025 09:30:00.098 # Failed opening the temp RDB file temp-343.rdb (in server root dir /data) for saving: Permission denied
1:M 17 Apr 2025 09:30:00.199 # Background saving error
1:M 17 Apr 2025 09:30:06.071 * 1 changes in 900 seconds. Saving...
1:M 17 Apr 2025 09:30:06.072 * Background saving started by pid 344
344:C 17 Apr 2025 09:30:06.089 * DB saved on disk
344:C 17 Apr 2025 09:30:06.091 * Fork CoW for RDB: current 0 MB, peak 0 MB, average 0 MB
1:M 17 Apr 2025 09:30:06.173 * Background saving terminated with success

成功写入了。依赖于redis的应用也正常了。