我在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进程但仍然没有来自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
也没有激活防火墙(使用iptables -L -n
检查)
请查看导致此问题的遗漏内容?
答案 0 :(得分:1)
最后,Linux防火墙正在阻止它。我使用
禁用了它service iptables stop