51

MongoDB Suggests that the easy way to handle security is to run it in a trusted environment, given that, "in such a configuration, one must be sure only trusted machines can access database TCP ports."

What would be the best approach for doing this? Is there a way for mongodb to natively only listen to localhost?

I'm using ubuntu 10.10

1
  • May I ask you to pick my answer as more correct?
    – freedev
    Commented Jan 22, 2013 at 21:01

5 Answers 5

98

Add the following line into mongodb.conf:

bind_ip = 127.0.0.1

As @Josh Rickard stated in comments: don't forget to restart the process after updating the config file:

service mongodb restart
5
  • 4
    This worked for me. Don't forget to restart the process after updating the config file: service mongodb restart Commented Nov 13, 2011 at 15:36
  • 2
    Thanks for answer. I'll just leave here this link for those who will look for this info like me: docs.mongodb.org/manual/core/security
    – user680786
    Commented Jul 1, 2013 at 15:54
  • 8
    Shouldn't this be a default in mongodb? Is it really true, that new mongodb installs are open to the world without any authentication? Commented Aug 30, 2013 at 21:35
  • 2
    @ThomasJensen I just installed mongodb from official Debian/Ubuntu source and it is set to localhost by default (now).
    – Daniel W.
    Commented Sep 22, 2014 at 10:25
  • I had also thought mongo is set to localhost only on installation, and that you had to change the configuration if you wanted it otherwise. But I got a message from Digital Ocean saying that one of my servers had an open mongo instance. I can't access the mongo instance from its public IP, so I'm not sure how Digital Ocean accessed it. But they must have, since they only warned me about the instance I had a mongo server running.
    – B T
    Commented Dec 22, 2015 at 6:31
35

As Andreas mentioned in a round-about way:

mongod --bind_ip 127.0.0.1
2
  • 3
    I wonder why this so vital configuration option is not included among samples in configuration file. Commented Jun 22, 2012 at 12:55
  • To add this to the Windows service, you need to edit the registry entry at [HKLM\SYSTEM\CurrentControlSet\Services\MongoDB]
    – msteiger
    Commented Dec 9, 2015 at 13:29
5

Note that although it didn't appear in the documentation last time I checked, you can also put this setting in the .conf file (vs. command line).

No setting = bind to all ips.

2

No one mentioned the location of the mongodb.conf file which is /etc/mongodb.conf. Double check before running the following commands.

echo "bind_ip = 127.0.0.1" >> /etc/mongodb.conf
service mongodb restart
1
  • 1
    With new versions, it was renamed to /etc/mongod.conf
    – hzitoun
    Commented Oct 7, 2017 at 13:51
1

This configuration appears to be default in 4.4 on Windows ...

C:\Program Files\MongoDB\Server\4.4\bin\mongod.cfg

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

... in case there are any other "poor, unfortunate souls" supporting or using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.