2.2. MongoDB 管理

2.2.1. Security and Authentication

开启认证

# vim /etc/mongodb.conf
auth = true
		

重载配置文件

# /etc/init.d/mongod reload
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]
		
2.2.1.1. 超级管理员
Database Administration Roles
			
use admin;
db.createUser(
   {
     user: "admin",
     pwd: "WkAFdmfVQpP1oAEkz4YVlMCDxkG36TAi",
     roles: [ "dbAdmin", "dbOwner", "userAdmin" ]
   }
);
			
			
2.2.1.2. 数据库访问用户

注意,只有创建了超级管理后,下面的操作才会生效

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
			
			
2.2.1.3. 数据库监控用户
			
db.createUser(
   {
     user: "monitor",
     pwd: "netkiller",
     roles: [ "clusterMonitor"]
   }
)			
			
			
2.2.1.4. 删除用户

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 } )
			
			
2.2.1.5. 更新角色
			

db.updateUser( "monitor",
   {
     roles: [ "read", "clusterMonitor" ]
   }
)
			
			

2.2.2. Unique Indexes

增加索引:1(ascending),-1(descending)

2.2.2.1. 索引管理

增加索引

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 } ]
		
		
2.2.2.1.1. 唯一索引
db.members.createIndex( { "user_id": 1 }, { unique: true } )
			
			
> db.apple.createIndex({"devicetoken":1},{unique: true})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
			





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值