Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to mongo on EC2 Linux

I can't connect to my mongo instance running on the free tier AWS Amazon Linux 2 AMI. I've followed several pages online and I can confirm what I've done, yet I'm not able to connect to mongo remotely.

  • EC2 (with mongo installed) has a public IP
  • Security group (inbound) assigned with port = 27017, protocol = tcp
  • I can SSH onto the EC2 (and connect to mongo, SSH command shown below)
  • I've commented out the bind setting in the /etc/mongod.conf file (see example below)
  • restart the mongo service, command below
  • local machine - I can ping the EC2 public IP successfully
  • local machine - I try connecting to the EC2 using the terminal with "mongo 1.1.1.1" and I get the "connection failed message"

Is there anything else I can try? Thanks

SSH command

ssh -i my-key.pem [email protected]

changed mongo binding setting

bindIp: 0.0.0.0 

Restart mongo

sudo service mongod restart

Update - inbound rules

inbound rules

Error received when trying to connect to mongo from local machine

Terminal command $ mongo 1.1.1.1

[js] Error: couldn't connect to server 1.1.1.1:27017, connection attempt failed: SocketException: Error connecting to 1.1.1.1:27017 :: caused by :: Operation timed out :

Mongo configuration file (from EC2 machine)

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


security:
  authorization: 'enabled'

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
~                                   
like image 900
James Avatar asked Sep 11 '25 16:09

James


1 Answers

Don't comment the bindIp settings. If you comment the bindIp setting it will default to localhost. You won't be able to connect from outside network. Enable bindIp settings and set the value to 0.0.0.0.

bind_ip

Also, check your inbound rule IP range and port(27017).

Update the inbound rule to accept ipv4 addresses (0.0.0.0/0)

like image 134
Mani Avatar answered Sep 13 '25 07:09

Mani