Im trying to connect to my MongoDB that is on my EC2 instance. I am able to connect to using the public dns with this statement:

mongoClient = new MongoClient("ec2-3-92-197-163.compute-1.amazonaws.com", 27021);

but if I tried to replace the "ec2-3-92...." with the private IP address, as such:

mongoClient = new MongoClient("", 27021);

It gives me the error:

INFO: Exception in monitor thread while connecting to server com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) at java.base/java.lang.Thread.run(Thread.java:844) Caused by: java.net.SocketTimeoutException: connect timed out at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400) at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243) at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402) at java.base/java.net.Socket.connect(Socket.java:591) at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ... 3 more

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}] at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:182) at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41) at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136) at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94) at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249) at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172) at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132) at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86) at com.mongodb.client.internal.MongoIterableImpl.forEach(MongoIterableImpl.java:110) at App.main(App.java:46)

Is there something I'm missing that is preventing me from connecting to mongo using the private IP address?

I have set up the inbound rules as such:

enter image description here

and this is what my mongod.conf file looks like:

  # 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:  # Enter,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.



replication:   replSetName: Midterm sharding:   clusterRole: configsvr
## Enterprise-Only Options



Note: I have not set any permission or add credentials in my admin db's collection.

  • 1
    From which machine you are connecting to the MongoDB in aws EC2? Both the machines are in same VPC or connected through internet?
    – Mani
    Apr 8, 2019 at 6:10
  • In trying to connect my local machine to the EC2 instance
    – tom dinh
    Apr 8, 2019 at 15:20

1 Answer 1


Since you are trying to connect to the MongoDB in EC2 instance from your local machine, the connection should be made via the public network (Internet).

If it is a public network you should use either Public IP or Elastic IP for the connection.

You cannot use Private IP to connect to MongoDB running in AWS EC2 via the public network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.