打开 Ubuntu 防火墙和 Mongod.conf 后无法远程连接到 MongoDB



在Linode上的ubuntu 20.10服务器上重新安装mongodb时,无法远程连接到mongodb。

root@localhost:~# sudo ufw status
Status: active
To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
27017                      ALLOW       0.0.0.0
22 (v6)                    ALLOW       Anywhere (v6)

/etc/mongod.conf

net:
port: 27017
bindIp: 0.0.0.0

mongo服务器已启动并运行

root@localhost:~# sudo service mongod status
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-04-04 18:34:05 UTC; 19min ago
Docs: https://docs.mongodb.org/manual
Main PID: 1332 (mongod)
Memory: 161.0M
CGroup: /system.slice/mongod.service
└─1332 /usr/bin/mongod --config /etc/mongod.conf
Apr 04 18:34:05 localhost systemd[1]: Started MongoDB Database Server.

运行mongo服务器的主机上的netstat

root@localhost:~# sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1332/mongod
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      640/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      817/sshd: /usr/sbin
tcp6       0      0 :::22                   :::*                    LISTEN      817/sshd: /usr/sbin
udp        0      0 127.0.0.53:53           0.0.0.0:*                           640/systemd-resolve

nc -zv IP_ADDRESS 27017超时,所以mongo -u $DB_USERNAME -p $DB_PASSWORD IP_ADDRESS/admin也会超时

nc -zv IP_ADDRESS 22按预期工作

通过执行sudo ufw allow 27017而不是sudo ufw allow from 0.0.0.0 to any port 27017解决

root@localhost:~# sudo ufw status
Status: active
To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
27017                      ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)

相关内容

  • 没有找到相关文章

最新更新