Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to MongoDB

I've just installed MongoDB (standard Ubuntu build, not the latest stable) and for some reason I can't connect:

Mon Feb  6 03:11:22 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:79
exception: connect failed

Now my machine isn't 127.x.x.1 it's for some reason x.x.x.2 (But i've changed my config to bind to that address, and also tried to bind to my public IP which provided no avail.

Config:

#mongodb.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 199.21.114.XX
port = 27017

I've checked the logs and only startup info is displaying in there.

I've also checked that the deamon is running too, and it is - I even tried running it manually with a --fork.

Has anyone come across this before? Or any suggestions?

Thx

UPDATE:

After a restart - here are the logs:

** WARNING: You are running in OpenVZ. This is known to be broken!!!

Tue Feb  7 19:43:32 [initandlisten] db version v1.8.2, pdfile version 4.5
Tue Feb  7 19:43:32 [initandlisten] git version: nogitversion
Tue Feb  7 19:43:32 [initandlisten] build sys info: Linux allspice 2.6.24-28-se$
Tue Feb  7 19:43:32 [initandlisten] *** warning: spider monkey build without ut$
Tue Feb  7 19:43:32 [initandlisten] waiting for connections on port 27017
Tue Feb  7 19:43:32 [websvr] web admin interface listening on port 28017
like image 501
Stuart.Sklinar Avatar asked Feb 05 '12 23:02

Stuart.Sklinar


8 Answers

I solved this problem by commenting out the following line in my /etc/mongod.conf file.

# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip = 127.0.0.1

You will need to follow this config change with restarting the mongod process.

sudo service mongod restart
like image 74
Jeremy White Avatar answered Oct 03 '22 01:10

Jeremy White


You must specify the IP (199.21.114.XX), by running mongo 199.21.114.XX.

Otherwise, if you want it to listen on localhost as well, you should remove the bind_ip setting in your config file.

Update Check your firewall config. Since you're connecting to the external IP, it can be configured to block, even from the local box.

like image 41
Eve Freeman Avatar answered Oct 03 '22 01:10

Eve Freeman


sudo rm /var/lib/mongodb/mongod.lock
like image 27
andyshi Avatar answered Oct 03 '22 01:10

andyshi


I had the same issue just because the silly mistake.

first remove mongodb file which is fine

rm -rf /tmp/mongodb-27017.sock

where I did mistake inside the /etc/mongod.conf

# network interfaces
net:
  port: 27017
 #bindIp: 127.0.0.1
bindIp: privateIp

instead of

net:
  port: 27017
  bindIp: 10.1.2.4

for listen to all available ips

bindIp: [127.0.0.1,10.128.0.2]

restart the mongodb

sudo service mongod start

hopefully this answer help for someone

like image 40
Gabber Avatar answered Oct 02 '22 23:10

Gabber


By default, running the mongo console command will look for mongodb on your localhost, hence the 127.0.0.1 IP which is your local loopback. Also, the default config for mongod should be available on localhost. Its better to do it this way for now to start, unless you have turned on auth. Make sure you have auth enabled or your database is running openly on your public ip.

When you want to connect to a host other than localhost or your default config, you do:

mongo <host>
like image 29
jdi Avatar answered Oct 02 '22 23:10

jdi


following steps may help you :

  1. open terminal and run "sudo rm /var/lib/mongodb/mongod.lock"
  2. now run " sudo mongod --repair "
  3. now "sudo start mongodb"
  4. now just check the status of mongodb status "sudo status mongodb"

5 lastly just invoke command "mongo"

like image 20
rahul sharma Avatar answered Oct 03 '22 00:10

rahul sharma


in /etc/mongod.conf

I changed,

bindIp: 127.0.0.1

to

bindIp: 0.0.0.0

which is all ports. Yeah, I know it's risky, but I am running on an internal network.

BTW commenting out #bindIp: 127.0.0.1 did NOT work for me, running under Ubuntu 18.

like image 29
reselbob Avatar answered Oct 03 '22 01:10

reselbob


I bumped into this when mongod was running but not yet running "properly". I'm guessing there is probably a better way to wait for it to come up after installation, but adding a sleep 10 seems to fix it.

A slightly less cavemanly approach might be to tail -f its log file and look for the event where it tells you that it is listening on the interface you specified.

local log=/var/log/mongodb/mongod.log
sudo -u mongodb touch "$log"
service mongod start &
launcher=$!
tail -0f "$log" |
grep -q 'port: 27017'
wait "$launcher"
sleep 1

The final sleep is a bit of an act of desperation; it seems to take a jiffy after it logs the startup until it's properly up and listening.

See also Bash shell script not getting connect to MongoDB even if status is active

like image 39
tripleee Avatar answered Oct 03 '22 01:10

tripleee