gpt4 book ai didi

java - MongoDB 不响应 Java 客户端

转载 作者:行者123 更新时间:2023-12-01 11:15:12 25 4
gpt4 key购买 nike

我在 Linux 7 上运行 mongodb。来自远程系统的 Java 客户端尝试使用 Driver 3.0.2 连接到它,代码如下:

 mongoClient = new MongoClient(new ServerAddress("10.12.22.22", 27017),
new MongoClientOptions.Builder().connectTimeout(300000).socketTimeout(300000).build());

结果出现以下异常:

**INFO: Exception in monitor thread while connecting to server 10.12.22.22:27017
com.mongodb.MongoSocketOpenException: Exception opening socket**
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:745)
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:345)
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 com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more

在 Windows 上运行的 Java 客户端:10.12.22.116在Linux上运行的Mongo DB:10.12.22.22:27017

当Java客户端执行时,Linux系统上的tcpdump清楚地显示数据包到达mongo db进程,但数据库仍然没有响应。

[root@localhost ~]# tcpdump -i enp0s3 port 27017 -vv

tcpdump: listening on enp0s3, link-type EN10MB (Ethernet), capture size 65535 bytes00:22:40.026816 IP (tos 0x0, ttl 128, id 8435, offset 0, flags [DF], proto TCP (6), length 52)
10.12.22.116.12364 > 10.12.22.22.27017: Flags [S], cksum 0xdd04 (correct), seq 1967333969, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
00:22:43.026954 IP (tos 0x0, ttl 128, id 8437, offset 0, flags [DF], proto TCP (6), length 52)
10.12.22.116.12364 > 10.12.22.22.27017: Flags [S], cksum 0xdd04 (correct), seq 1967333969, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
00:22:49.028216 IP (tos 0x0, ttl 128, id 8439, offset 0, flags [DF], proto TCP (6), length 48)

MongodB正在监听目标端口,如下所示:

[root@localhost ~]# netstat -tunlep

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 20288 2360/master
tcp 0 0 10.12.22.22:27017 0.0.0.0:* LISTEN 1001 27947 3220/mongod**
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1001 27945 3220/mongod

也没有 Activity 的防火墙(使用iptables -L -n检查)

请看看这里缺少什么导致了这个问题?

最佳答案

最后发现是 Linux 防火墙阻止了它。我使用

禁用了它
service iptables stop

关于java - MongoDB 不响应 Java 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com