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,重啟服務即可。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: