Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb can't connect to localhost but can connect to localhost's IP address

If I try to run mongodb shell with the mongo command alone, I get:

Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84

exception: connect failed

However, if I stipulate localhost's LAN IP address like this:

mongo 10.10.5.90

...it connects fine.

Any clues??

like image 747
MFB Avatar asked Jan 18 '12 04:01

MFB


People also ask

How to connect to MongoDB server in Linux?

Now to start the MongoDB server, you have to click on Start button on right side this will start you MongoDB server. After starting the MongoDB services, connect to MongoDB server. Open command prompt and write command mongo in the shell. Now, we successfully connect to the MongoDB server.

How to resolve “MongoDB connection error failed to connect to server [localhost]”?

Sometimes, website owners may face error like “MongoDB connection error: MongoError: failed to connect to server [localhost:27017]”. This error occurs when a firewall blocks the connection from an IP address. In order to solve this error, our Support Engineers allow IP address in the firewall configuration file based on the client request.

Why is MongoDB not listening to my IP or port 27017?

Here, 127.0.0.1 (localhost) is the hostname and 27017 is the port number. There could be any reason for this error: Firewall or network configuration blocked you to connect to the server. The MongoDB server is not listening the IP or port. Now, you have to analyse that what could be the reason behind the error and then solve the error:

Why can't I open MongoDB on Windows?

Also make sure to check if the mongo server service is running: win + r > services.msc > MongoDB Server (MongoDB) > Rightclick > Start. Should save some time, hope it helps.


3 Answers

Do you have a bind_ip set in your mongodb.conf (or startup script)? edit for clarity A bind_ip setting limits the IP it will listen on to that IP only.

See the IP Address Binding section: http://www.mongodb.org/display/DOCS/Security+and+Authentication

If not, do you have any firewall rules blocking the localhost access? That would be kind of strange, but I can't think of another reason why it wouldn't work while the LAN IP would work.

like image 69
Eve Freeman Avatar answered Oct 06 '22 20:10

Eve Freeman


This error could also appear if mongodb was not properly shutdown. If you type sudo start mongodb and if it gives a new process id on every execution then your mongodb was not properly shutdown. To resolve this type

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair 
sudo start mongodb
like image 33
benchwarmer Avatar answered Oct 06 '22 20:10

benchwarmer


Just follow all these steps to solve this problem

Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb.
mongod --repair 

Step 3: start mongodb.
sudo start mongodb 
or
sudo service mongodb start

Step 4: Check status of mongodb.
sudo status mongodb 
or   
sudo service mongodb status

Step 5: Start mongo console.
mongo
like image 9
saimadhu.polamuri Avatar answered Oct 06 '22 19:10

saimadhu.polamuri