MongoDB - Fernzugriff zulassen

MongoDB - Remotezugriff zulassen

In diesem Tutorial zeigen wir Ihnen, wie Sie den Fernzugriff auf einen MongoDB-Server aktivieren. Hier ist die getestete Umgebung:

1. MongoDB Server

  • Private IP - 192.168.161.100

  • Öffentliche IP - 45.56.65.100

  • MongoDB 2.6.3, Port 27017

  • IPTables Firewall

2. Anwendungsserver (gleiches LAN-Netzwerk)

  • Private IP - 192.168.161.200

  • Öffentliche IP - irrelevant

3. Entwickler zu Hause (anderes LAN-Netzwerk, WAN)

  • Öffentliche IP - 10.0.0.1

P.S By default, MongoDB doesn’t allow remote connections.

1. IP binden

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1

Standardmäßig bindet MongoDB nur an die lokale Schnittstelle. Dadurch werden die Remoteverbindungen eingeschränkt. Wenn Sie sich nicht um die Sicherheit kümmern, kommentieren Sie einfach aus, um Remoteverbindungen zu akzeptieren (NICHT empfohlen).

1.1 To allow LAN connections from Application Server.
Da sich beide im selben LAN-Netzwerk befinden, müssen Sie MongoDB nur an die eigene private IP-Schnittstelle binden.

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local and LAN interfaces.
bind_ip = 127.0.0.1,192.168.161.100

Common Mistake
Geben Sie die IP des Anwendungsservers nicht in die Optionbind_ipein. Diesebind_ip-Option weist MongoDB an, Verbindungen von welchen lokalen Netzwerkschnittstellen zu akzeptieren, nicht von welcher „Remote-IP-Adresse“.

Standard - Verbindung fehlgeschlagen

AS (192.168.161.200) <-- LAN --> MongoDB(192.168.161.100) <--> bind_ip (127.0.0.1)

Jetzt - Verbindungserfolg

AS (192.168.161.200) <-- LAN --> MongoDB(192.168.161.100) <--> bind_ip (192.168.161.100, 127.0.0.1)

1.2 To allow remote access for developers at home.
Entwickler greifen über die öffentliche MongoDB-IP 45.56.65.100 per Fernzugriff zu. Um dies zu ermöglichen, binden Sie auch die öffentliche IP-Schnittstelle.

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local, LAN and Public interfaces.
bind_ip = 127.0.0.1,192.168.161.100,45.56.65.100

Note
Für Entwickler zu Hause wird empfohlen, eine VPN-Verbindung einzurichten, anstatt die öffentliche MongoDB-IP-Verbindung zu öffnen. Sie ist anfällig für Angriffe von Personen.

Starten Sie MongoDB neu, damit es wirksam wird.

$ sudo service mongod restart
[ ok ] Restarting database: mongod.

2. IPTables Firewall

Wenn Sie über eine Firewall verfügen, lassen Sie Verbindungen zu Port27017, dem MongoDB-Standardport, zu.

2.1 Any connections can connect to MongoDB on port 27017

iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

2.2 Only certain IP can connect to MongoDB on port 27017

iptables -A INPUT -s  -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d  -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -s 192.168.161.200 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d 192.168.161.200 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

Note
Konsultieren Sie dieseMongoDB firewall documentation

2.3 Here is the firewall rules using in one of my MongoDB servers.

/etc/iptables.firewall.rules

*filter

-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp --dport 27017 -j ACCEPT

#-A INPUT -s  -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
#-A OUTPUT -d  -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

#  Allow SSH connections
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Drop incoming connections if IP make more than 15 connection attempts to port 80 within 60 seconds
-A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
-A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 60  --hitcount 15 -j DROP

#  Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT

Aktualisieren Sie die iptables-Regeln

sudo vim /etc/iptables.firewall.rules
sudo iptables-restore < /etc/iptables.firewall.rules