By default MongoDB doesn’t allow remote connections. MongoDB bind to local interface only, it will restrict the remote connections, besides that to allow LAN connections from Application Server open and modify your mongod.conf file like below.
In this tutorial i’m using:
- Debian 9 Linux 64bit x64
- MongoDB v4.0.2 Community Server
- Compute Engine on Google cloud platform
Allow Remote Access On MongoDB (Public Access)
$ nano /etc/mongod.conf # /etc/mongod.conf # Listen to Any/Public. bindIp = 0.0.0.0
then restart mongod.conf file to take effect with this command “sudo service mongod restart“. Because if you not restart your new configuration, you will not take any effect on your system.
Allowing IpTables Firewall For Public Access
Finally, allow connections on port 27017 to your firewall.
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
In this tutorial, you have learned how to enable remote access to a MongoDB server. For another mongoDB tutorial you can learn from official documentation. Also do not forget to learn how to use mongoDB in real world programming with spring data mongoDB for inserting, updating and deleting data in mongoDB.