14

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??

MFB
  • 19,017
  • 27
  • 72
  • 118

7 Answers7

21

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.

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • Here's my mongodb.conf: bind_ip = 10.10.5.90 port = 27017 dbpath = /data/db logpath = /var/log/mongodb/mongodb.log logappend = true vvvv = true journal = true rest = true – MFB Jan 18 '12 at 04:31
  • Take out the bind_ip setting if you want to be able to connect from localhost also. See the doc I linked. – Eve Freeman Jan 18 '12 at 04:35
  • Wes, thank you. Removing the bind_ip setting fixed the issue. – MFB Jan 18 '12 at 04:50
  • 1
    You should be able to list multiple addresses for bind_ip (comma separated). Not working for me. – Rob Sep 28 '13 at 23:49
  • Where can I find my mongodb.conf? – Solace Jun 20 '16 at 22:37
  • @Solace this is fully up to you. Usually you put it at `/etc/mongodb.conf`. When you start the service, then you must provide the config file, e.g. `mongod --config /etc/mongodb.conf` otherwise is uses default settings. – Wernfried Domscheit May 24 '23 at 06:24
  • How can a firewall block the localhost access? – Wernfried Domscheit May 24 '23 at 06:30
11

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
benchwarmer
  • 2,764
  • 23
  • 25
9

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
saimadhu.polamuri
  • 4,439
  • 2
  • 24
  • 21
1

I prefer configuration

net:
  bindIpAll: true

or

net:
  bindIp: localhost

The upper configuration allows connections from remote computers (unless blocked by firewall), the second one allows only connections from your local machine.

In my opinion, configuration like

net:
  bindIp: 10.10.5.90

makes only sense on a computer with multiple network interfaces.

Note, meanwhile we are in 2023, it makes sense to add

net:
  ipv6: true

or mongod --ipv6 ... because IPv6 is disabled by default, but IPv6 is getting more and more common.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
0

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

Pallav
  • 332
  • 3
  • 8
0

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.

Nightfall
  • 107
  • 1
  • 10
-2

Edit your mongod.conf as follow

bindIp: 0.0.0.0,localhost

It works for me.

Prashobh
  • 11
  • 1