【问题标题】:"not authorized" error while querying from mongo shell, working fine from RoboMongo [duplicate]从 mongo shell 查询时出现“未授权”错误,从 RoboMongo 可以正常工作 [重复]
【发布时间】: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


【解决方案1】:

奇怪,在尝试列出集合或查询之前,我必须在 mongo shell 中输入 use test_db 来选择数据库。即使我的登录脚本中有--authenticationDatabase 参数,mongo 也不会自动为您选择提到的数据库。你必须自己做。

【讨论】:

    【解决方案2】:

    --authenticationDatabase 仅指定 MongoDB 用户需要通过身份验证才能登录的数据库。换句话说,是创建用户的数据库。

    由于您创建的用户只能访问test_db,因此在使用您创建的用户登录 MongoDB 时,您将无权在其他数据库(如默认测试数据库)上运行命令。

    如果你想在登录后切换到test_db,你应该使用这个命令:

    mongo test_db -u 'test_user' --authenticationDatabase 'test_db' -p '密码'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多