gpt4 book ai didi

java - MongoDB+Java : Credentials missing in the user document

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:47 25 4
gpt4 key购买 nike

使用现有数据库的正确登录凭据在 Java 中测试 MongoDB 连接失败。显示 com.mongodb.CommandFailureException

这可能是不同版本的 MongoDB 之间的不匹配,因为我使用的是为版本 2.6 编写的代码,并使用版本 3 运行它。不同版本的 MongoDB 驱动程序之间出现这种情况的可能性较小,因为我使用的是测试代码的同一版本 (mongo-java-driver 2.12.4)。

如何修复此错误并使其与一系列 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)

异常(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)

最佳答案

通过在 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

关于java - MongoDB+Java : Credentials missing in the user document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com