4

I'm trying to connect my java application to MongoDB server. I'm using

java 8, mongodb server 4.0.9 , mongo-java-driver:3.10.0

I am able to connect with my localhost server but getting following error when connecting to remote server which is ssl enabled by self signing.

INFO: Cluster created with settings {hosts=[host-name:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='300000 ms', maxWaitQueueSize=500}
Jun 07, 2019 3:33:36 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 300000 ms before timing out
Jun 07, 2019 3:33:37 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server host-name:27017
    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:748)
Timed out after 300000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{host-name:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]

i have commented out bind address in mongod.conf file.

 System.setProperty("javax.net.ssl.trustStore", "path\\cacerts");
 System.setProperty("javax.net.ssl.trustStorePassword", "xxxx");
 CountDownLatch latch = new CountDownLatch(1);
 MongoCredential credential = MongoCredential.createCredential("username", 
                            "dbname","xxxx".toCharArray());
 MongoClientSettings settings = MongoClientSettings.builder()
   .credential(credential)
   .applyToSslSettings(ssl -> { ssl.enabled(true); })
   .applyToClusterSettings(builder ->
    builder.serverSelectionTimeout(1, TimeUnit.MINUTES)
   .hosts(Arrays.asList(new ServerAddress("host-address", 27017))))
   .build();
 MongoClient mongoClient = MongoClients.create(settings);
 MongoDatabase database = mongoClient.getDatabase("sample");
 MongoCollection<Document> collection = database.getCollection("data");
 System.out.println(collection.countDocuments());
julika Zen
  • 357
  • 6
  • 22
  • According to https://stackoverflow.com/questions/39079876/mongosocketreadexception-prematurely-reached-end-of-stream-after-a-period-of-i adding a `keepAlive` value may help here – klhr Jun 10 '19 at 08:26
  • I answered to a similar question here https://stackoverflow.com/questions/61289948/mongosocketreadexception-prematurely-reached-end-of-stream-while-connecting-mo/76868718#76868718 – Andrea Ciccotta Aug 09 '23 at 14:24

1 Answers1

4

Finally i fixed this issue. it was related to SSL

i skipped these 2 things

1) exporting client cerificate PEM file to pkcs12 format

openssl pkcs12 -export -out mongodb.pkcs12 -in client.pem

2)Adding following line of code

 System.setProperty ("javax.net.ssl.keyStore", "path to java security folder\\mongodb.pkcs12");
 System.setProperty ("javax.net.ssl.keyStorePassword","changeit");
julika Zen
  • 357
  • 6
  • 22
  • Can you please let me know the process you followed if you skipped the .pem file conversion to .pkcs12? have a similar use case and am facing the same premature end of stream error. – Jimil Shah Jun 21 '22 at 13:52