解决:连接远程redis服务失败(在linux部署)

问题描述:

我在远程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!

猜你喜欢

转载自blog.csdn.net/weixin_41674401/article/details/109506700