How to connect to MongoDB EC2 instance

前端 未结 6 740
失恋的感觉
失恋的感觉 2020-12-31 02:05

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.c         


        
相关标签:
6条回答
  • 2020-12-31 02:13

    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

    0 讨论(0)
  • 2020-12-31 02:14

    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

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

    0 讨论(0)
  • 2020-12-31 02:22

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

    0 讨论(0)
  • 2020-12-31 02:22

    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.

    0 讨论(0)
  • 2020-12-31 02:30

    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 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.

    0 讨论(0)
  • 2020-12-31 02:36

    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.

    0 讨论(0)
提交回复
热议问题