尝试保存到 mongodb 时身份验证失败 [英] Authentication failure while trying to save to mongodb

查看:31
本文介绍了尝试保存到 mongodb 时身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以保存到本地运行的 mongo 实例:

I have following code to save to a local running mongo instance:

MongoCredential credential = MongoCredential.createCredential("myuser", "mydatabase", "mypassword".toCharArray());

MongoClient mongo = MongoClients.create(MongoClientSettings.builder()
                    .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new 
ServerAddress("localhost", 27017))))
                    .credential(credential)
                    .build());
MongoDatabase database = mongo.getDatabase("mydatabase");
MongoCollection<Document> collection = database.getCollection("mycollection");
collection.insertOne(document);

我在 mongo.exe shell 中使用 db.createUser() 命令为上面代码中使用的 usernmae/password 创建了一个用户,这些与我在安装 mongodb 时提供的凭据相同.

I have created a user for usernmae/password used in code above using db.createUser() command in mongo.exe shell and these are same credentials I provided while installing mongodb.

db.createUser(
{   user: "myuser",
    pwd: "mypassword",

    roles:[{role: "userAdminAnyDatabase" , db:"admin"}]})

但代码失败:

Exception in thread "main" com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='myuser', source='mydatabase', password=<hidden>, mechanismProperties={}}

我在这里遗漏了什么?

推荐答案

您在哪里,即在哪个数据库中创建了用户?通常,用户是在数据库 admin 中创建的.当您连接到 MongoDB 时,您应该始终指定身份验证数据库和您喜欢使用的数据库.

Where, i.e. in which database did you create the user? Typically users are created in database admin. When you connect to a MongoDB then you should always specify the authentication database and the database you like to use.

默认值有点令人困惑并且不是很一致,请参阅此表以获取概览:

The defaults are a bit confusing and not really consistent, see this table to get an overview:

+----------------------------------------------------------------------------------------+
|Connection string                                           | Authentication | Current  |
|                                                            | database       | database |
+----------------------------------------------------------------------------------------+
|mongo -u <...> -p <...> --authenticationDatabase admin myDB |     admin      |   myDB   |
|mongo -u <...> -p <...> myDB                                |     myDB       |   myDB   |
|mongo -u <...> -p <...> --authenticationDatabase admin      |     admin      |   test   |
|mongo -u <...> -p <...> localhost:27017                     |     test       |   test   |
|mongo -u <...> -p <...> --host localhost:27017              |     admin      |   test   |
|mongo -u <...> -p <...>                                     |     admin      |   test   |
+----------------------------------------------------------------------------------------+

如果你喜欢使用 URI 格式的连接字符串,它会对应这些:

If you like to use Connection string in URI format, it would correspond to these ones:

mongodb://<username>:<password>@hostname/myDB?authSource=admin
mongodb://<username>:<password>@hostname/myDB
mongodb://<username>:<password>@hostname?authSource=admin
mongodb://<username>:<password>@hostname

我猜您在 admin 数据库中创建了用户,但是由于您在连接时没有指定 authenticationDatabase,Mongo 将其默认为 mydatabase 失败,因为用户没有存在于数据库 mydatabase 中.

I guess you created the user in admin database but as you don't specify authenticationDatabase while connecting, Mongo defaults it to mydatabase where it fails, because user does not exist in database mydatabase.

这篇关于尝试保存到 mongodb 时身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆