1. 程式人生 > >ip地址正確,redis卻始終連線失敗

ip地址正確,redis卻始終連線失敗

    今天測試人員測試專案的時候開啟頁面就一直報錯,檢視錯誤日誌是redis連線失敗的問題。但是測試發過來他在本地部署專案修改的配置ip埠都是正確的,遠端也能ping通。

     之前沒遇到過這種問題,後來發現他是連線的本機的redis服務,而沒有修改redis的預設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