1.问题描述

最近在windows系统下进行开发,所以用到的redis也是windows版本的。因为解压之后便直接使用了,并没有修改redis相关配置。导致redis跑了一段时间过后就会报下面的错误。

 Failed opening the RDB file dump.rdb (in server root dir D:\environment\Redis-x64-3.2.100) for saving: Permission denied


2.错误信息

# Background saving error
[12784] 20 Oct 10:52:32.091 * 100 changes in 300 seconds. Saving...
[12784] 20 Oct 10:52:32.136 * Background saving started by pid 4428
[4428] 20 Oct 10:52:32.302 # Failed opening the RDB file dump.rdb (in server root dir D:\environment\Redis-x64-3.2.100) for saving: Permission denied
[4428] 20 Oct 10:52:32.303 # rdbSave failed in qfork: Permission denied
[12784] 20 Oct 10:52:32.360 # fork operation complete
[12784] 20 Oct 10:52:32.364 # Background saving error


#背景保存错误
[12784] 10月20日10:52:32.091 * 100个变化在300秒。拯救……
[12784] 20 Oct 10:52:32.136 * pid 4428启动后台保存
[4428] 20 Oct 10:52:32.302 #打开RDB文件转储失败。rdb(在服务器根目录D:\environment\Redis-x64-3.2.100)保存:权限被拒绝
[4428] 20 Oct 10:52:32.303 # rdbSave failed in qfork: Permission denied .日志含义
[12784] 10月20日10:52:32.360 # fork操作完成
[12784] 20 Oct 10:52:32.364 #后台保存错误

3.原因

首先redis默认配置是RDB的存储方式。

RDB方式,是将redis某一时刻的数据持久化到磁盘中,是一种快照式的持久化方法。redis在进行数据持久化的过程中,会先将数据写入到一个临时文件中,待持久化过程都结束了,才会用这个临时文件替换上次持久化好的文件。正是这种特性,让我们可以随时来进行备份,因为快照文件总是完整可用的。

# 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'配置指令。
#只追加文件也将在该目录中创建。
#注意,您必须在这里指定一个目录,而不是一个文件名。
dir。/

而配置文件中并默认的工作目录我们并没有配置,所以会导致redis的RDB文件没有地方存储。从而报上面的错误。


4.解决办法

1. 首先创建RDB的工作目录,作为数据源。

步骤:打开redis的配置文件,在redis目录有个redis.windows.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 'D:/redis_database'

2. 禁用Redis的自启动,设置为手动,不要使用Redis安装版,使用压缩版,通过命令行CMD加载配置文件(redis.windows.conf)启动。出现下面的结果就已经成功了。

    Windows版的Redis有2个配置文件,一个是:redis.windows.conf,另一个是redis.windows-service.conf。

    由于安装版的Redis服务自启动,是直接通过redis-server.exe启动的,但是,启动时并没有加载Redis的配置文件(redis.windows.conf),会导致redis 中bind配置和密码设置不生效。

所以通过手动加载配置文件的方式启动redis。cmd执行下面的命令

命令:redis-server.exe redis.windows.conf 

D:\environment\redis\redis>redis-server.exe redis.windows.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6016
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[6016] 20 Oct 11:32:21.178 # Server started, Redis version 3.2.100
[6016] 20 Oct 11:32:21.181 * DB loaded from disk: 0.002 seconds
[6016] 20 Oct 11:32:21.182 * The server is now ready to accept connections on port 6379

4、再新打开一个cmd(不要关闭之前打的Cmd窗口),启动Redis客户端:

D:\soft\Redis>redis-cli.exe 

5、测试Redis连接,提示无权限。(如果没有设置密码,则能连接成功,并跳过第6步)

127.0.0.1:6379> ping 
(error) NOAUTH Authentication required. 

6、通过密码进入访问,使用 auth + 密码,如下:

127.0.0.1:6379> auth 123456 
OK 
127.0.0.1:6379> ping 
pong
127.0.0.1:6379> 

Redis自启动导致的常见的问题:

1、在CMD命令加载配置文件(redis.windows.conf)进行启动是不成功的。提示如下:

D:\soft\Redis>redis-server.exe redis.windows.conf 
[13760] 11 Jul 16:39:51.067 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error 

因为Redis服务已经自启动,这里是不会再新启动的,故加载配置文件是失败的。也没有出现Redis启动的小盒子(下面有图片,慢慢往下看),需要注意的是Windows版的Redis安装时,默认启动加载的配置文件是redis.windows-service.conf,如下图所示:

2、密码失效。虽然在配置文件(redis.windows.conf)设置了密码,密码为123456:

################################## SECURITY ################################### 
……省略…… 
# requirepass foobared 
requirepass 123456 

但启动客户端进行Redis命令操作时,是不需要密码的,也没有提示无权限操作,这是一个严重的安全问题。

D:\soft\Redis>redis-cli.exe 
127.0.0.1:6379> get name 
"haha" 
127.0.0.1:6379> 

3、Redis访问IP绑定(bind)无效

Redis默认绑定的ip为127.0.0.1,但如果想内网的机器都能访问,则需要设置内网的ip地址,如192.168.100.66,然后redis.host则可以设置为192.168.100.66访问Redis。
Redis ip地址绑定默认说明:

################################## NETWORK ##################################### 

# By default, if no "bind" configuration directive is specified, Redis listens 
# for connections from all the network interfaces available on the server. 
# It is possible to listen to just one or multiple selected interfaces using 
# the "bind" configuration directive, followed by one or more IP addresses. 
# 
# Examples: 
# 
# bind 192.168.1.100 10.0.0.1 
# bind 127.0.0.1 ::1 
# 
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the 
# internet, binding to all the interfaces is dangerous and will expose the 
# instance to everybody on the internet. So by default we uncomment the 
# following bind directive, that will force Redis to listen only into 
# the IPv4 lookback interface address (this means Redis will be able to 
# accept connections only from clients running into the same computer it 
# is running). 
# 
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES 
# JUST COMMENT THE FOLLOWING LINE. 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
bind 127.0.0.1 

主要是意思是,如果设置了bind,只能通过绑定的地址访问Redis。

如果不设置bind,则所有地址都可以访问,如果在项目部署外网,所有人都可以访问到,所以这里也是个注意的地址,还是设置bind比较安全

绑定多个ip地址: bind 127.0.0.1 192.168.100.66 

127.0.0.1和192.168.100.66之间通过空格分隔,不是逗号。

但如果Redis是自启动的,没有加载配置文件(redis.windows.conf)启动,这里的设置也是无效的。如果不绑定ip地址(192.168.100.66),直接设置redis.host=192.168.100.66是访问不了的,出现以下的错误:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

所以说,Redis由Windows自启动的,配置文件(redis.windows.conf)的设置都是无效的

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐