In order to enable access over the internet, modify the value of
/etc/mongod.conf By default, it is set to
127.0.0.1 which only allows local access. Changing the value to the IP address of your Droplet will allow external connections. Before doing so, please remember to review the MongoDB security checklist.
# network interfaces
You will need to restart MongoDB for the changes to take effect:
- sudo service mongod restart
In addition to enabling one of the forms of authentication supported by MongoDB, setting up a firewall that only allows remote connections from specific IP addresses is a good security measure to implement. On Ubuntu, you can do this with UFW. The following commands will setup a basic firewall which will allow all outgoing connections from your server but only allow incoming connections via SSH or from the specified IP address (ip.address.to.allow) of your other server.
sudo apt-get install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow from ip.address.to.allow
sudo ufw enable
Using your Droplet's private IP address is also a good idea.
Learn how to setup a firewall with UFW on an Ubuntu / Debian cloud server.