2017-06-13 36 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 '密碼'