简体   繁体   English

获取com.mongodb.MongoSocketReadException:过早地到达流MongoDB的末尾

[英]Getting com.mongodb.MongoSocketReadException: Prematurely reached end of stream- MongoDB

I'm trying to connect my java application to MongoDB server. 我正在尝试将我的java应用程序连接到MongoDB服务器。 I'm using 我正在使用

java 8, mongodb server 4.0.9 , mongo-java-driver:3.10.0 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. 我能够连接到我的localhost服务器,但在连接到远程服务器时遇到以下错误,该服务器是通过自签名启用的ssl。

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. 我在mongod.conf文件中注释掉了绑定地址。

 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());

Finally i fixed this issue. 最后我解决了这个问题。 it was related to SSL 它与SSL有关

i skipped these 2 things 我跳过这两件事

1) exporting client cerificate PEM file to pkcs12 format 1)将客户端证书PEM文件导出为pkcs12格式

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

2)Adding following line of code 2)添加以下代码行

 System.setProperty ("javax.net.ssl.keyStore", "path to java security folder\\mongodb.pkcs12");
 System.setProperty ("javax.net.ssl.keyStorePassword","changeit");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongoDB和Spark:“ com.mongodb.MongoSocketReadException:过早到达流的末尾” - mongoDB & Spark: “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” com.mongodb.MongoSocketReadException:过早到达流的结尾 - com.mongodb.MongoSocketReadException: Prematurely reached end of stream 将Java应用程序连接到MongoDB-com.mongodb.MongoSocketReadException:已提前到达流的末尾 - Connect Java- Application to MongoDB - com.mongodb.MongoSocketReadException: Prematurely reached end of stream MongoClientURI 连接字符串抛出错误“com.mongodb.MongoSocketReadException:过早到达流的末尾” - MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” MongoSocketReadException:在 Spring 引导连接 MongoDB Atlas 时,过早到达 stream 的末尾 - MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot 什么是“com.mongodb.MongoSocketReadException”和“com.mongodb.MongoTimeoutException”的解决方案 - What is the solution "com.mongodb.MongoSocketReadException" and "com.mongodb.MongoTimeoutException" 引起原因:com.mongodb.MongoSocketReadException:异常接收消息 - Caused by: com.mongodb.MongoSocketReadException: Exception receiving message 如何修复 com.mongo.MongoSocketReadException 并在尝试插入文档时出现消息“过早到达 Stream 的末尾”? - How do I fix a com.mongo.MongoSocketReadException, with the message "Prematurely reached the end of Stream" on attempting to insert a document? MongoSocketReadException:过早到达流末尾(使用 ssl 从 Java 到 Mongo) - MongoSocketReadException: Prematurely reached end of stream (Java to Mongo using ssl) MongoSocketReadException:过早到达 stream 的末尾(一段时间不活动后) - MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM