I'm developing an android application which uses the mongoDB for data storage, currently I'm using Mongolab sandbox to store data's. I'm thinking of moving the database to the Amazon Ec2 Instance and I've configured it through the MMS service.
I've Created a database user and I can able to connect to the database via putty and perform CURD operations. Right now I'm exploring how to connect to the Ec2 Instance from MongoDB JAVA driver.
I'm 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't connect the database. It gives me a 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 haven't disclosed the host & port values in the error.
Can any one help me to figure out what to do even though all the credentials are correct. In most of the post which I found they asked to create a REST API to connect, but I think that there should any other way to connect with the MongoDB JAVA driver itself. Else is there any other way to access the database.
Check the following, it might help you
1) The port thru which you are accessing mongodb might be blocked by your network security.
2) Mongo Server is not running on the SSH server host
3) The User privileges thru which you are trying to connect is not appropriate
4) PEM file is not valid
Useful links:
How to connect to MongoDB on EC2 using Java driver
http://docs.mongodb.org/manual/faq/diagnostics/#Troubleshooting-Socketerrorsinshardedclustersandreplicasets
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With