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:
...it connects fine.
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
I added localhost along with the ip I had in bind_ip while starting mongo and it solved my problem, for example: bin/mongod --dbpath data --logpath mongo.log --fork --bind_ip localhost,10.100.1.2
For people using MongoDB Compass on VMWare windows:
C:\Program Files\MongoDB\Server\5.0\bin by default is the Mongo server directory. You can change bindIp and port in mongod.cfg
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.
Edit your mongod.conf as follow
It works for me.