skip to Main Content

I created an AWS EC2 (ubuntu-20.04) instance and setup a mongo server as per https://www.mongodb.com/docs/v4.4/tutorial/install-mongodb-on-ubuntu/

(mongo server is running in default port 27017 and not secured by any username/password)

I am able to connect to it from within the VM (after connecting to VM by SSH), by running the terminal command:

mongo

I have configured EC2 instance’s Security Group’s inbound rule to access port 27017 from my local system’s IP.

however when I try to connect to it from my local system’s terminal by command:

 mongo --host "<public_ip_of_vm>:27017"

Its throwing error:

connecting to:
mongodb://<public_ip_of_vm>:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn’t connect to server <public_ip_of_vm>:27017, connection attempt
failed: SocketException: Error connecting to <public_ip_of_vm>:27017 :: caused
by :: Connection refused : connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6 exception: connect failed exiting with code 1

Not sure what’s causing this issue, I have checked the ufw rules too.
port 27017 is open.

2

Answers


  1. Did you modify ?

    /etc/mongod.conf

    Have a look at this tutorial, especially the step 2. Maybe this helps
    https://ianlondon.github.io/blog/mongodb-auth/

    Login or Signup to reply.
  2. Connection refused means you probably do not have a firewall problem. Connection timeout indicates a firewall issue.

    Since you can connect locally via localhost, the error indicates that the mongo process is only listening on localhost.

    Edit the file /etc/mogod.conf. The interesting line is bindIp.

    It should look like this for IPv4 only:

    bindIp: 0.0.0.0
    

    If you have IPv6 enabled

    bindIp: ::,0.0.0.0
    

    Warning: enable authentication first. You might be hacked faster than you might expect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search