Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo couldn't connect to [localhost/127.0.0.1:27017]

Tags:

java

mongodb

I'm getting the following error:

נוב 08, 2013 12:05:46 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:27017
java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
    at com.mongodb.DBPort._open(DBPort.java:214)
    at com.mongodb.DBPort.go(DBPort.java:107)
    at com.mongodb.DBPort.go(DBPort.java:88)
    at com.mongodb.DBPort.findOne(DBPort.java:143)
    at com.mongodb.DBPort.runCommand(DBPort.java:148)
    at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
    at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:277)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
    at com.mongodb.DB.getCollectionNames(DB.java:412)
    at Main.Main.main(Main.java:26)

my code is simple (my first time with mongo):

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

        DB db = Client.getDB("qw");
        DBCollection coll[] = new DBCollection[4];
        Set<String> colls = db.getCollectionNames();
        for(String s: colls)
            System.out.println(s);

what is the problem?

like image 240
asaf app Avatar asked Nov 08 '13 10:11

asaf app


2 Answers

Possibly you haven't started the Mongo server.

Open a shell and type

mongod

On the file system, you can start it from $MONGO_INSTALL_PATH/bin/mongod.

Don't close the shell and then try to run your code again.

More info:

  • Manage the mongod process
like image 190
Konstantin Yovkov Avatar answered Sep 22 '22 23:09

Konstantin Yovkov


Try restarting your mongo. I had the same problem, and restarting solved it for me.

like image 20
avian Avatar answered Sep 23 '22 23:09

avian