解決:連線遠端redis服務失敗(在linux部署)

怪力烏龜發表於2020-11-05

問題描述:

我在遠端linux上部署了redis服務並在linux本地連線成功,但是在遠端windows上用Jedis(或redis客戶端)連線失敗。
程式碼如下:

	public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.*.*", 6379);
        System.out.println(jedis.ping());
    }

錯誤日誌如下(*是為了隱藏真實資訊)

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.*.*:6379
	at redis.clients.jedis.Connection.connect(Connection.java:204)
	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:125)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:120)
	at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)
	at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)
	at com.term.TestPing.main(TestPing.java:8)
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at redis.clients.jedis.Connection.connect(Connection.java:181)
	... 6 more

排錯

  • 確定redis啟動成功
[root@localhost bin]# ps -ef|grep redis
root       3257      1  0 10:31 ?        00:00:00 redis-server 127.0.0.1:6379
root       3321   3161  0 10:32 pts/0    00:00:00 grep --color=auto redis
  • 確定6379埠號向外開放
[root@localhost bin]# firewall-cmd --list-ports
8080/tcp 9090/tcp 8848/tcp 6379/tcp
  • 確定能夠ping通遠端ip
PS C:\Users\Termlis> ping 192.168.*.*
正在 Ping 192.168.31.150 具有 32 位元組的資料:
來自 192.168.31.150 的回覆: 位元組=32 時間<1ms TTL=64
來自 192.168.31.150 的回覆: 位元組=32 時間<1ms TTL=64
來自 192.168.31.150 的回覆: 位元組=32 時間<1ms TTL=64
來自 192.168.31.150 的回覆: 位元組=32 時間<1ms TTL=64

192.168.31.150 的 Ping 統計資訊:
    資料包: 已傳送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
    最短 = 0ms,最長 = 0ms,平均 = 0ms

通過排錯發現ip能夠ping通,埠號也是開放的,redis服務也正常執行中。(如果有人排錯結果跟我不一樣,請解決上述問題,保證以上三個條件)
但是可以看到在我們檢視redis程式的時候他指定了127.0.0.1本地訪問!
(00:00:00 redis-server 127.0.0.1:6379)

檢視redis.conf(vim命令)

發現如下配置及註釋:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# 意識就是如果你想遠端訪問,請註釋下一行配置
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

# 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就不能遠端訪問,我們得把它關上。
protected-mode yes

根據上面註釋的內容,我們想要遠端訪問redis服務,需要將bind 127.0.0.1配置註釋。
然後將protected-mode yes設定為no

  • 註釋bind 127.0.0.1
  • protected-mode yes設定為no
  • 儲存並退出

重啟Redis再次測試

linux本地連線成功

127.0.0.1:6379> ping
PONG

遠端jedis連線成功:

//java 程式執行結果
PONG

Process finished with exit code 0

Is over!

相關文章