Solution: Failed to connect to remote redis service (deployed on linux)

Problem Description:

I deployed the redis service on remote linux and successfully connected locally on linux, but failed to connect with Jedis (or redis client) on remote windows.
code show as below:

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

The error log is as follows (* is to hide the real information)

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

Troubleshooting

  • Make sure redis starts successfully
[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
  • Make sure port number 6379 is open to the outside world
[root@localhost bin]# firewall-cmd --list-ports
8080/tcp 9090/tcp 8848/tcp 6379/tcp
  • Make sure you can ping the remote 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

Through troubleshooting, we found that the IP can be pinged successfully, the port number is open, and the redis service is running normally. (If someone’s troubleshooting results are different from mine, please solve the above problems and ensure the above three conditions)
But we can see that when we check the redis process, he specified 127.0.0.1 local access!
(00:00:00 redis-server 127.0.0.1:6379)

View redis.conf (vim command)

Found the following configuration and comments:

# 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

According to the content of the above annotation, if we want to access the redis service remotely, we need to configure the bind 127.0.0.1 annotation.
Then set protected-mode yes to no

  • Note bind 127.0.0.1
  • protected-mode yes is set to no
  • Save and exit

Restart Redis and test again

Linux local connection successful

127.0.0.1:6379> ping
PONG

The remote jedis connection is successful:

//java 程序运行结果
PONG

Process finished with exit code 0

Is over!

Guess you like

Origin blog.csdn.net/weixin_41674401/article/details/109506700