
Rocky Linux 9上のMongoDB 6.0にネットワーク経由で接続可能にします!
記事の目次
MongoDBのネットワーク設定を確認する!
MongoDBのネットワーク設定を確認します。デフォルトでは、127.0.0.1からのみ可能で、ネットワーク経由からの接続は不可に設定されています。
# ss -lnp4
Netid   State    Recv-Q   Send-Q      Local Address:Port        Peer Address:Port   Process                             
udp     UNCONN   0        0               127.0.0.1:323              0.0.0.0:*       users:(("chronyd",pid=734,fd=5))   
tcp     LISTEN   0        128               0.0.0.0:22               0.0.0.0:*       users:(("sshd",pid=747,fd=3))      
tcp     LISTEN   0        4096            127.0.0.1:27017            0.0.0.0:*       users:(("mongod",pid=36714,fd=13)) 
# cat /etc/mongod.conf
...
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
ネットワーク経由で接続可能に設定する!
「bindIp」の設定を「127.0.0.1」から「0.0.0.0」に変更します。アクセス元が確定していれば、アクセス元のIPアドレスのみ設定したほうがセキュリティが強化されます。
# vi /etc/mongod.conf
...
net:
  port: 27017
  bindIp: 0.0.0.0
# systemctl restart mongod
# ss -lnp4
Netid   State    Recv-Q   Send-Q      Local Address:Port        Peer Address:Port   Process                             
udp     UNCONN   0        0               127.0.0.1:323              0.0.0.0:*       users:(("chronyd",pid=734,fd=5))   
tcp     LISTEN   0        128               0.0.0.0:22               0.0.0.0:*       users:(("sshd",pid=747,fd=3))      
tcp     LISTEN   0        4096              0.0.0.0:27017            0.0.0.0:*       users:(("mongod",pid=44101,fd=14)) 
Firewalldの通信許可設定を行う!
Firewalldの通信許可設定を行います。
# firewall-cmd --permanent --add-port=27017/tcp success # firewall-cmd --reload success # firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: enp0s3 sources: services: cockpit dhcpv6-client ssh ports: 27017/tcp protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
おわりに
MongoDBにネットワーク接続するためには、MongoDBの接続元の設定を変更する必要があります。
参考情報
関連記事
参考書籍(Amazon)

