Connection refused to MongoDB errno 111

LinuxMongodbFirewallIptables

Linux Problem Overview


I have a Linode server running Ubuntu 12.04 LTS and MongoDB instance (service is running and CAN connect locally) that I can't connect to from an outside source.

I have added these two rules to my IP tables, where < ip address > is the server I want to connect FROM (as outlined in this MongoDB reference):

iptables -A INPUT -s < ip-address > -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -d < ip-address > -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

And I see the rule in my IP table allowing connections on 27017 to and from < ip address > however when I try to connect from , < ip address > to my mongo database using a command like this:

mongo databasedomain/databasename -u username -p password

I get this error:

2014-07-22T23:54:03.093+0000 warning: Failed to connect to databaseserverip:27017, reason: errno:111 Connection refused 2014-07-22T23:54:03.094+0000 Error: couldn't connect to server < ip address >:27017 (databaseserverip), connection attempt failed at src/mongo/shell/mongo.js:148 exception: connect failed

Any help is VERY APPRECIATED!!!! Thanks!!!

Linux Solutions


Solution 1 - Linux

Thanks for the help everyone!

Turns out that it was an iptable conflict. Two rules listing the port open (which resulted in a closed port).

However, one of the comments by aka and another by manu2013 were problems that I would have run into, if not for the conflict.

So! Always remember to edit the /etc/mongod.conf file and set your bind_ip = 0.0.0.0 in order to make connections externally.

Also, make sure that you don't have conflicting rules in your iptable for the port mongo wants (see link on mongodb's site to set up your iptables properly).

Solution 2 - Linux

Try the following:

sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart

Solution 3 - Linux

These commands fixed the issue for me,

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair
sudo service mongod start
sudo service mongod status

If you are behind proxy, use:-
export http_proxy="http://username:[email protected]:port/"
export https_proxy="http://username:[email protected]:port/"

Reference: https://stackoverflow.com/a/24410282/4359237

Solution 4 - Linux

For Ubuntu Server 15.04 and 16.04 you need execute only this command

sudo apt-get install --reinstall mongodb

Solution 5 - Linux

I Didn't have a /data/db directory. I created one and gave a chmod 777 permission and it worked for me

Solution 6 - Linux

For me, changing the ownership of /var/lib/mongodb and /tmp/mongodb-27017.sock to mongodb was the way to go.

Just do:

sudo chown -R mongodb:mongodb /var/lib/mongodb

and then:

sudo chown mongodb:mongodb /tmp/mongodb-27017.sock

and then start or restart the mongodb server:

sudo systemctl start mongod

or

sudo systemctl restart mongod

and check the status

sudo systemctl status mongod

Solution 7 - Linux

One other option is to just repair your database like so (note: db0 directory should be pre-created first):

mongod --dbpath /var/lib/mongodb/ --repairpath /var/lib/mongodb/db0

This is also an acceptable option in production environments...

Solution 8 - Linux

I also had the same issue.Make a directory in dbpath.In my case there wasn't a directory in /data/db .So i created one.Now its working.Make sure to give permission to that directory.

Solution 9 - Linux

In my case previous version was 3.2. I have upgraded to 3.6 but data files was not compatible to new version so I removed all data files as it was not usable for me and its works.

You can check logs using /var/log/mongodb

Solution 10 - Linux

I follow this tutorial's instructions for installation

How to Install MongoDB on Ubuntu 16.04

I had the same mistake. Finally, I found out that I needed to set the port number

The default port number for the mongo command is 27017

But the default port number in mongo.conf is 29999

Solution 11 - Linux

This done the trick for me

sudo service mongod restart

Solution 12 - Linux

Even though the port is open, MongoDB is currently only listening on the local address 127.0.0.1. To allow remote connections, add your server’s publicly-routable IP address to the mongod.conf file.

Open the MongoDB configuration file in your editor:

sudo nano /etc/mongodb.conf

Add your server’s IP address to the bindIP value:

...
logappend=true

bind_ip = 127.0.0.1,your_server_ip
#port = 27017

...

Note that now everybody who has the username and password can login to your DB and you want to avoid this by restrict the connection only for specific IP's. This can be done using Firewall (read about UFW service at Google). But in short this should be something like this:

sudo ufw allow from YOUR_IP to any port 27017

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondbcooperView Question on Stackoverflow
Solution 1 - LinuxdbcooperView Answer on Stackoverflow
Solution 2 - Linuxmanu2013View Answer on Stackoverflow
Solution 3 - Linuxsk1pro99View Answer on Stackoverflow
Solution 4 - LinuxtechfanoView Answer on Stackoverflow
Solution 5 - LinuxSeadon Francis PintoView Answer on Stackoverflow
Solution 6 - LinuxkodahScriptsView Answer on Stackoverflow
Solution 7 - LinuxOstatiView Answer on Stackoverflow
Solution 8 - LinuxSusampathView Answer on Stackoverflow
Solution 9 - LinuxKamal KumarView Answer on Stackoverflow
Solution 10 - LinuxfreedomView Answer on Stackoverflow
Solution 11 - LinuxAvin MathewView Answer on Stackoverflow
Solution 12 - LinuxRaz BuchnikView Answer on Stackoverflow