无法连接到 EC2 Linux 上的 mongo

Jam*_*mes 4 amazon-ec2 mongodb amazon-web-services

我无法连接到在免费套餐 AWS Amazon Linux 2 AMI 上运行的 mongo 实例。我已经在网上关注了几个页面,我可以确认我所做的事情,但我无法远程连接到 mongo。

  • EC2(安装了mongo)有一个公共IP
  • 安全组(入站)分配端口 = 27017,协议 = tcp
  • 我可以通过 SSH 连接到 EC2(并连接到 mongo,SSH 命令如下所示)
  • 我已注释掉 /etc/mongod.conf 文件中的绑定设置(请参见下面的示例)
  • 重启mongo服务,命令如下
  • 本地计算机 - 我可以成功 ping EC2 公共 IP
  • 本地计算机 - 我尝试使用带有“mongo 1.1.1.1”的终端连接到 EC2,并收到“连接失败消息”

还有什么我可以尝试的吗?谢谢

SSH命令

ssh -i my-key.pem ec2-user@ec2-1-1-1-1.eu-west-2.compute.amazonaws.com
Run Code Online (Sandbox Code Playgroud)

更改了 mongo 绑定设置

bindIp: 0.0.0.0 
Run Code Online (Sandbox Code Playgroud)

重启蒙戈

sudo service mongod restart
Run Code Online (Sandbox Code Playgroud)

更新-入站规则

入境规则

尝试从本地计算机连接到 mongo 时收到错误

Terminal command $ mongo 1.1.1.1

[js] Error: couldn't connect to server 1.1.1.1:27017, connection attempt failed: SocketException: Error connecting to 1.1.1.1:27017 :: caused by :: Operation timed out :
Run Code Online (Sandbox Code Playgroud)

Mongo配置文件(来自EC2机器)

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

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

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


security:
  authorization: 'enabled'

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
~                                   
Run Code Online (Sandbox Code Playgroud)

Man*_*ani 6

不要注释bindIp 设置。如果您评论bindIp设置,它将默认为localhost。您将无法从外部网络进行连接。启用bindIp设置并将值设置为0.0.0.0。

绑定IP

另外,检查您的入站规则 IP 范围和端口 (27017)。

更新入站规则以接受 ipv4 地址 (0.0.0.0/0)