簡體   English   中英

com.mongodb.MongoTimeoutException:等待連接時 10000 毫秒后超時

[英]com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect

我以為這個問題被問了好幾次,但我不得不再問一次。 因為為這個問題提供的解決方案沒有給我一個確切的答案來擺脫這個血腥的錯誤。

當我嘗試將文檔插入數據庫時​​,我使用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();
        } 

    }

}

您獲得連接被拒絕。 你確定 mongod 正在運行?

嘗試與 mongoclient 連接:

蒙戈 127.0.0.1:27000/測試

這適用於所有三個實例(27000、27002、27001)。

如果您對 mongoclient 也有問題,請檢查您的日志。

此錯誤的另一個原因可能是 mongo-java-driver 的版本與您的 mongo 應用程序不兼容。 我的情況:我使用 mongo-java-driver 版本 2.12.3 和 mongo 3.0.8 -> 不起作用。 https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java

這里列出了此錯誤的所有可能原因。 在我的情況下,這是由於副本集未初始化。 使用rs.initiate()初始化副本rs.initiate() 就我而言,我使用了從生產數據創建的卷並將其用於暫存。 由於local數據庫具有舊的副本集配置,因此無法成為 PRIMARY。 我做了以下事情使其成為主要:

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

現在客戶端能夠連接並執行讀/寫操作。

給出一個可能給出基本想法的片段。

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

    }
}
            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM