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
my code is simple (my first time with mongo):
MongoClient Client = new MongoClient( "localhost" , 27017 );
DB db = Client.getDB("qw");
DBCollection coll = new DBCollection;
Set<String> colls = db.getCollectionNames();
for(String s: colls)
what is the problem?
Possibly you haven't started the Mongo server.
Open a shell and type
On the file system, you can start it from
Don't close the shell and then try to run your code again.
Try restarting your mongo. I had the same problem, and restarting solved it for me.
You haven't started your MongoDB Server.
First start your mongodb server and then run your code.
You can also create MongoDB service which will always be running in the background, so from next time onwards you don't have to start the MongoDB server.
Here are the steps to create MongoDB service:
Copy the ‘mongo.config’ file parallel to log folder inside ‘mongodb’ folder.
Here is the content for your mongo.config file:
dbpath = your_drive\mongodb-win32-x86_64-2.2.3\data\db
logpath = your_drive\mongodb-win32-x86_64-2.2.3\log\mongo.log
diaglog = 3
Create a file MongoServer.bat,
Here is the content for MongoServer.bat
your_drive\mongodb-win32-x86_64-2.2.3\bin\mongod.exe --config "your_drive\mongodb-win32-x86_64-2.2.3\mongo.config"
On your command prompt, go to your mongodb\bin directory and then write this command, mongod --config your_Drive\mongodb-win32-x86_64-2.2.3\mongo.config –install
This will create the service named as ‘Mongo DB’.
To start the service, type on command prompt net start MongoDB.
Hence, your service is created. Now you can perform your task.
For further information, you can visit website.
Probably not your problem, but I've seen this Exception raised when mongodb is refusing the connection because it hit a limit on the number of open connections. Check your mongodb log file for a statement like:
"connection refused because too many open connections".
This can mean you're leaving connections open, or you need to raise the limit on number of open files in the db server.
Seems to have the same problem when using the below code to the connection
MongoClient mongo = new MongoClient("localhost", 27017);
But worked out when changed to
MongoClient mongo = new MongoClient("127.0.0.1", 27017);