从Java连接到MongoDB会引发错误?

时间:2019-03-27 15:34:57

标签: java mongodb

我正在尝试从Java代码连接到Mongo DB Server。我能够从命令行连接到同一台服务器。但是当我从Java代码进行连接时,遇到了一些问题。
下面是我用来连接数据库的命令行:

mongo --username user --password pwd --authenticationDatabase authDB --ssl --host xxx.yyy.com --port 37043  

我能够与上述命令行连接。我正在使用下面的Java代码连接到MongoDB:

MongoClientOptions.Builder options = MongoClientOptions.builder().sslInvalidHostNameAllowed(true);
String url="mongodb://user:pwd@xxx.yyy.com:37043/authDB?ssl=false"
MongoClient client = new MongoClient(new MongoClientURI(url,options));
System.out.println("Opion"+client.getDatabaseNames());

我收到以下异常:

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
       at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
       at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570)
       at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441)
       at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
       at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
       at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
       at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
       at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105)
       at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62)
       at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
       at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
       at java.lang.Thread.run(Thread.java:745)
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=xxx.yyy.com:37043, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
       at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:182)
       at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
       at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
       at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
       at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
       at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
       at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
       at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
       at com.mongodb.client.internal.MongoIterableImpl.forEach(MongoIterableImpl.java:110)
       at com.mongodb.client.internal.MappingIterable.forEach(MappingIterable.java:54)
       at com.mongodb.client.internal.MappingIterable.into(MappingIterable.java:64)
       at com.mongodb.Mongo.getDatabaseNames(Mongo.java:480)
       at it.damore.solr.importexport.MongoConnect.main(MongoConnect.java:25)

注意:我也尝试从MongoDB Compass连接到同一服务器。我能够成功连接。在MongoDBCompass中,我选择SSL作为Unvalidated(insecure)和SSHTunnel作为None

如果从Java代码连接时缺少某些内容,请提供帮助。

0 个答案:

没有答案