Com.mongodb.MongoTimeoutException: timeout after 10,000 ms when waiting for a connection

I suggested that this question was asked several times, but I had to ask it again. Since the solutions provided for this question did not give me an exact answer to get rid of this bloody mistake.

I use mongo-java-driver-2.12.4mongo.jar when I try to insert a document in db. I get the following error. Any help is appreciated.

Error:

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

The code:

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
source share
4 answers

You have been refused a connection. Are you sure mongon is working?

Try connecting to mongoclient:

mongo 127.0.0.1:27000/test

(27000, 27002, 27001).

, .

+3
+3

. - , replicaset . rs.initiate(). , , . local db replicaset, . , :

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

/.

+3

Source: https://habr.com/ru/post/1648231/


All Articles