如何连接到 MongoDB EC2 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20175819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:31:14  来源:igfitidea点击:

How to connect to MongoDB EC2 instance

mongodbamazon-ec2

提问by user2708100

we have an EC2 MongoDB 2.4 instance from Amazon MarketPlace. when i try to access it from my computer using the mongo command like so:

我们有一个来自 Amazon MarketPlace 的 EC2 MongoDB 2.4 实例。当我尝试使用 mongo 命令从我的计算机访问它时,如下所示:

mongo xx-xx-xx-xx-xx.compute-1.amazonaws.com

i get the following error

我收到以下错误

Error: couldn't connect to server xx-xx-xx-xx-xx.compute-1.amazonaws.com:27017 at src/mongo/shell/mongo.js:147
exception: connect failed
  • i can connect to the remote instance using ssh so it is reachable.
  • the port is the default port 27017.
  • mongod is running and working on the remote instance.
  • 我可以使用 ssh 连接到远程实例,因此可以访问。
  • 端口是默认端口 27017。
  • mongod 正在运行并处理远程实例。

any suggestions on how to fix this?

对于如何解决这个问题,有任何的建议吗?

回答by Jundiaius

As frisky said, you have to open the port 27017 at the EC2 Security Group Console. (To know more about how to do that: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html)

正如 frisk 所说,您必须在 EC2 安全组控制台上打开端口 27017。(要了解更多关于如何做到这一点:http: //docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

But you also need to change the bind_ipvariable at the /etc/mongodb.conf file. You need to comment the line or assign the ip that will be able to connect the DB to it.

但是您还需要更改bind_ip/etc/mongodb.conf 文件中的变量。您需要注释该行或分配能够将数据库连接到它的 IP。

回答by deepakmodak

Since mongod is running and working on the remote instance, you can access your MongoDB content via

由于 mongod 正在远程实例上运行和工作,因此您可以通过以下方式访问您的 MongoDB 内容

mongo xx-xx-xx-xx-xx.compute-1.amazonaws.com or mongo machine_elastic_IP

mongo xx-xx-xx-xx-xx.compute-1.amazonaws.com 或 mongo machine_elastic_IP

Before that you need to open Inbound port for that machine.By-default port 27017 is closed for external world.

在此之前,您需要为该机器打开入站端口。默认端口 27017 对外关闭。

For more info refer : http://docs.aws.amazon.com/gettingstarted/latest/wah/getting-started-security-group.html

有关更多信息,请参阅:http: //docs.aws.amazon.com/gettingstarted/latest/wah/getting-started-security-group.html

回答by user2708100

Amazon created mongo security group without 27017 open. opening port 27017 to my pc fixed the issue.

亚马逊创建 mongo 安全组没有 27017 打开。打开 27017 端口到我的电脑解决了这个问题。

回答by Stas Ezersky

The following two steps enabled the remote connection for me:

以下两个步骤为我启用了远程连接:

    1. Opening the inbound rould for my VPC at port 27017 VPC -> Security -> Security Groups -> choose your instance's TCP roule -> click "Edit Rules" -> add a rule with Type: "Custom TCP Rule", Protocol: TCP, Port Range: 27017, Source: Custom 0.0.0.0/0 -> save
    1. SSH into your instance -> sudo vi /etc/mongod.conf -> set bindIp: 0.0.0.0 -> save -> sudo service mongod restart
    1. 在端口 27017 VPC 上为我的 VPC 打开入站 rould -> 安全 -> 安全组 -> 选择您实例的 TCP 规则 -> 单击“编辑规则”-> 添加规则类型:“自定义 TCP 规则”,协议:TCP,端口范围:27017,来源:自定义 0.0.0.0/0 -> 保存
    1. SSH 进入您的实例 -> sudo vi /etc/mongod.conf -> set bindIp: 0.0.0.0 -> save -> sudo service mongod restart

After that you will be able to connect into you remote mongo instance with mongo --host YOUR_INSTANCE_IP

之后,您将能够使用 mongo --host YOUR_INSTANCE_IP 连接到远程 mongo 实例

回答by Bijendra

Try to access xx-xx-xx-xx-xx.compute-1.amazonaws.com:27017 from browser, if it works then ssh into the instance.

尝试从浏览器访问 xx-xx-xx-xx-xx.compute-1.amazonaws.com:27017,如果它有效,则 ssh 进入实例。

  • Stop the mongodb server
  • Remove the file /var/lib/mongodb/mongod.lock
  • 停止mongodb服务器
  • 删除文件 /var/lib/mongodb/mongod.lock

start the mongodb server again, and check if typing mongotakes into a mongo shell or gives any error.

再次启动 mongodb 服务器,并检查输入是否mongo进入 mongo shell 或出现任何错误。

If the error is present, exit from the instance and again ssh to check for $mongo, if it opens a mongo shell. Try a reboot after this changes if the error still exists after the above changes.

如果存在错误,请退出实例并再次 ssh 以检查$mongo是否打开了 mongo shell。如果上述更改后错误仍然存​​在,请在此更改后尝试重新启动。

The only concern here is, before accessing it from any application. Mongodb should work independently in the terminal.

这里唯一的问题是,在从任何应用程序访问它之前。Mongodb 应该在终端中独立工作。

回答by Confused

EC2 controls access using security group, so make sure that box accessing mongo db host is in the security groupallowed to access this box and portis enabled for the same security group.

EC2 使用安全组控制访问,因此请确保访问 mongo db 主机的框在security group允许访问此框的范围内,并port为同一安全组启用。

Another problem can be iptables. Check sudo service iptables statuson the mongodb box and see what are the rules there.

另一个问题可能是iptables。检查sudo service iptables statusmongodb 框,看看那里的规则是什么。

Make sure there you enable appropriate ports as per securityrequirement of the box.

确保security根据盒子的要求启用适当的端口。