redis啟動報錯Can‘t?open?the?log?file:?No?such?file?or?directory
問題描述
在使用docker-compose安裝redis的時候,啟動失敗,提示無法開發(fā)日志文件,這主要是容器中沒有對應(yīng)的日志文件造成的,另一點(diǎn)就是對應(yīng)的日志文件沒有相應(yīng)權(quán)限所導(dǎo)致
異常信息如下所示
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 103
>>> 'logfile /var/log/redis/redis.log'
Can't open the log file: No such file or directory
原因分析
這個提示里面的/var/log/redis/redis.log日志文件指的是容器中的文件,千萬別理解為宿主機(jī)中的路徑,如果你在宿主機(jī)創(chuàng)建此文件并授權(quán),最后結(jié)果也是一樣的,理解這一點(diǎn)很重要。
相關(guān)docker-compse.yml
配置如下
version: '3'
services:
redis:
hostname: redis
image: redis:latest
container_name: redis
restart: unless-stopped
command: redis-server /etc/redis.conf
environment:
- TZ=Asia/Shanghai # 時區(qū)設(shè)置
volumes:
- /etc/localtime:/etc/localtime:ro # 時區(qū)設(shè)置
- ./data:/data # redis 數(shù)據(jù)存儲目錄
- ./redis.conf:/etc/redis.conf # redis配置文件
ports:
- "6379:6379"
為了方面隨時修改redis配置,所以將配置文件進(jìn)行了映射
redis中日志文件的配置項如下:
# Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null logfile "/var/log/redis/redis.log"
啟動的時候就提示上述錯誤了,要解決這個問題有兩種方案
解決方案
方案一
直接將redis.conf中的logfile配置注釋掉或者設(shè)置為空就可以了,但是這樣就不會輸出日志了,如果有問題需要排查就不方面了。
方案二
在宿主機(jī)的./data目錄下創(chuàng)建redis.log文件并授予權(quán)限,當(dāng)前redis安裝的絕對路徑為/home/local/docker/redis
以下都是指的相對路徑
touch data/redis.log chmod 777 data/redis.log 修改日志相關(guān)配置,這樣容器中就會自動創(chuàng)建日志文件并授予權(quán)限了 logfile "/data/redis.log" 重新構(gòu)建并查看日志應(yīng)該都正常了 docker-compose up --build -d docker logs redis
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis實(shí)現(xiàn)延遲任務(wù)的常見方案詳解
延遲任務(wù)(Delayed?Task)是指在未來的某個時間點(diǎn),執(zhí)行相應(yīng)的任務(wù),本文為大家整理了Redis實(shí)現(xiàn)延遲任務(wù)的幾個常見方案,希望對大家有所幫助2024-04-04
Redis 主從搭建的實(shí)現(xiàn)(同主機(jī)和跨節(jié)點(diǎn))
本文主要介紹了Redis 主從搭建的實(shí)現(xiàn),包括同主機(jī)和跨節(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
Redis實(shí)現(xiàn)開機(jī)自啟腳本(linux)
本段文字主要介紹了在Linux系統(tǒng)中通過創(chuàng)建啟動腳本和修改配置文件來實(shí)現(xiàn)Redis服務(wù)的開機(jī)自啟動,并強(qiáng)調(diào)了使用chkconfig命令的重要性,通過本文,讀者可以掌握Redis服務(wù)在Linux系統(tǒng)中的安裝與配置方法2026-06-06

