5

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.

S A R
  • 146
  • 3
  • 20
  • It's pitty really, as any good answer would explain to you why you should not be connecting your mobile device directly to a database. You and your application will be better off for constructing a server side API that your mobile app can talk to where it in turn talks to the database. This gives you much better control and security than connect remote clients everywhere directly to a database. – Blakes Seven Sep 01 '15 at 10:50
  • The error itself says something about the hostname not being correct. But besides that I just wanted to point that if you are using MongoDB 3.0+ and created the user in 3.0+ then you need to use the authentication scheme SCRAM-SHA-1 because they removed MONGODB-CR completely. If the user was created in a version prior to 3.0 then it stil has the authentication scheme MONGODB-CR. http://docs.mongodb.org/manual/core/authentication/ – Liviu Costea Sep 02 '15 at 07:17

1 Answers1

1

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

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34