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("172.31.90.193", 27021);
It gives me the error:
INFO: Exception in monitor thread while connecting to server
172.31.90.193:27021 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 moreException 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=172.31.90.193:27021,
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:
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: 172.31.90.193 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
replication: replSetName: Midterm sharding: clusterRole: configsvr
## Enterprise-Only Options
#auditLog:
#snmp:
Note: I have not set any permission or add credentials in my admin db's collection.
Best Answer
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.