问题
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...
回答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...
回答2:
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
来源:https://stackoverflow.com/questions/22045285/java-mongodb-authentication