Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I assumed this question was asked several times but I had to reask it again. Because solutions provided for this question did not give me an exact answer to get rid of this bloody error.

I use mongo-java-driver-2.12.4 and mongo.jar when I try to insert document to db I get 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)

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

    }

}
like image 498
Tonyukuk Avatar asked Jul 18 '16 09:07

Tonyukuk


People also ask

How to fix the timeout error in MongoDB?

When the client takes too much time to connect to MongodB server than the preset timeout value, it can result in error. And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client. We then compare the values set in the MongoDB server.

What to do when MongoDB client takes too long to connect?

When the client takes too much time to connect to MongodB server than the preset timeout value, it can result in error. And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client.

Why am I getting connection drop error in MongoDB?

This error is most common with MongoDB & happens when the connection gets dropped by the firewall. So, to solve the problem our Support Engineers opened the firewall settings on the server side and allowed the corresponding IP to allow connections.

Why does my Mongo log show intermittent connection failure?

It may be intermittent and not happen all the time. But from the drivers perspective it is timing out trying to select a server. The log message just shows you the current view of the servers after selection has failed, which is it is in the process of connecting to the mongo1 node.


2 Answers

You obtain a Connection refused. Are you sure mongod is running?

Try to connect with mongoclient:

mongo 127.0.0.1:27000/test

and this for all the three instances (27000, 27002, 27001).

If you have problem also with mongoclient, check your logs.

like image 155
chf Avatar answered Oct 14 '22 11:10

chf


another reason for this error can be that the version of mongo-java-driver is not compatible with your mongo application. My case : I was using mongo-java-driver version 2.12.3 with mongo 3.0.8 -> doesn't work. (https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java)

like image 36
Jokeur Avatar answered Oct 14 '22 09:10

Jokeur