1

Testing of MongoDB connection fails in Java with correct login credentials for an existing database. A com.mongodb.CommandFailureException is displayed.

This can be a mismatch between different versions of MongoDB, because I'm using code that was written for version 2.6 and running it with version 3. It's less likely to be between different versions of MongoDB driver, because I'm using a the same version (mongo-java-driver 2.12.4) that the code was tested on.

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

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)

The 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)
Community
  • 1
  • 1
Peter G.
  • 7,816
  • 20
  • 80
  • 154
  • have you checked with version of Mongo ? – Harshil Dec 04 '15 at 09:11
  • i'm still looking in manual for version 3 of MongoDB what query actually gets called – Peter G. Dec 04 '15 at 09:15
  • well these 2 links says similar question. http://stackoverflow.com/questions/30793784/mongo-server-accepts-credentials-from-shell-but-not-from-java-scala-interface http://stackoverflow.com/questions/18216712/cannot-authenticate-into-mongo-auth-fails – Harshil Dec 04 '15 at 09:19
  • they are probably asking the same, but a good solution with Java code was not proposed yet – Peter G. Dec 04 '15 at 09:35

1 Answers1

0

The problem was fixed by properly adding the user in Mongo (3.0.7) CLI, I tried running it on admin database, while it should have been on 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
Peter G.
  • 7,816
  • 20
  • 80
  • 154