Redis远程连接失败-“Connection reset by peer”的解决方式(Win10以及Ubuntu)

每篇一句:

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.1IPlocalhost之间的关系,可以移步 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.1bind 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服务的话有可能会是另一个):

    1

  • 默认的配置文件根据Redis服务确定,在服务中查看:

    1

  • 在配置文件中将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端口。有关防火墙的操作不是本文重点,读者自行百度解决即可。

最后,如果本文有什么错误或者不足之处,欢迎指出!

Redis在从客户端读取数据时,如果遇到"Connection reset by peer"的错误,这意味着与客户端的连接被意外中断。这种情况通常发生在网络连接异常或客户端主动关闭连接的情况下。出现这个错误可能有几种原因,比如网络问题、客户端程序错误、防火墙设置等等。 关于你提到的配置,引用中提到了一个Redis的配置文件,其中指定了Redis客户端的参数,如监听地址和端口。配置中"accept"参数指定了Redis监听的IP地址和端口号,"connect"参数指定了连接的Redis主机和端口号。这些配置用于建立客户端与Redis服务器的连接。 引用中提到了一个Java报错"java.net.SocketException: Connection reset",这个错误通常表示与目标主机的连接被重置。这可能是由于网络问题或目标主机关闭了连接导致的。 引用中提到了stunnel的使用,stunnel是一个用于提供SSL加密和解密功能的工具,可以用于解决一些不支持SSL的客户端与服务器之间的连接问题。通过使用stunnel代理,客户端可以在不知道SSL的存在的情况下与服务器进行通信。 总结起来,当Redis在读取客户端数据时遇到"Connection reset by peer"的错误,可能是由于网络问题、客户端程序错误或其他原因导致的连接中断。有时候可以通过配置参数来解决连接问题,也可以考虑使用stunnel等工具来处理不支持SSL的客户端连接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Redis连接出现Error: Connection reset by peer的问题是由于使用Redis的安全模式](https://blog.csdn.net/angou6476/article/details/101997159)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java.net.SocketException: Connection reset 解决方法](https://download.csdn.net/download/weixin_38564503/12816344)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值