允许 MongoDB 远程连接时出现 code:48 的 SocketException 错误

SocketException error with code:48 when allowing MongoDB to remote connections

我有一个 运行ning Ubuntu 的 AWS ligthsail 服务器。我刚刚安装了 MongoDB,它在本地运行良好。现在我想使用端口 27017 远程打开它。我已经将该端口添加到 AWS 控制台上的 Networking 列表,并将我的 public IP 添加到配置文件。

这是我对 运行 mongo 的命令:

sudo mongod --fork --config /etc/mongod.conf

这是我遇到的错误:

2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten] modules: none
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten] build environment:
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten]     distmod: ubuntu1804
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten]     distarch: x86_64
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2020-04-07T11:40:24.424+0000 I CONTROL  [initandlisten] options: { config:     "/etc/mongod.conf", net: { bindIp: "0.0.0.0,my-remote-ip", port: 27017 }, processManagement: {     fork: true, timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "the-path-to-the-db", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2020-04-07T11:40:24.424+0000 E STORAGE  [initandlisten] Failed to set up listener:     SocketException: Cannot assign requested address
2020-04-07T11:40:24.425+0000 I CONTROL  [initandlisten] now exiting
2020-04-07T11:40:24.425+0000 I CONTROL  [initandlisten] shutting down with code:48


这是 mongod 的配置:

storage:
  dbPath: the-path-to-the-db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0,<my-public-ip>


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

另外,我检查了 运行ning 进程。 MongoDB 不是 运行ning。我很确定 我也在 SO 和 Google 上搜索但找不到任何东西。 请问有人可以引导我解决这个问题吗?谢谢!

好的。我添加了

security:
  authorization: "enabled"

到配置文件,然后使用以下命令配置ipteables

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


显然在 AWS UI 上打开端口是不够的。