How to connect to MongoDB EC2 instance

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

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:

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.

any suggestions on how to fix this?

回答1:

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)

But it you also need to change the bind_ip variable 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.



回答2:

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

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

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

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



回答3:

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



回答4:

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

  • Stop the mongodb server
  • Remove the file /var/lib/mongodb/mongod.lock

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

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.

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



回答5:

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.

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

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!