Failed to connect MongoDB to Amazon EC2 instance using java

I am developing an android application using mongoDB for data storage, currently I am using Mongolab sandbox for data storage. I'm going to move the database to an Amazon Ec2 instance , and I configured it through the MMS service.

I created a database user, and I can connect to the database using putty and perform CURD operations. Right now, I'm learning how to connect to the Equator Ec2 from MongoDB's JAVA driver.

I am using this code right now to connect to the database

MongoClientURI uri = new MongoClientURI("mongodb://" + myUserName + ":" + myPassword + "@" + DB + "/" + DB_NAME + "?authMechanism=MONGODB-CR"); MongoClient mongoClient = new MongoClient(uri); MongoDatabase blogDatabase = mongoClient.getDatabase(DB_NAME); MongoCollection<Document> usersCollection = blogDatabase.getCollection("users"); 

But I can not connect the database. It gives me an error

 Aug 27, 2015 9:12:14 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster created with settings {hosts=[ 5fdef03a@host :port], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} Aug 27, 2015 9:12:14 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Exception in monitor thread while connecting to server 5fdef03a@host :port com.mongodb.MongoSocketException: 5fdef03a@host at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:37) at com.mongodb.connection.SocketStream.open(SocketStream.java:53) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:96) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) at java.lang.Thread.run(Unknown Source) Caused by: java.net.UnknownHostException: 5fdef03a@host at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source) at java.net.InetAddress.getAddressesFromNameService(Unknown Source) at java.net.InetAddress.getAllByName0(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source) at java.net.InetAddress.getByName(Unknown Source) at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ... 5 more Aug 27, 2015 9:12:17 PM com.mongodb.diagnostics.logging.JULLogger log INFO: No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{ address=5fdef03a@host :port, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: 5fdef03a@host }, caused by {java.net.UnknownHostException: 5fdef03a@host }}]}. Waiting for 30000 ms before timing out 

Note. I did not disclose the host and port values ​​in the error.

Can someone help me figure out what to do even if all the credentials are correct. In most of the posts I found, they asked to create a REST API for the connection, but I think there is some other way to connect with the JAVA MongoDB driver itself. Else has another way to access the database.

+5
source share
1 answer

Check the following, it may help you

1) The port through which you access mongodb may be blocked by your network security.

2) Mongo Server is not running on the SSH server host

3) The user privileges you are trying to connect do not fit

4) PEM file is invalid

Useful links:

How to connect to MongoDB on EC2 using Java driver

http://docs.mongodb.org/manual/faq/diagnostics/#Troubleshooting-Socketerrorsinshardedclustersandreplicasets

+1
source

All Articles