Mongodb java驱动程序连接超时

时间:2017-06-30 21:16:06

标签: mongodb sockets mongodb-java

我在我的Ubuntu 16.04上全新安装了mongoDB(3.4.5)。

我能够成功连接python驱动程序,但是我的java驱动程序不断返回连接超时异常。

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:?]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:?]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_121]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_121]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204) ~[?:1.8.0_121]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_121]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_121]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_121]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:?]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:?]
... 3 more

我已经使用以下配置选项启动了mongo DB:

{ config: "/etc/mongod.conf", net: { bindIp: "0.0.0.0", port: 27017 }, 
storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } },
 systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log", quiet: true } }

在我的Java课程中,我有:

MongoClient mongoClient = new MongoClient(host:"0.0.0.0", port:27017);

我已经使用telnet检查过端口27017是否可访问。不知道为什么这会导致连接超时。我真的很感激任何帮助。

感谢。

1 个答案:

答案 0 :(得分:2)

关于传出端口27017被阻止存在一些问题。添加防火墙规则以打开该端口修复了该问题。