简体   繁体   English

MongoClientURI 连接字符串抛出错误“com.mongodb.MongoSocketReadException:过早到达流的末尾”

[英]MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream”

public void MongoDBClient(String user, String pwd, String dbName, String collectionName) {
    MongoClientURI uri = new MongoClientURI("mongodb+srv://" + user + ":" + pwd + "@cluster0.mff6p.mongodb.net/"
            + dbName + "?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000");

    try (MongoClient mongoClient = new MongoClient(uri)) {
        MongoDatabase database = mongoClient.getDatabase(dbName);
        MongoCollection<Document> collection = database.getCollection(collectionName);
        Document query = new Document("_id", new ObjectId("5f05e46281048f54ac98c455"));
        Document result = collection.find(query).iterator().next();
        System.out.println(result);
        System.out.println("Test3: " + result.getString("Cluster"));
    }
}

Getting exception in the above code -在上面的代码中出现异常 -

    INFO: Exception in monitor thread while connecting to server cluster0-shard-00-01.mff6p.mongodb.net: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:580)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:445)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)
    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:129)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.base/java.lang.Thread.run(Thread.java:834)

Can anyone help me, i think there is an issue with the connection string url, MongoClientURI...谁能帮助我,我认为连接字符串 url,MongoClientURI 存在问题...

Its seems that your connection was terminated by the mongo server.看来您的连接已被 mongo 服务器终止。 It could have multiple reasons and best way to debug is to look at the server logs.它可能有多种原因,最好的调试方法是查看服务器日志。

Usually the following is the issue:通常会出现以下问题:

From documentation:从文档:

For long running applications, it is often prudent to enable "keepAlive" with a number of milliseconds.对于长时间运行的应用程序,通常谨慎的做法是在几毫秒内启用“keepAlive”。 Without it, after some period of time you may start to see "connection closed" errors for what seems like no reason.没有它,一段时间后您可能会开始看到“连接关闭”错误,这似乎是没有原因的。

Try to enable to keepAlive property.尝试启用keepAlive属性。

server: {
        socketOptions: {
            keepAlive: 100,
            connectTimeoutMS: 30000
        }
    }

Try like this像这样试试

const MongoClient = require('mongodb').MongoClient; 

const dburl = "mongodb+srv://" + user + ":" + pwd + "@cluster0.mff6p.mongodb.net/"
        + dbName + "?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000";

MongoClient.connect(dburl,{useNewUrlParser:true,useUnifiedTopology:true},(err,client) => {

                  if(err){
                      console.log("Error",err);
                   }
                  else{
                      console.log("Connected");
                  }  
});

THANKS谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循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 获取com.mongodb.MongoSocketReadException:过早地到达流MongoDB的末尾 - Getting com.mongodb.MongoSocketReadException: Prematurely reached end of stream- MongoDB MongoSocketReadException:在 Spring 引导连接 MongoDB Atlas 时,过早到达 stream 的末尾 - MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot 如何修复 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) Mongo客户端尝试关闭不存在/创建的连接,并因MongoSocketReadException失败:过早到达流的末尾 - Mongo client trying to close connection that didn't exist/create and fails with MongoSocketReadException: Prematurely reached end of stream MongoSocketReadException:过早到达 stream 的末尾(一段时间不活动后) - MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity) 什么是“com.mongodb.MongoSocketReadException”和“com.mongodb.MongoTimeoutException”的解决方案 - What is the solution "com.mongodb.MongoSocketReadException" and "com.mongodb.MongoTimeoutException"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM