Created
April 13, 2015 16:39
-
-
Save dmregister/346aa4402a6fae4f2d54 to your computer and use it in GitHub Desktop.
mongodb auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//First user created | |
use admin | |
db.createUser( | |
{ | |
user: "admintest", | |
pwd: "testing", | |
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] | |
} | |
) | |
//second user created | |
use admin | |
db.createUser( | |
{ | |
user: "test_dbuser", | |
pwd: "testpass", | |
roles: [ | |
{ role: "readWrite", db: "testingdb" } | |
] | |
} | |
) | |
//sample connection string | |
mongodb://test_dbuser:testpass@localhost:27017/testingdb.test | |
//rebuilds the connection | |
MongoCredential credential = MongoCredential.createMongoCRCredential(mongoClientUri.getUsername(), "admin", mongoClientUri.getPassword()); | |
String mongoHost = mongoClientUri.getHosts().get(0); | |
MongoClient mongoClient = new MongoClient(new ServerAddress(mongoHost), Arrays.asList(credential)); | |
//Always throws exception | |
com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment