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??
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.
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.
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:
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With