MongoDB + Java: credentials missing from user document

MongoDB connection testing fails in Java with the correct credentials for the existing database. A com.mongodb.CommandFailureException .

This may be a mismatch between the different versions of MongoDB, because I use the code written for version 2.6 and run it with version 3 . It is less likely to be between different versions of the MongoDB driver, because I use the same version ( mongo-java-driver 2.12.4 ) in which the code was tested.

What can I do to fix this error and make it compatible with several versions of MongoDB?

 String dbName = "test_db"; String host = "localhost"; Integer port = 27017; String username = "admin"; String password = "password"; static Logger log = Logger.getLogger(MongoTest.class.getName()); log.debug("DBNAME:"+dbName+"\nHOST:"+host+"\nPORT:"+port+"\nUSERNAME:"+username+"\nPASSWORD:"+password+"\n"); MongoCredential credential = MongoCredential.createMongoCRCredential(username, dbName, password.toCharArray()); List<MongoCredential> auths = new ArrayList<MongoCredential>(); auths.add(credential); MongoClient mongo = new MongoClient(new ServerAddress(host, port), auths); DB db = mongo.getDB(dbName); log.debug("DB:"+db); // not null DBCollection dbCollection = db.createCollection("__test__", new BasicDBObject()); // FAIL 

mongo.log :

 2015-12-04T15:55:36.148+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:7273 #79 (18 connections now open) 2015-12-04T15:55:36.242+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:7274 #80 (19 connections now open) 2015-12-04T15:55:36.256+0100 I ACCESS [conn80] authenticate db: test_db { authenticate: 1, user: "admin", nonce: "xxx", key: "xxx" } 2015-12-04T15:55:36.258+0100 I ACCESS [conn80] Failed to authenticate admin@test _db with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document 2015-12-04T15:55:36.265+0100 I NETWORK [conn80] end connection 127.0.0.1:7274 (18 connections now open) 2015-12-04T15:55:36.282+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:7275 #81 (19 connections now open) 2015-12-04T15:55:36.301+0100 I NETWORK [conn79] end connection 127.0.0.1:7273 (18 connections now open) 2015-12-04T15:55:36.301+0100 I NETWORK [conn81] end connection 127.0.0.1:7275 (18 connections now open) 

An exception:

 com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18} at com.mongodb.CommandResult.getException(CommandResult.java:76) at com.mongodb.CommandResult.throwOnError(CommandResult.java:131) at com.mongodb.DBPort$NativeAuthenticator.authenticate(DBPort.java:652) at com.mongodb.DBPort.authenticate(DBPort.java:364) at com.mongodb.DBPort.checkAuth(DBPort.java:375) at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:209) at com.mongodb.DBCollectionImpl.sendWriteCommandMessage(DBCollectionImpl.java:477) at com.mongodb.DBCollectionImpl.writeWithCommandProtocol(DBCollectionImpl.java:427) at com.mongodb.DBCollectionImpl.insertWithCommandProtocol(DBCollectionImpl.java:387) at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:186) at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165) at com.mongodb.DBCollection.insert(DBCollection.java:93) at com.mongodb.DBCollection.insert(DBCollection.java:78) at com.mongodb.DBCollection.insert(DBCollection.java:120) at com.scs.setup.webapp.util.Mongo.isConnectionValid(Mongo.java:39) at com.scs.setup.webapp.controller.AjaxActionTest.isMongoConnectionValidDefault(AjaxActionTest.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
+1
source share
1 answer

The problem was fixed by adding the user to the Mongo CLI (3.0.7) correctly, I tried to run it in the admin database, while it was supposed to be in the test_db database:

 mongo --authenticationDatabase db_name -u username -p password (Connected to Mongo CLI client...) > use test-db; > db.createUser( { "user" : "admin", "pwd": "password", "roles" : [ { role: "root", db: "admin" }, { role: "readWrite", db: "admin" }, { role: "dbAdmin", db: "admin" }, { role: "readWrite", db: "test_db" }, { role: "dbAdmin", db: "test_db" } ] }); (Success...) > db.auth("admin","password") 1 
0
source

All Articles