MongoDB——權限管理
MongoDB默認是沒有權限驗證的,但生產環境中,沒有權限控制是很不安全的。
我們先不詳談太多概念,直接動手創建兩個典型的賬號:
- 超級管理員,類似sql server的sa賬號或者mysql的root賬號
- 單個數據庫的讀寫權限賬號
創建超級管理員
-
在沒開啟權限驗證機制的時候,我們在“命令指示符”窗口用mongo打開MongoDB的shell。
-
使用“use”命令,切換到admin數據庫,MongoDB用admin的system.users集合保存着用戶信息。
> use admin switched to db admin >
-
使用db.createUser方法來創建用戶,具體的說明,請參考官網文檔
> db.createUser( ... { ... user: "sa", ... pwd: "123", ... roles: [ { role: "__system", db: "admin" } ] ... } ... ) Successfully added user: { "user" : "sa", "roles" : [ { "role" : "__system", "db" : "admin" } ] } >
這樣我們就創建了一個賬號:sa,密碼:123,擁有“__system”角色的權限,關於“__system”角色的具體說明,請參考官網文檔
警告:不要把擁有“__system”角色的賬號分配給系統程序使用
開啟權限驗證
在MongoDB學習——基礎入門這一篇中,我們將MongoDB安裝到window服務的時候,創建了一個“mongod.cfg"配置文件,現在我們給配置文件修改為如下:
systemLog:
destination: file
path: D:\Program Files\MongoDB\Server\3.0\log\mongod.log
storage:
dbPath: D:\Program Files\MongoDB\Server\3.0\db
# 開啟驗證
security:
authorization: enabled
重新啟動后,MongoDB的操作就需要給客戶端授權后才能正常的工作了。
給test創建一個擁有讀寫權限的賬號
-
退出之前的MongoDB的shell,可以直接關閉,然后重新打開一個“命令指示符”窗口或者使用“exit”命令。
-
使用之前創建的“sa”賬號登陸,登陸用到“db.auth"方法,如下所示:
> exit bye C:\Users\**>mongo MongoDB shell version: 3.0.6 connecting to: test > use admin switched to db admin > db.auth("sa","123") 1 >
-
給“test”數據庫創建一個賬號:testUser,密碼:123,擁有“readWrite”角色的權限
> use test switched to db test > db.createUser( ... { ... user: "testUser", ... pwd: "123", ... roles: [ { role: "readWrite", db: "test" } ] ... } ... ) Successfully added user: { "user" : "testUser", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } >
MongoDB中用戶的角色說明
read
數據庫的只讀權限,包括:
aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats,count,dataSize,dbHash,dbStats,distinct,filemd5,mapReduce (inline output only.),text (beta feature.)geoNear,geoSearch,geoWalk,group
readWrite
數據庫的讀寫權限,包括:
cloneCollection (as the target database.),convertToCapped,create (and to create collections implicitly.),renameCollection (within the same database.)findAndModify,mapReduce (output to a collection.) drop(),dropIndexes,emptycapped,ensureIndex()
和read的所有權限
dbAdmin
clean,collMod,collStats,compact,convertToCappe 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
shardingState,shutdown,splitChunk,splitVector,split,top,touchresync
serverStatus,setParameter,setShardVersion,shardCollection
replSetMaintenance,replSetReconfig,replSetStepDown,replSetSyncFrom
repairDatabase,replSetFreeze,replSetGetStatus,replSetInitiate
logRotate,moveChunk,movePrimary,netstat,removeShard,unsetSharding
hostInfo,db.currentOp(),db.killOp(),listDatabases,listShardsgetCmdLineOpts,getLog,getParameter,getShardMap,getShardVersion
enableSharding,flushRouterConfig,fsync,db.fsyncUnlock()
readAnyDatabase
任何數據庫的只讀權限(和read相似)
readWriteAnyDatabase
任何數據庫的讀寫權限(和readWrite相似)
userAdminAnyDatabase
任何數據庫用戶的管理權限(和userAdmin相似)
dbAdminAnyDatabase
任何數據庫的管理權限(dbAdmin相似)
__system
什么權限都有