Sushank Dahiwadkar
Sushank Dahiwadkar

Reputation: 93

Mongolab Authentication Failed Java

I am trying to connect to MongoLab Database with Java client. But not able to authenticate with Mongolab server with following code:

Mongo mongo = new Mongo("ds03456.mongolab.com", 51575);
DB db = mongo.getDB("sample");
boolean auth = db.authenticate("test", "test".toCharArray());

And i tried with same credentials with Node.js and it works fine:

var server = new Server('ds03456.mongolab.com', 51575);
var db = new Db('sample', server);
db.open({
      client.authenticate('test', 'test', function(err, success){
        //success = true.
      })
});

what could be the possible error?

Upvotes: 2

Views: 395

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48133

Mongolab is using MongoDB 3.0+ and you're using version 2.0 deprecated classes to authenticate. This should work:

MongoClientURI clientURI = new MongoClientURI("mongodb://user:pass@address:port/dbname");
MongoClient client = new MongoClient(clientURI);

Upvotes: 1

Related Questions