2017-06-13 62 views
0
工作正常

我创建了一个用户,"test_user"有关MongoDB的分贝"test_db"并授予readWrite作用如下“未授权”错误而从蒙戈外壳查询,从RoboMongo

db.createUser(
    { 
     user : "test_user", 
     pwd : "password", 
     roles : [ 
     { 
      role : "readWrite", 
      db : "test_db" 
     } 
    ] 
    } 
) 

蒙戈说:Successfully added user

一切都很好,我用RoboMongo连接到我的数据库,并能够创建和列出集合。

但是,当我连接使用shell从我的机器蒙戈如下

mongo -u 'test_user' --authenticationDatabase 'test_db' -p 'password' 

我无法列出或创建集合。 Mongo说

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

我缺少什么?

+0

奇怪的是,在尝试列出集合或查询之前,我必须在mongo shell中使用'test_db'。尽管我在登录脚本中使用了'--authenticationDatabase'参数,mongo并不会自动为您选择所提到的db。你必须自己做。 –

回答

0

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

0

--authenticationDatabase仅指定MongoDB用户需要进行身份验证以登录的数据库。换句话说,就是创建用户的数据库。

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

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

蒙戈TEST_DB -u 'test_user' --authenticationDatabase 'TEST_DB' -p '密码'