redis SHUTDOWN出错问题解决

启动Redis以后,在停掉的时候报错了,以下:程序员

c80k2@ubuntu:/usr/local/redis$ bin/redis-cli shutdown
(error) ERR Errors trying to SHUTDOWN. Check logs.

须要查看日志。那日志在哪里呢?进配置文件 redis.conf 查看: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 ""

在这里的logfile被用来设置日志的文件名。若是是空字符串,讲迫使Redis用标准输出的方式来进行记录。同时要注意,若是使用了标准输出来记录,但Redis被设置为 daemonize 也就是常驻进程,日志将被发送到 /dev/null。shell

可是咱们尝试去查看/dev/null文件时,发现它是一个c类型文件,也就是 字符设备文件。数据库

l是连接: link
d是目录: directory
c是字符设备文件: character special file
b是块设备: block special file
-是文件: regular fileubuntu

关于/dev/null这个文件, bash

在类Unix系统中,/dev/null,或称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操做成功),读取它则会当即获得一个EOF。
在程序员行话,尤为是Unix行话中,/dev/null 被称为位桶(bit bucket)或者黑洞(black hole)。空设备一般被用于丢弃不须要的输出流,或做为用于输入流的空文件。当你读它的时候,它会提供无限的空字符(NULL, ASCII NUL, 0x00)。ide

其中的一个典型用法是cat $filename  会输出filename对应的文件内容(输出到标准输出)
而使用         cat $filename >/dev/null 
则不会获得任何信息,由于咱们将原本该经过标准输出显示的文件信息重定向到了 /dev/null 中,so what will you get ?
使用  cat $filename 1>/dev/null 也会获得一样的效果,由于默认重定向的 1 就是标准输出。  若是你对 shell 脚本或者重定向比较熟悉的话,应该会联想到 2,也即标准错误输出。this

无论怎么样,为了能更方便地查看日志,咱们更改一下配置:日志

logfile "/var/log/redis/redis.log"

 保存退出后,建立对应的日志文件,并给上读写执行权限,这里直接给了0777。code

重启Redis,让配置生效,再次尝试关闭Redis,

bin/redis-cli SHUTDOWN

仍然报错:

(error) ERR Errors trying to SHUTDOWN. Check logs.

查看日志:

42661:M 30 May 10:45:15.477 # User requested shutdown...
42661:M 30 May 10:45:15.478 * Saving the final RDB snapshot before exiting.
42661:M 30 May 10:45:15.478 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/redis) for saving: Permission denied
42661:M 30 May 10:45:15.478 # Error trying to save the DB, can't exit.

看到是由于保存RDB snapshot快照的时候,因为权限问题,打开失败,进而保存失败,退出失败。

进配置文件看看:

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

工做目录: 数据库DB将被写入这个目录,使用上面的 'dbfilename' 配置指令的文件名。AOF将一样再这个目录下被建立。注意你必须指明一个目录,而不是文件名。

对应的dbfilename配置以下,也就是说redis的DB数据将被写入这个文件。

# The filename where to dump the DB
dbfilename dump.rdb

而工做目录被设置成当前的redis目录 /usr/local/redis,看看目录权限。

drwxr-xr-x  4 root root 4096 May 30 10:48 redis
c80k2@ubuntu:/usr/local/redis$ ps -aux | grep redis | grep -v grep
c80k2     36437  0.0  0.0  14388   692 pts/29   S+   May29   0:00 tail -f redis.log
c80k2     42661  0.0  0.0  51820  3992 ?        Ssl  10:44   0:00 bin/redis-server 127.0.0.1:6379
root      42677  0.0  0.0  61860  3920 pts/20   S+   10:48   0:00 sudo vi redis.conf
root      42678  0.0  0.2  61136  8608 pts/20   S+   10:48   0:00 vi redis.conf

而redis是以当前用户权限运行的,没有root权限。为了解决这个问题,咱们讲工做目录进行设置,并给上0777权限。注意,这里的权限是要从/usr/local/redis目录给起的。

dir /usr/local/redis/redis_dbfiles
c80k2@ubuntu:/usr/local$ sudo chmod -R 0777 redis

从新启动,让配置生效,再次尝试关闭,没有报错。

c80k2@ubuntu:/usr/local/redis$ bin/redis-server redis.conf
c80k2@ubuntu:/usr/local/redis$ bin/redis-cli SHUTDOWN

日志

42661:M 30 May 11:08:57.781 * Saving the final RDB snapshot before exiting.
42661:M 30 May 11:08:57.783 * DB saved on disk
42661:M 30 May 11:08:57.784 * Removing the pid file.
42661:M 30 May 11:08:57.784 # Redis is now ready to exit, bye bye...

进程

c80k2@ubuntu:/usr/local/redis$ ps -aux | grep redis | grep -v grep
c80k2     36437  0.0  0.0  14388   692 pts/29   S+   May29   0:00 tail -f redis.log

解决。

相关文章
相关标签/搜索