本文共 2249 字,大约阅读时间需要 7 分钟。
开启认证
# vim /etc/mongodb.confauth = true
重载配置文件
# /etc/init.d/mongod reloadStopping mongod: [ OK ]Starting mongod: [ OK ]
use admin;db.createUser( { user: "admin", pwd: "WkAFdmfVQpP1oAEkz4YVlMCDxkG36TAi", roles: [ "dbAdmin", "dbOwner", "userAdmin" ] });
注意,只有创建了超级管理后,下面的操作才会生效
MongoDB
use productsdb.createUser( { user: "accountUser", pwd: "password", roles: [ "readWrite", "dbAdmin" ] })
早期版本
> use neoswitched 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
db.createUser( { user: "monitor", pwd: "netkiller", roles: [ "clusterMonitor"] })
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 } )
db.updateUser( "monitor", { roles: [ "read", "clusterMonitor" ] })
增加索引: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 } ]
db.members.createIndex( { "user_id": 1 }, { unique: true } )
> db.apple.createIndex({"devicetoken":1},{unique: true}){ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}