开启认证
# 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 } )
增加索引:1(ascending),-1(descending)
增加索引
db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
索引子对象
db.logging.users.ensureIndex({address.city:1})
查看索引信息
db.logging.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "logging.logging", "name" : "_id_" } ]
db.logging.users.getIndexKeys() [ { "_id" : 1 }, { "name" : 1 } ]
根据索引名删除索引
> db.logging.users.dropIndex('name_1') { "nIndexesWas" : 2, "ok" : 1 } > db.logging.users.getIndexKeys() [ { "_id" : 1 } ]
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。