目錄
開啟認證
# vim /etc/mongodb.conf auth = true
重載配置檔案
# /etc/init.d/mongod reload Stopping mongod: [ OK ] Starting mongod: [ OK ]
use admin;
db.createUser(
{
user: "admin",
pwd: "WkAFdmfVQpP1oAEkz4YVlMCDxkG36TAi",
roles: [ "dbAdmin", "dbOwner", "userAdmin" ]
}
);
注意,只有創建了超級管理後,下面的操作才會生效
MongoDB
use products
db.createUser(
{
user: "accountUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
早期版本
> use neo
switched to db neo
> db.addUser('neo','chen')
{
"user" : "neo",
"readOnly" : false,
"pwd" : "68ace374737253d87e0ec91d4fcb673d"
}
> db.system.users.find()
{ "_id" : ObjectId("4c481404b9db6474d2fcb76f"), "user" : "neo", "readOnly" : false, "pwd" : "68ace374737253d87e0ec91d4fcb673d" }
> db.auth('neo','chen')
1
Deleting Users 刪除用戶
To delete a user:
> db.getUsers();
[
{
"_id" : "test.monitor",
"user" : "monitor",
"db" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
]
}
]
> db.dropUser('monitor')
ture
> db.getUsers();
[ ]
早期版本
db.system.users.remove( { user: username } )