Unable to connect to MongoLab from java driver

516 views Asked by At

I have Mongolab account , there is only one user in that account.

I can connect to the DB using MongoChef tool, however when I try and connect to that DB from within Eclipse using the java driver 3.2.0 , I continuously get an authentication failure. I get the following error.I am on Java 7 .

EDIT Java Code used to connect to the MongoLab hosted MongoDB instance.

    String mongoURL = new StringBuilder().append("mongodb://").append(mongoUserName).append(":").append(mongoPassword).append("@").append(mongoServer).append(":").append(mongoPort).append("/").append(dbName).toString();
    System.out.println(" The Mongo URL is " +mongoURL);
    MongoClientURI uri = new MongoClientURI(mongoURL);
    MongoClient mongo = new MongoClient(uri);

Exception received when trying to connect.

 com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
 waiting for a server that matches
 ReadPreferenceServerSelector{readPreference=primary}. Client view of
 cluster state is {type=UNKNOWN,
 servers=[{address=ds061974.mongolab.com:61974, type=UNKNOWN,
 state=CONNECTING, exception={com.mongodb.MongoSecurityException:
 Exception authenticating MongoCredential{mechanism=null,
 userName='xxxx', source='hidden', password=<hidden>,
 mechanismProperties={}}}, caused by
 {com.mongodb.MongoCommandException: Command failed with error 18:
 'Authentication failed.' on server ds061974.mongolab.com:61974. The
 full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication
 failed." }}}]
1

There are 1 answers

0
Mohsen ZareZardeyni On

try this code:

ServerAddress addr = new ServerAddress(mongoServer, mongoPort);
MongoCredential credential = MongoCredential.createMongoCredential(
        mongoUserName, mongoPassword, dbName);

MongoClient mongoClient = new MongoClient(addr, Arrays.asList(credential), null);