简体   繁体   English

连接到特定 mongodb 数据库时的身份验证错误

[英]Authentication error when connecting to specific mongodb database

I am currently using nodejs with mongodb native driver.我目前正在使用带有 mongodb 本机驱动程序的 nodejs。 So my mongodb has been set with admin auth with root role, so I can log in using robomongo or command line just fine.所以我的 mongodb 已经设置了具有 root 角色的管理员身份验证,所以我可以使用 robomongo 或命令行登录就好了。 So back to my project, I m able to connect to mongodb just fine if i set my connection string:所以回到我的项目,如果我设置我的连接字符串,我可以连接到 mongodb 就好了:

mongodb://admin:password@localhost:27017/

However, if I use this connection string:但是,如果我使用这个连接字符串:

mongodb://admin:password@localhost:27017/specificdb

It return as:它返回为:

MongoError: Authentication failed MongoError:身份验证失败

I am able to access the db using command and robomongo, is there anything I can do?我可以使用命令和 robomongo 访问数据库,有什么可以做的吗? I have added the admin user under the db, but still got the same problem.我已经在数据库下添加了管理员用户,但仍然遇到同样的问题。

The database you specify is the one you authenticate on. 您指定的数据库是您进行身份验证的数据库。 If the user is not known/has no roles for that database it cannot authenticate to it. 如果该用户未知/对该数据库没有任何角色,则无法对其进行身份验证。

https://docs.mongodb.com/manual/reference/connection-string/ https://docs.mongodb.com/manual/reference/connection-string/

What you can do is either create the (or a new) user for that database or use an authentication database parameter. 您可以为该数据库创建(或新)用户,或者使用身份验证数据库参数。

this worked for me:这对我有用:

first solution on connection:关于连接的第一个解决方案

mongoose.connect('mongodb://....', {
// add this line 
authSource:"admin",
....
})

or second solution add to the uri "?authSource=admin":或第二个解决方案添加到 uri "?authSource=admin":

mongodb://user:password@host/yourDB?authSource=admin

** to note that I am using mongoose and this is the documentation specefic statement and the url: ** 请注意,我使用的是 mongoose,这是文档特定声明和 url:

authSource - The database to use when authenticating with user and pass. authSource - 与用户进行身份验证时使用的数据库并通过。 In MongoDB, users are scoped to a database.在 MongoDB 中,用户的范围仅限于数据库。 If you are getting an unexpected login failure, you may need to set this option.如果您遇到意外的登录失败,您可能需要设置此选项。

https://mongoosejs.com/docs/connections.html#options https://mongoosejs.com/docs/connections.html#options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM