前置知识:Redis最简单的基本命令:
1. 系统终端
./redis-server 启动redis服务
./redis-cli 启动redis客户端
./redis-cli shutdown 关闭redis服务
2. 在使用./redis-cli进入redis命令终端之后,
shutdown 关闭redis服务
save 保存存储至磁盘
set [key] [value] 对键[key]赋值[value]
get [key] 获取键[key]对应的值
问题描述:
Redis安装完成,使用./redis-server启动之后,运行./redis-cli进入redis终端,对键值的操作可正常完成,但shutdown与save会报错,具体在终端或者日志中体现为:
1. shutdown请求失败:
1550:M 02 Jan 18:22:48.778 # User requested shutdown...
1550:M 02 Jan 18:22:48.779 * Saving the final RDB snapshot before exiting.
1550:M 02 Jan 18:22:48.781 # Failed opening the RDB file dump.rdb (in server root dir /usr/share/redis/redis-3.2.6/src) for saving: Permission denied
1550:M 02 Jan 18:22:48.782 # Error trying to save the DB, can‘t exit.
2. save失败
1550:M 02 Jan 18:42:05.298 # Failed opening the RDB file dump.rdb (in server root dir /usr/share/redis/redis-3.2.6/src) for saving: Permission denied
shutdown和save失败的原因是一样的,对rdb文件dump.rdb的操作过程出现问题(虽然日志中显示permission denied,但不一定为权限问题,可能是目录或则文件无法生成等多方面)
解决方法:
综合考虑了以下两点(非常抱歉,因为两部分都同时修改后问题解决,所以无法确定是哪一点导致上述问题)
1. 在服务启动之后,日志信息有如下Warning显示:
1550:M 02 Jan 18:16:12.147 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1550:M 02 Jan 18:16:12.148 # Server started, Redis version 3.2.6
1550:M 02 Jan 18:16:12.148 # 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.
1550:M 02 Jan 18:16:12.148 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
据此推测可能由于某些存储上的上限极低(上述日志貌似默认0了),导致创建文件失败,这里笔者理解为:没有创建大小为X的文件的权限导致permission denied
2. 根据报错日志显示rdb文件是建立在 /usr/share/redis/redis-3.2.6/src(即redis安装目录)下,但实际查看时此目录并木有rdb文件,即使redis是在系统的root用户下启动服务,shutdown和save依旧会出现上述error。所以感觉在这里没有文件权限的可能性应该不大,不过最后还是修改配置文件更新了rdb文件的默认生成目录。
操作:
1. 解决Warning
A. 终端root用户执行:sysctl vm.overcommit_memory=1
B. 终端root用户执行:echo never > /sys/kernel/mm/transparent_hugepage/enabled
C. 终端root用户执行:vim /etc/rc.local,在打开文件之后,将echo never > /sys/kernel/mm/transparent_hugepage/enabled加入在语句exit 0之前
2. 更改rdb文件默认路径
修改conf文件,在这里建议,复制一份新的conf文件,如test.conf,在test.conf进行修改,之后启动redis时,使用脚本./redis-server test.conf启动服务。
修改方式:在redis原来的解压目录下找到redis.conf文件(也可以在终端用命令locate redis.conf查找其位置),
打开后定位到如下位置:
# 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 ./
将其中的"dir ./"修改为相应的目录,如"dir /home/distance/redis_dbfiles/"(这里需要注意最后的斜杠不能忘记,如果缺失斜杠可能会解析为文件而非目录),保存。
上述操作完成之后,可以正常save与shutdown。