Home  >  Article  >  Database  >  mongodb用户权限管理讲解

mongodb用户权限管理讲解

WBOY
WBOYOriginal
2016-06-07 17:37:51905browse

mongodb在2.4最新版本中对用户权限管理做了全新的调整,把权限细化了,增强了安全性,越来越像mysql的权限管理了。一、2.4之前的版本用户管理1、创建某个数据库

mongodb在2.4最新版本中对用户权限管理做了全新的调整,把权限细化了,增强了安全性,越来越像mysql的权限管理了。


一、2.4之前的版本用户管理

1、创建某个数据库的管理用户


1.1、进入weiw数据库:

>use weiw;


1.2、添加用户(读写权限,readOnly-->false):

>db.addUser("java","java");默认是拥有weiw数据库所有权限

>db.addUser("java1","java1",true);拥有这个数据库的只读权限


1.3、查看一下所有的用户

>db.system.users.find();


{ "_id" : ObjectId("4e02a89fb841deb5fda3e5e2"), "user" : "java", "readOnly" : fa

lse, "pwd" : "59cf7cc156874cbd35cb00869126f569" }


1.4、删除用户

>db.system.users.remove({user:"java1"});

>db.removeUser("java1");


2、创建超级用户

2.1、进入admin数据库

>use admin


2.2、在admin中创建的所有用户都是超级用户,可以操作任何的数据库

>db.addUser("admin","admin");默认是拥有所有数据库所有权限

>db.addUser("admin1","admin",true);拥有所有数据库的只读权限


二、2.4之后的版本对用户权限管理这块做了改进,越来越像mysql了,建议大家使用2.4之后的权限管理。


2.4版本的权限管理主要分为:数据库的操作权限、数据库用户的管理权限、集群的管理权限,建议由超级用户在admin数据库中管理这些用户。不过依然兼容2.4版本之前的用户管理方法。


1、进入admin数据库

>admin


2、添加一个对app数据库有只读权限,,对app_log拥有读写权限的用户app,但是不对admin数据库有任何操作权限,不能添加任何用户。

>db.addUser({

user: "app",

pwd:'1q2w3e4r',

roles:[],

otherDBRoles:

{

app: [ "read" ],

app_log: [ "readWrite" ]

}

})

>db.addUser({user:'app',pwd:'1q2w3e4r',roles:["readWrite"]})#对所在数据库有读写权限


3、查看用户

> db.system.users.find()

{ "_id" : ObjectId("528ac7d4bf62beb8249db527"), "user" : "app", "pwd" : "c5be065694f328e0ac6629e846d32e0f", "roles" : [ ], "otherDBRoles" : { "app" : [ "read" ], "app_log" : [ "readWrite" ] } }


添加用户时的user就是用户名字,pwd就是密码,roles指定用户所拥有的权限,otherDBRoles指的是除roles之外对其他数据库所拥有的权限,格式就是个字典。


三、以下是roles中的权限说明:


read 指定数据库的只读权限,拥有以下权限:


aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats

count,dataSize,dbHash,dbStats,distinct,filemd5

geoNear,geoSearch,geoWalk,group

mapReduce (inline output only.),text (beta feature.)


readWrite 拥有指定数据库的读写权限,除了具有read权限,还拥有以下权限:


cloneCollection (as the target database.),convertToCapped

create (and to create collections implicitly.)

drop(),dropIndexes,emptycapped,ensureIndex()

findAndModify,mapReduce (output to a collection.)

renameCollection (within the same database.)


read和readWrite只要就是对库中表的操作权限


dbAdmin 指定数据库的管理权限


clean,collMod,collStats,compact,convertToCapped

create,db.createCollection(),dbStats,drop(),dropIndexes,ensureIndex()

indexStats,profile,reIndex,renameCollection (within a single database.),validate


userAdmin 指定数据库的用户管理权限


clusterAdmin 集群管理权限(副本集、分片、主从等相关管理)


addShard,closeAllDatabases,connPoolStats,connPoolSync,_cpuProfilerStart

_cpuProfilerStop,cursorInfo,diagLogging,dropDatabase

enableSharding,flushRouterConfig,fsync,db.fsyncUnlock()

getCmdLineOpts,getLog,getParameter,getShardMap,getShardVersion

hostInfo,db.currentOp(),db.killOp(),listDatabases,listShards

logRotate,moveChunk,movePrimary,netstat,removeShard,unsetSharding

repairDatabase,replSetFreeze,replSetGetStatus,replSetInitiate

replSetMaintenance,replSetReconfig,replSetStepDown,replSetSyncFrom

resync,serverStatus,setParameter,setShardVersion,shardCollection

shardingState,shutdown,splitChunk,splitVector,split,top,touch


readAnyDatabase 任何数据库的只读权限(和read相似)

readWriteAnyDatabase 任何数据库的读写权限(和readWrite相似)

userAdminAnyDatabase 任何数据库用户的管理权限(和userAdmin相似)

dbAdminAnyDatabase 任何数据库的管理权限(dbAdmin相似)


详细的可以参看官方文档:


本文出自 “王伟” 博客,请务必保留此出处

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn