【发布时间】:2017-06-13 13:57:20
【问题描述】:
我在 mongodb 上为 db "test_db" 创建了一个用户 "test_user" 并授予 readWrite 角色如下
db.createUser(
{
user : "test_user",
pwd : "password",
roles : [
{
role : "readWrite",
db : "test_db"
}
]
}
)
Mongo 说Successfully added user
一切顺利,我使用 RoboMongo 连接到我的数据库,并且能够创建和列出集合。
但是当我使用 shell 从我的机器连接到 mongo 时,如下所示
mongo -u 'test_user' --authenticationDatabase 'test_db' -p 'password'
我无法列出或创建集合。蒙哥说
E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
}
当我在 shell 中输入 show collections 时。
我错过了什么?
【问题讨论】:
-
奇怪,在尝试列出集合或查询之前,我必须在 mongo shell 中执行
use test_db。即使我的登录脚本中有--authenticationDatabase参数,mongo 也不会自动为您选择提到的数据库。你必须自己做。
标签: mongodb mongodb-roles