redis不能访问本机真实ip地址的如何解决方案

沿途的风景美不胜收,真是让人流连忘返。人生就像一场旅行,不知道在下一站邂逅什么样的人,发生怎么样的故事!

redis无法访问本机真实ip地址

1.我在进行用jedis来连接redis时出现了问题:

我用Jedis jedis = new Jedis("127.0.0.1",6379);可以成功连接,但是我如果把127.0.0.1换成的本机地址如192.168.1.103发现会报错.

2.解决这个问题

要修改redis.windows.conf的配置文件(说明一下我的redis是在windows版的)

这里要改两个地方:

  • 1.修改bind的ip
  • 2.修改protected-mode yes为protected-mode no

我的修改如下

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 192.168.1.103

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# 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 no

上面的代码可以看到我的修改了哪些东西,之后再尝试连接一下。

无法通过ip访问redis服务

尝试在本机以及其他主机通过ip来访问redis服务时,一直出现错误,无法通过IP访问redis服务。

究其原因:

redis.conf文件中配置了访问限制,通过bind来限制了ip访问,默认为127.0.0.1

注释掉bind之后,本地可以通过ip访问,但是其他主机无法访问,在redis3之后,有一个protected-mode 参数,默认开启 yes,改成no,重启服务即可。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

您可能有感兴趣的文章
Redis数据结构SortedSet的底层原理解析

redis主从哨兵模式如何实现一主二从

RediSearch加RedisJSON大于Elasticsearch的搜索存储引擎

Redis如何存储对象

Redis唯一ID生成器的如何实现