86

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

dbcooper
  • 2,299
  • 2
  • 14
  • 8

12 Answers12

140

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).

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
dbcooper
  • 2,299
  • 2
  • 14
  • 8
52

Try the following:

sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
manu2013
  • 1,215
  • 11
  • 8
  • 3
    No luck. Still getting the connection failure. Thanks for the try. – dbcooper Jul 23 '14 at 17:59
  • 3
    Is your MongoD server bound to listen on all interfaces? 2.6.x is by default set to listen only on localhost. You can confirm this by "netstat -an | grep 27017" and it should show what all IP addresses the mongod is bound to. – aks Jul 24 '14 at 10:19
  • 2
    Make sure that in your configuration file mongodb.conf, the bindIp setting (bind_ip for earlier versions) is set to listen to all interfaces by assigning the value 0.0.0.0 . By default, it only listens on localhost as its default value is 127.0.0.1 . – manu2013 Jul 24 '14 at 12:46
  • 1
    sometimes you need to remove the `mongodb-.sock` file. in my case it is located on /tmp folder – Matan Tubul Jan 20 '19 at 09:41
  • @MatanTubul after trying everything and nothing worked, removing the file from /tmp solved it. Thanks! – Elia Jan 31 '21 at 12:00
48

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:password@company.com:port/"
export https_proxy="http://username:password@company.com:port/"

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

Community
  • 1
  • 1
sk1pro99
  • 967
  • 1
  • 12
  • 23
  • If I run this command does it remove the data I have stored in the database – c74ckds May 17 '16 at 15:01
  • @devend It shouldn't as repair command runs repairDatabase command on all databases. repairDatabase: Checks and repairs errors and inconsistencies in data storage. repairDatabase is analogous to a fsck command for file systems. [Reference](https://docs.mongodb.com/v3.0/reference/command/repairDatabase/#repairdatabase) – sk1pro99 May 21 '16 at 03:20
17

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

sudo apt-get install --reinstall mongodb
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
techfano
  • 171
  • 1
  • 3
8

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

7

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
kodahScripts
  • 71
  • 1
  • 4
3

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

Ostati
  • 4,623
  • 3
  • 44
  • 48
2

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.

Susampath
  • 706
  • 10
  • 13
1

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

Kamal Kumar
  • 3,393
  • 2
  • 19
  • 15
  • MongoDB should be successively upgraded to each major version until you reach the target. In this case, you need to upgrade to 3.4 first and you also need to configure featureCompatibilityVersion: https://docs.mongodb.com/manual/release-notes/3.6-upgrade-standalone/ By doing so, you don't need to remove your data files, which is absolutely not an option if you have a large database. – sezanzeb Sep 15 '20 at 22:56
1

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

freedom
  • 110
  • 2
  • 11
1

This done the trick for me

sudo service mongod restart
Avin Mathew
  • 336
  • 11
  • 25
0

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
Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96