每篇一句:
The business of life is the acquisition of memories. In the end that’s all there is.
问题出现:
今天在在本机(Windows 10)以及虚拟机(Ubuntu 16.04)中分别安装了Redis服务,
Windows 10安装 : Redis-x64-3.2.100.msi
Ubuntu安装:
sudo apt-get install redis-server
(版本为:3.0.6
)命令:
redis-server --version
但在测试Redis的远程连接时,遇到了连接失败的错误。
问题解决:
在网上查找原因,说是要将配置文件中的bind 127.0.0.1
注释掉。但为什么这么做呢?
分析原因如下:
redis-server在启动时,如果没有指定配置文件的话,它使用的是默认的配置文件。
在修改配置文件时,发现bind 127.0.0.1
句处上方的注释为:
- ubuntu中为
/etc/redis/redis.conf
:
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# 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
- windows 中为
redis.windows-service.conf
:
# ~~~ 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
选项绑定的 并不是请求连接的客户机的IP ,而是 提供redis-server服务的服务器的网络接口,或者说是 网卡 。
当配置bind 127.0.0.1
时,意味着 只有127.0.0.1
网络接口接收连接请求 ,而 不是只接受来自127.0.0.1
的请求。
切记127.0.0.1
不等于你的本机IP,这是两个不同的网络接口。
有关
127.0.0.1
、IP
、localhost
之间的关系,可以移步 localhost、127.0.0.1 和 本机IP 三者的区别?——知乎
当客户机使用redis-cli -h your_ip_address -p 6379
命令请求连接时,应该是your_ip_address
这个网络接口在接收请求,所以可以做出以下修改:
直接将
bind 127.0.0.1
注释掉 。这时,你所有的网络接口都在接收连接请求。(包括127.0.0.1
,your_ip_address
,…(如果还有其它接口,也在接收请求))(客户机可以远程连接)将
bind 127.0.0.1
修改为bind your_ip_address
。这时,只有your_ip_address
这个接口在接收请求。(客户机可以远程连接,更安全)
修改配置文件后需要重启redis-server
: 命令为:sudo /etc/init.d/redis-server restart
进行如下测试验证:
windows中客户端请求Ubuntu中的Server服务:
- 未修改启动:
C:\Users\14344>redis-cli -h 1**.1**.1**.1** -p 6379
# (一直连接不了,失败)
- 注释
bind 127.0.0.1
:
C:\Users\14344>redis-cli -h 1**.1**.1**.1** -p 6379
1**.1**.1**.1**:6379> keys *
(empty list or set)
# (连接成功)
- 修改
bind 127.0.0.1
为bind your_ip_address
:
C:\Users\14344>redis-cli -h 1**.1**.1**.1** -p 6379
1**.1**.1**.1**:6379> keys *
(empty list or set)
# (连接成功)
继续:
接下来修改windows中的配置文件。
windows中有两个配置文件(一般为
redis.windows-service.conf
,手动安装Redis服务的话有可能会是另一个):默认的配置文件根据Redis服务确定,在服务中查看:
在配置文件中将
bind 127.0.0.1
注释掉,重启Redis服务。
进行如下测试验证:
- 在ubuntu中请求Windows:
bdccl@bdccl-virtual-machine:~$ redis-cli -h 1**.1**.4*.1** -p 6379
1**.1**.4*.1**:6379> keys *
Error: Connection reset by peer
发现 连接成功,但数据操作时发生了错误。
查找原因,发现是需要修改配置文件中protected-mode
配置项。
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
查看注释可知protected-mode
配置项默认开启yes
,redis处于保护模式状态,会拒绝来自其它主机的连接。
- 解决方式:将
protected-mode
配置项设为no
,注意 要在配置文件中修改,若直接在客户端中通过config set protected-mode no
命令修改,只对本次有效,redis-server
重启后,还是为yes
.
对比发现,Ubuntu系统中Redis配置文件中不存在此配置项,该配置项应该是较新版本才加入的(3.2版本后)。
修改为 no
后,再次测试,请求连接,数据操作均可正常使用。
结束:
到此,本人的windows 与 Ubuntu 之间就可以相互连接,进行Redis数据库的操作了。
PS:如果你的远程连接还是有问题的话,应该检查一下服务器端的防火墙设置,看一下是否开启了6379端口。有关防火墙的操作不是本文重点,读者自行百度解决即可。
最后,如果本文有什么错误或者不足之处,欢迎指出!