Discussion:
[mongodb-user] Atlas Prematurely reached end of stream error
Subhash Chandra
2018-07-15 17:49:09 UTC
Permalink
I get a Prematurely reached end of stream error when trying to connect to
MongoDB Atlas Free Tier. Using Java 1.8 and Mongo Driver 3.8.0. Does this
have any thing to do with the Cluster setup in Atlas? I got the same
program working with my MongoDB localhost.

Any Idea?


Java Test class


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:***@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();

}}


Error:


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)
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/36d22b91-8b30-4205-9e93-c63190fb1ec2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Wan Bachtiar' via mongodb-user
2018-08-07 07:50:31 UTC
Permalink
Does this have any thing to do with the Cluster setup in Atlas? I got the
same program working with my MongoDB localhost.

Hi Subhash,

Based on the log you provided, looks like the client is having a difficulty
in connecting to MongoDB Atlas.

Looking at your connection string URI, you’re using the SRV address, but
the prefix for the connection format is only showing mongodb://, try
changing this to mongodb+srv://. If you’re using the MongoDB 3.4 or earlier
format, but obfuscated the URI just to post the question, check whether
you’ve also specified ssl=true and the replicaSet parameter.

Also, ensure that you have added to the whitelist
<https://docs.atlas.mongodb.com/security-whitelist/> the IP of the
application in MongoDB Atlas.

Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/49004d9d-fe85-45ec-9bf5-7bf03b4f9769%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...