com.mongodb.MongoTimeoutException:在等待连接的10000毫秒后超时

时间:2016-07-18 09:16:49

标签: java mongodb java-ee database nosql

我认为这个问题被问过几次,但我不得不重新考虑它。因为为这个问题提供的解决方案并没有给我一个确切的答案来摆脱这个血腥的错误。

当我尝试将文档插入db时,我使用mongo-java-driver-2.12.4mongo.jar我收到以下错误。任何帮助表示赞赏。

错误:

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=127.0.0.1:27000, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=127.0.0.1:27001, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=127.0.0.1:27002, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
    at com.mongodb.BaseCluster.getDescription(BaseCluster.java:128)

代码:

    public class MongoDbConnectDatabase {

    public static void main(String[] args) {

        // To connect to mongodb server
        try {

             List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
             lstServer.add(new ServerAddress("127.0.0.1", 27000));
             lstServer.add(new ServerAddress("127.0.0.1", 27002));
             lstServer.add(new ServerAddress("127.0.0.1", 27001));
             MongoClient  mongoClient = new MongoClient(lstServer);

            // Now connect to your database
            DB db = mongoClient.getDB("test");
            System.out.println("connect to database successfully");

            DBCollection coll = db.createCollection("mycol", null);
            System.out.println("Collection created successfully");

            DBCollection colReceived= db.getCollection("mycol");
            System.out.println("Collection mycol selected successfully");

            BasicDBObject doc = new BasicDBObject("title", "MongoDB").
                    append("description", "database").
                    append("likes", 100).
                    append("url", "http://www.tutorialspoint.com/mongodb/").
                    append("by", "tutorials point");

            colReceived.insert(doc);
                 System.out.println("Document inserted successfully");

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

}

4 个答案:

答案 0 :(得分:6)

您获得拒绝连接。你确定mongod正在运行吗?

尝试连接mongoclient:

  

mongo 127.0.0.1:27000/test

这适用于所有三个实例(27000,27002,27001)。

如果您对mongoclient也有问题,请检查您的日志。

答案 1 :(得分:4)

此错误的另一个原因可能是mongo-java-driver的版本与您的mongo应用程序不兼容。我的情况:我使用mongo-java-driver版本2.12.3和mongo 3.0.8 - &gt;不起作用。 (https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java

答案 2 :(得分:3)

Here 列出了此错误的所有可能原因。在我的情况下,这是由于replicaset没有初始化。使用rs.initiate()初始化replicaset。 就我而言,我使用了从生产数据创建的卷并将其用于分段。由于local db具有旧的replicaset配置,因此无法成为PRIMARY。我做了以下事情,使它成为主要的:

>use local
> db.dropDatabase();
{ "dropped" : "local", "ok" : 1 }
> rs.initiate()
>myrepl:PRMIARY

现在,客户端能够连接并执行读/写操作。

答案 3 :(得分:0)

提供一个可能给出基本概念的代码段。

package com.mkyong.core;


import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;


public class MongoTest {
    static DBCollection table;
    public static void main(String[] args) {
        MongoClient mongo = null;
        try {
            mongo = new MongoClient("localhost", 27017);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DB db = mongo.getDB("lending");

         table = db.getCollection("bureaudata");
    
            
        /**** Find and display ****/
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("id", "63057298");

        DBCursor cursor = table.find(searchQuery);

        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

    }
}