Error connecting to 127.0.0.1:27017 -- target machine actively refused it - MongoDB Issue


In this post we will talk about an issue i encountered after installing MongoDB on my windows machine.

For installation I have followed the installation guide for windows machine mentioned on the MongoDB official website. At the time it all worked perfectly, created a new database and stored documents to it. Even created a blog post about it. Refer here.

Issue

But today when I was trying to open the mongo.exe by double clicking on it, the console opens and then it vanishes. So, I tired again, still the console just showed for a second or two and then vanished again in thin air. Even tried using the mongoDB compass editor to check the connection, but that also didn't connect.

Reason

So this issue can happen if the underline data directory is corrupted or the mongoDB needs some repairing. 

Check for data directory you have started MongoDB with, or you can create another data directory. Mostly if you are using a different data directory, then should solve the issue, but if there is something to be repaired on the MongoDB, then this will again occur, when you again open it.

Resolution

Try opening with a different data directory like this
mongod --dbpath C:/data/db
If the data/db directory is not present, you can create it under C: directory. You can also use any other directory and mention the path here.

If creating the db path also didn't solve the issue, then you have to repair the mongoDB installed.
Go to the installed bin directory and run following command:
mongod --repair
Let this run, it shouldn't take much time. When the repairing is done, it will not close the console and you also shouldn't close it. 

Now, double click on the mongo.exe and the console will not vanish and you can key in your commands. 

This helped me resolving the above issue. Let me know in the comments below if you followed another resolution technique. 

Comments