MongoDB - リモートアクセスを許可する

MongoDB –リモートアクセスを許可する

このチュートリアルでは、MongoDBサーバーへのリモートアクセスを有効にする方法を示します。 テスト済みの環境は次のとおりです。

1. MongoDBサーバー

  • プライベートIP – 192.168.161.100

  • パブリックIP – 45.56.65.100

  • MongoDB 2.6.3、ポート27017

  • IpTablesファイアウォール

2. アプリケーションサーバー(同じLANネットワーク)

  • プライベートIP – 192.168.161.200

  • パブリックIP –無関係

3. 自宅の開発者(さまざまなLANネットワーク、WAN)

  • パブリックIP – 10.0.0.1

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

1. バインドIP

$ 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

デフォルトでは、MongoDBはローカルインターフェイスのみにバインドし、リモート接続を制限します。 セキュリティを気にしない場合は、コメントアウトしてリモート接続を受け入れます(推奨されません)。

1.1 To allow LAN connections from Application Server.
両方が同じLANネットワーク内にあるため、MongoDBを独自のプライベートIPインターフェースにバインドする必要があります。

$ 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
アプリケーションサーバーのIPをbind_ipオプションに入れないでください。 このbind_ipオプションは、どの「リモートIPアドレス」ではなく、どのローカルネットワークインターフェイスからの接続を受け入れるようにMongoDBに指示します。

デフォルト-接続失敗

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

今–接続成功

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.
開発者は、MongoDBパブリックIP 45.56.65.100を介してリモートアクセスします。これを許可するには、パブリックIPインターフェースもバインドします。

$ 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
自宅の開発者には、MongoDBパブリックIP接続を開くのではなく、VPN接続を設定することをお勧めします。これは人の攻撃に対して脆弱です。

MongoDBを再起動して有効にします。

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

2. IpTablesファイアウォール

ファイアウォールがある場合は、MongoDBのデフォルトポートであるポート27017での接続を許可します。

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
これを参照してくださいMongoDB 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

iptablesルールを更新する

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