简体   繁体   English

如何连接到MongoDB EC2实例

[英]How to connect to MongoDB EC2 instance

we have an EC2 MongoDB 2.4 instance from Amazon MarketPlace. 我们有来自Amazon MarketPlace的EC2 MongoDB 2.4实例。 when i try to access it from my computer using the mongo command like so: 当我尝试使用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. 我可以使用ssh连接到远程实例,因此可以访问它。
  • the port is the default port 27017. 端口是默认端口27017。
  • mongod is running and working on the remote instance. mongod正在运行并正在处理远程实例。

any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗?

As frisky said, you have to open the port 27017 at the EC2 Security Group Console. 如弗里斯基所说,您必须在EC2安全组控制台打开端口27017。 (To know more about how to do that: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html ) (要了解有关如何执行此操作的更多信息,请访问: http//docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

But you also need to change the bind_ip variable at the /etc/mongodb.conf file. 但是您还需要在/etc/mongodb.conf文件中更改bind_ip变量。 You need to comment the line or assign the ip that will be able to connect the DB to it. 您需要注释该行或分配能够将DB连接到它的IP。

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

Amazon created mongo security group without 27017 open. 亚马逊创建了mongo安全组,没有打开27017。 opening port 27017 to my pc fixed the issue. 打开端口27017到我的电脑修复了这个问题。

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 在端口27017 VPC上打开我的VPC的入站rould - >安全 - >安全组 - >选择实例的TCP路由 - >单击“编辑规则” - >添加类型为“自定义TCP规则”的规则,协议:TCP,端口范围:27017,来源:自定义0.0.0.0/0 - >保存
    1. SSH into your instance -> sudo vi /etc/mongod.conf -> set bindIp: 0.0.0.0 -> save -> sudo service mongod restart 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实例

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 停止mongodb服务器
  • Remove the file /var/lib/mongodb/mongod.lock 删除文件/var/lib/mongodb/mongod.lock

start the mongodb server again, and check if typing mongo takes 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. 如果存在错误,请退出实例并再次ssh检查$mongo ,如果它打开mongo shell。 Try a reboot after this changes if the error still exists after the above changes. 如果在上述更改后错误仍然存​​在,请在此更改后尝试重新启动。

The only concern here is, before accessing it from any application. 这里唯一关心的是,从任何应用程序访问它之前。 Mongodb should work independently in the terminal. Mongodb应该在终端独立工作。

EC2 controls access using security group, so make sure that box accessing mongo db host is in the security group allowed to access this box and port is enabled for the same security group. EC2使用安全组控制访问,因此请确保访问mongo db host的框位于允许访问此框的security group中,并且为同一安全组启用了port

Another problem can be iptables . 另一个问题可能是iptables Check sudo service iptables status on the mongodb box and see what are the rules there. 检查mongodb框上的sudo service iptables status ,看看那里的规则是什么。

Make sure there you enable appropriate ports as per security requirement of the box. 确保根据包装盒的security要求启用适当的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM