我为我的mongo安装创建了一个管理员用户,如下所示:
> use admin
> db.addUser( { user: "test",
pwd: "password",
roles: [ "dbAdminAnyDatabse",
otherDBRoles:
{
"otherTestDB": [ "readWrite" ]
}]
} )
当我尝试使用user:“test”和pwd:“password”连接到“otherTestDB”时使用robomongo或java驱动程序时,我收到错误的身份验证错误。
哪里出错?
答案 0 :(得分:1)
您已为admin db创建了一个userid,因此要使用该userid,您必须连接到admin db,而不是otherTestDB。 otherDBRoles文档控制对作为该用户连接到admin db的其他dbs的访问。因此,使用addUser指定它时,以下命令失败,因为用户是admin db而不是otherTestDB:
$ mongo otherTestDB --username test --password password --eval 'printjson(db.c.findOne())'
connecting to: otherTestDB
Thu Feb 27 10:45:20.722 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
而以下命令连接到admin db,然后通过getSiblingDB使用otherTestDB,则成功:
$ mongo admin --username test --password password --eval 'printjson(db.getSiblingDB("otherTestDB").c.findOne())'
connecting to: admin
{ "_id" : ObjectId("530f5d904dbd43cfb46aab5b"), "hello" : "world" }
如果您希望用户在使用该用户ID和密码连接到otherTestDB时进行身份验证,则需要将此用户单独添加到otherTestDB。
这有帮助吗?