[英]MongoDB+Java: Credentials missing in the user document
Testing of MongoDB connection fails in Java with correct login credentials for an existing database. 使用现有数据库的正确登录凭证的Java中,MongoDB连接的测试失败。 A
com.mongodb.CommandFailureException
is displayed. 显示
com.mongodb.CommandFailureException
。
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 . 这可能是MongoDB的不同版本之间的不匹配,因为我使用的是为2.6版本编写的代码,并与版本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. 不太可能出现在不同版本的MongoDB驱动程序之间,因为我使用的是与测试代码相同的版本(
mongo-java-driver 2.12.4
)。
What can I do to fix this error and make it compatible with a range of MongoDB versions? 我该如何解决此错误并使它与一系列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
: 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)
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(3.0.7)CLI中正确添加用户,解决了该问题,我尝试在
admin
数据库上运行它,而应该在test_db
数据库上运行它:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.