MongoDB用戶權限管理講解
mongodb在2.4***版本中對用戶權限管理做了全新的調(diào)整,把權限細化了,增強了安全性,越來越像mysql的權限管理了。
一、2.4之前的版本用戶管理
1、創(chuàng)建某個數(shù)據(jù)庫的管理用戶
1.1、進入weiw數(shù)據(jù)庫:
>use weiw;
1.2、添加用戶(讀寫權限,readOnly-->false):
>db.addUser("java","java");默認是擁有weiw數(shù)據(jù)庫所有權限
>db.addUser("java1","java1",true);擁有這個數(shù)據(jù)庫的只讀權限
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、創(chuàng)建超級用戶
2.1、進入admin數(shù)據(jù)庫
>use admin
2.2、在admin中創(chuàng)建的所有用戶都是超級用戶,可以操作任何的數(shù)據(jù)庫
>db.addUser("admin","admin");默認是擁有所有數(shù)據(jù)庫所有權限
>db.addUser("admin1","admin",true);擁有所有數(shù)據(jù)庫的只讀權限
二、2.4之后的版本對用戶權限管理這塊做了改進,越來越像mysql了,建議大家使用2.4之后的權限管理。
2.4版本的權限管理主要分為:數(shù)據(jù)庫的操作權限、數(shù)據(jù)庫用戶的管理權限、集群的管理權限,建議由超級用戶在admin數(shù)據(jù)庫中管理這些用戶。不過依然兼容2.4版本之前的用戶管理方法。
1、進入admin數(shù)據(jù)庫
>admin
2、添加一個對app數(shù)據(jù)庫有只讀權限,對app_log擁有讀寫權限的用戶app,但是不對admin數(shù)據(jù)庫有任何操作權限,不能添加任何用戶。
>db.addUser({
user: "app",
pwd:'1q2w3e4r',
roles:[],
otherDBRoles:
{
app: [ "read" ],
app_log: [ "readWrite" ]
}
})
>db.addUser({user:'app',pwd:'1q2w3e4r',roles:["readWrite"]})#對所在數(shù)據(jù)庫有讀寫權限
3、查看用戶
> db.system.users.find()
{ "_id" : ObjectId("528ac7d4bf62beb8249db527"), "user" : "app", "pwd" : "c5be065694f328e0ac6629e846d32e0f", "roles" : [ ], "otherDBRoles" : { "app" : [ "read" ], "app_log" : [ "readWrite" ] } }
添加用戶時的user就是用戶名字,pwd就是密碼,roles指定用戶所擁有的權限,otherDBRoles指的是除roles之外對其他數(shù)據(jù)庫所擁有的權限,格式就是個字典。
三、以下是roles中的權限說明:
read 指定數(shù)據(jù)庫的只讀權限,擁有以下權限:
aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats
count,dataSize,dbHash,dbStats,distinct,filemd5
geoNear,geoSearch,geoWalk,group
mapReduce (inline output only.),text (beta feature.)
readWrite 擁有指定數(shù)據(jù)庫的讀寫權限,除了具有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 指定數(shù)據(jù)庫的管理權限
clean,collMod,collStats,compact,convertToCapped
create,db.createCollection(),dbStats,drop(),dropIndexes,ensureIndex()
indexStats,profile,reIndex,renameCollection (within a single database.),validate
userAdmin 指定數(shù)據(jù)庫的用戶管理權限
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 任何數(shù)據(jù)庫的只讀權限(和read相似)
readWriteAnyDatabase 任何數(shù)據(jù)庫的讀寫權限(和readWrite相似)
userAdminAnyDatabase 任何數(shù)據(jù)庫用戶的管理權限(和userAdmin相似)
dbAdminAnyDatabase 任何數(shù)據(jù)庫的管理權限(dbAdmin相似)
詳細的可以參看官方文檔:http://docs.mongodb.org/manual/reference/method/db.addUser/
本文出自 “王偉” 博客,請務必保留此出處http://wangwei007.blog.51cto.com/68019/1328186