Is it safe to open
A database should only be reachable from the application servers. Bind it to localhost or a private network, require authentication and TLS, and connect from outside through a VPN or an SSH tunnel.
How to open port 27017
Open to everyone
# Linux iptables iptables -A INPUT -p tcp --dport 27017 -j ACCEPT # Ubuntu ufw ufw allow 27017/tcp # firewalld firewall-cmd --permanent --add-port=27017/tcp && firewall-cmd --reload # MikroTik /ip firewall filter add chain=input protocol=tcp dst-port=27017 action=accept place-before=0 comment="port 27017" # Windows (PowerShell) New-NetFirewallRule -DisplayName "Port 27017 TCP" -Direction Inbound -Protocol TCP -LocalPort 27017 -Action Allow
Only for your addresses
# allow only one address or network (replace 203.0.113.0/24) iptables -A INPUT -p tcp --dport 27017 -s 203.0.113.0/24 -j ACCEPT iptables -A INPUT -p tcp --dport 27017 -j DROP ufw allow from 203.0.113.0/24 to any port 27017 proto tcp /ip firewall filter add chain=input protocol=tcp dst-port=27017 src-address=203.0.113.0/24 action=accept /ip firewall filter add chain=input protocol=tcp dst-port=27017 action=drop
If the server is behind a router, you also need a port forward (dst-nat). Behind ISP CGNAT (100.64.0.0/10 on the WAN) forwarding will not help: you need a public IP or a VPN.
What is listening
Check on the server
# what listens on the port ss -lntup | grep ':27017 ' netstat -ano | findstr :27017 # Windows lsof -i :27017 # macOS # test from another machine nc -vz example.com 27017