MongoDB提前到达流结束

时间:2018-07-15 17:42:50

标签: java mongodb

当尝试连接到MongoDB Atlas Free Tier时,我收到流的提前结束错误。使用Java 1.8和Mongo驱动程序3.8.0。这与Atlas中的群集设置有什么关系吗?我在MongoDB本地主机上使用了相同的程序。

有什么想法吗?

Java测试类

import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoTest {

public static void main(String args[]) {

    MongoClientURI uri = new MongoClientURI(
            "mongodb://username:password@cluster0-****.mongodb.net");

    MongoClient mongoClient = new MongoClient(uri);
    MongoDatabase database = mongoClient.getDatabase("account");

    System.out.println("Hello");

    MongoCollection<Document> collection = database.getCollection("users");

    System.out.println(collection.count());

    mongoClient.close();

  }
}

错误:

    Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[cluster0-****.mongodb.net:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Hello
Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server cluster0-****.mongodb.net:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:92)
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:554)
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:425)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:289)
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:106)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
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:748)

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=cluster0-****.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:179)
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.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:248)
at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:183)
at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:171)
at MongoTest.main(MongoTest.java:21)

1 个答案:

答案 0 :(得分:1)

确保将您的客户地址(通过在Google中键入“我的IP地址是什么”很容易找到)添加到群集的IP白名单中。没有这个,您将无法连接。

IP白名单位于“安全性”选项卡下。

enter image description here