1

I saw a lot of tutorials showing how to auth in mongodb using Java

I have my mongodb configured with auth enabled. In console I use

use admin

db.auth("myUser","newPassword") and works well.

and in java In every sites visited the code is

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("myBase");

boolean auth = db.authenticate("myUser", "newPassword".toCharArray());
    //auth is true if everithing went well

The BIG problem is that code doesn't work for me and I don't know why. I tested changing explicitly to admin database.

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("admin");

boolean auth = db.authenticate("myUser", "newPassword".toCharArray());
DB db = mongo.getDB("myBase");
    //auth is true if everithing went well

This works BUT for me it is not a solution because I triying to use mongo with mule and the problem is that I can't be changing between db's for each auth which I must do.

WHY the first code works for all the people instead of me?

I realized what is the problem.

It seems that mongo work using user for each DB stored in the DB itself. I created the user in the same db and it works. It a very weird feature because I will have a lot of duplicated users in mongo, one for each DB that I have. Minus one for user managing in Mongo...

2 Answers 2

1

I realized what is the problem.

It seems that mongo work using user for each DB stored in the DB itself. I created the user in the same db and it works. It a very weird feature because I will have a lot of duplicated users in mongo, one for each DB that I have. Minus one for user managing in Mongo...

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to mention that possibility so I'm glad you found it. 2.6 is reworking auth a fair bit so it might not hurt to look at those upcoming changes.
0

That method is now deprecated. The new, preferred way is to us MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList) to create a client which will authenticate all connections to server

3 Comments

I don't think that would be the problem, because I'm using mule and internally it uses db.authenticate(username, password.toCharArray()) check github.com/mulesoft/mongo-connector/blob/master/src/main/java/… line 1257
Maybe could be a mongo config issue?
an error added but it isn't very important if you read carefully what is the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.