MongoDB使用者管理

來源:互聯網
上載者:User

標籤:local   insert   create   字元   command   new   密碼驗證   code   str   

建立使用者

文法:
db.createUser(
{
user:<name_string>, #字串
pwd:<password_strin>, #字串
roles:[{role:<role_name>,db:<db_name>}] #數組
}
)

建立使用者:

> db.createUser(... {...     user:"root",...     pwd:"admin",...     roles:[{role:"root",db:"admin"}]... }... )

使用者驗證:

> db.auth("root","admin")1

查看使用者:

刪除使用者

db.dropUser(<user_name>) #刪除某個使用者,授受字串參數
db.dropAllUsers() #刪除當前庫的所有使用者

查詢所有使用者> db.getUsers()[    {        "_id" : "admin.root",        "user" : "root",        "db" : "admin",        "roles" : [            {                "role" : "root",                "db" : "admin"            }        ]    },    {        "_id" : "admin.sysadmin",        "user" : "sysadmin",        "db" : "admin",        "roles" : [            {                "role" : "root",                "db" : "admin"            }        ]    },    {        "_id" : "admin.test",        "user" : "test",        "db" : "admin",        "roles" : [            {                "role" : "root",                "db" : "admin"            }        ]    }]
刪除test使用者> db.dropUser("test")true
確認test使用者是否存在> db.getUser("test")null
刪除所有使用者> db.dropAllUsers()2
修改使用者密碼

要修改使用者密碼,要求使用者具有changePassword或changeOwnPassword的許可權,有以下兩種方式用來修改使用者密碼:
db.changeUserPassword(<user_name>,<new_password>)
db.updateUser(<user_name>,{update_object})

db.changeUserPassword()樣本:

[email protected]$ db.auth("root","admin")1

[email protected]$ db.changeUserPassword("root","123456")

[email protected]$ db.auth("root","admin")Error: Authentication failed.0
[email protected]$ db.auth("root","123456")1
[email protected]$ show dbsadmin  0.000GBlocal  0.000GBtest   0.000GBtest1  0.000GB

可以看到在修改root使用者的密碼後,原來的密碼驗證就失敗了,但是當前這個會話還是可以正常執行操作,新會話則需要用修改後的密碼進行驗證

db.updateUser()樣本:

[email protected]$ db.auth("root","123456")1

[email protected]$ db.updateUser("root",{pwd:"admin123"})

[email protected]$ db.auth("root","admin123")1
[email protected]$ db.auth("root","123456")Error: Authentication failed.0

修改使用者權限(角色):
修改使用者角色也是使用db.updateUser()函數來實現的
我們先建立一個測試使用者readtest,它只對test庫具備讀許可權:
db.createUser(
{
user:"readtest",
pwd:"123456",
roles:[{role:"read",db:"test"}]
}
)

[email protected]$ db.auth("readtest","123456")1
[email protected]$ use testswitched to db test
[email protected]$ show tablesgoodsusers
[email protected]$ db.goods.find(){ "_id" : ObjectId("5a7c5b7e83dba596ccad3ac0"), "sn" : "fhbowhnlerio12o47", "category" : "food" }
[email protected]$ db.goods.insert({"sn":"04t68gjsoe076","category" : "beauty"})WriteResult({    "writeError" : {        "code" : 13,        "errmsg" : "not authorized on test to execute command { insert: \"goods\", documents: [ { _id: ObjectId(‘5a8ef5aa3cdd503ad3903fcc‘), sn: \"04t68gjsoe076\", category: \"beauty\" } ], ordered: true }"    }})

可以看到這個使用者可以執行讀操作,寫操作是沒有許可權的,現在我們通過db.updateUser()來擴充它的許可權,記其具有讀寫權限。
[email protected]$ db.updateUser("readtest",{"roles":[{role:"readWrite",db:"test"}]})

[email protected]$ db.auth("readtest","123456")1
[email protected]$ use testswitched to db test
[email protected]$ db.goods.insert({"sn":"04t68gjsoe076","category" : "beauty"})WriteResult({ "nInserted" : 1 })

可以看到,當我們把readtest使用者的角色從read改成readWrite時,它就具有了對test庫的寫入權限。通過db.updateUser()我們可以實現使用者權限的放大和縮小

MongoDB使用者管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.