MongoDB Refusing Connection using Java + Eclipse

10,826

Solution 1

while you can connect with the mongo command it is definite that the mongodb server listen on the port 27017 (while without parameters it tries to connect there). That means in the java code you have to change this line:

MongoClient mongoClient = new MongoClient( "localhost" , 27107 );

To this line:

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

And i am not sure this behavior of the driver that give back connection refused if there is no server listening on a host:port config is something which is good. At least a bit misleading from my point of view.

Solution 2

Got the same error when mongodb was newly installed on our cluster. Code worked when i run the java program on the server mongodb was installed with localhost.

To run the code outside the cluster , got the connection refused error.

The issue was :

Port on which mongodb was installed was limited to localhost. We corrected and restarted the service and it worked perfect !!

Share:
10,826
donsiuch
Author by

donsiuch

Software Engineer & Student

Updated on June 04, 2022

Comments

  • donsiuch
    donsiuch almost 2 years

    When I run the following simple code, I typically get a connection refused error... but 1 time out of 20 it will work randomly. Then continue working repeatedly for 2-3 minutes, then refuse connections again. I can't detect a pattern. I have looked at the other connection refused threads but they are using differing technologies that may or may not be complicating the situation (and unfortunately not every thread is even resolved).

    I am completely new to Mongo and am following this guide: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ . My goal is to have a stable connection so I can experiment/learn the DB. Any help with this matter is greatly appreciated! Keep in mind I'm completely new to this technology and don't know my way around it yet.

    I am using JDK 1.7.0_25 and Eclipse. I have added mongo-driver-2.11.3.jar to my project's build path already. The following is my simple code, directly from the example on the site I listed.

    package database;
    
    import java.util.Set;
    import com.mongodb.MongoClient;
    import com.mongodb.MongoException;
    import com.mongodb.WriteConcern;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.BasicDBObject;
    import com.mongodb.DBObject;
    import com.mongodb.DBCursor;
    import com.mongodb.ServerAddress;
    
    public class MongoPortal {
    
        /*static final String domain = "localhost";  
        static final int port = 27107;
        static final String database = "test";*/
    
        public boolean insert(){
    
            try {
                MongoClient mongoClient = new MongoClient( "localhost" , 27107 );
    
                DB db = mongoClient.getDB("test");
    
                // Get and print all the collections
                Set<String> colls = db.getCollectionNames();
                for (String s : colls)
                    System.out.println(s);
    
                mongoClient.close();
            }
            catch (Exception e){
                e.printStackTrace();
                return false;
            }
    
            return true;
        }
    }
    

    And the error I get:

    Oct 13, 2013 9:12:50 AM com.mongodb.DBTCPConnector initDirectConnection
    WARNING: Exception executing isMaster command on localhost/127.0.0.1:27107
    java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at com.mongodb.DBPort._open(DBPort.java:223)
        at com.mongodb.DBPort.go(DBPort.java:125)
        at com.mongodb.DBPort.go(DBPort.java:106)
        at com.mongodb.DBPort.findOne(DBPort.java:162)
        at com.mongodb.DBPort.runCommand(DBPort.java:170)
        at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
        at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
        at com.mongodb.DB.getCollectionNames(DB.java:400)
        at database.MongoPortal.insert(MongoPortal.java:29)
        at driver.Driver.main(Driver.java:22)
    
    Oct 13, 2013 9:12:50 AM com.mongodb.DBPortPool gotError
    WARNING: emptying DBPortPool to localhost/127.0.0.1:27107 b/c of error
    java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at com.mongodb.DBPort._open(DBPort.java:223)
        at com.mongodb.DBPort.go(DBPort.java:125)
        at com.mongodb.DBPort.call(DBPort.java:92)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
        at com.mongodb.DB.getCollectionNames(DB.java:400)
        at database.MongoPortal.insert(MongoPortal.java:29)
        at driver.Driver.main(Driver.java:22)
    
    com.mongodb.MongoException$Network: Read operation to server localhost/127.0.0.1:27107 failed on database test
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
        at com.mongodb.DB.getCollectionNames(DB.java:400)
        at database.MongoPortal.insert(MongoPortal.java:29)
        at driver.Driver.main(Driver.java:22)
    Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at com.mongodb.DBPort._open(DBPort.java:223)
        at com.mongodb.DBPort.go(DBPort.java:125)
        at com.mongodb.DBPort.call(DBPort.java:92)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
        ... 6 more
    

    When I type mongo in bash, the display I get is:

    @debian:~$ mongo
    MongoDB shell version: 2.4.6
    connecting to: test
    >