如何在 MongoDB v3 中建立使用者?
要在 MongoDB v3 中建立使用者,請使用 createUser() 方法。該方法能讓你建立使用者,在建立過程中還需要新增使用者、密碼和角色。這些角色會分配許可權。語法如下所示 −
use admin
db.createUser(
{
user: “yourUserName",
pwd: "yourPassword",
roles: [ { role: "yourPermission", db: "yourDatabase" } ]
}
);讓我們實現上述語法來在 MongoDB v3 中建立使用者 −
> use admin
switched to db admin
> db.createUser(
... {
... user: "Robert",
... pwd: "robert",
... roles: [ { role: "readWrite", db: "sample" } ]
... }
... );在上文中,我們為新使用者設定如下內容 −
User: Robert
Password: robert
Roles: readWrite
This will produce the following output:
Successfully added user: {
"user" : "Robert",
"roles" : [
{
"role" : "readWrite",
"db" : "sample"
}
]
}我們已經成功地在上方建立了一個新使用者 “Robert”。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP