MongoDB——权限管理

MongoDB——权限管理

MongoDB默认是没有权限验证的,但生产环境中,没有权限控制是很不安全的。

我们先不详谈太多概念,直接动手创建两个典型的账号:

  • 超级管理员,类似sql server的sa账号或者mysql的root账号
  • 单个数据库的读写权限账号

创建超级管理员

  1. 在没开启权限验证机制的时候,我们在“命令指示符”窗口用mongo打开MongoDB的shell。

  2. 使用“use”命令,切换到admin数据库,MongoDB用admin的system.users集合保存着用户信息。

     > use admin
     switched to db admin
     >
  3. 使用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创建一个拥有读写权限的账号

  1. 退出之前的MongoDB的shell,可以直接关闭,然后重新打开一个“命令指示符”窗口或者使用“exit”命令。

  2. 使用之前创建的“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
     >
  3. 给“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
什么权限都有

转载于:https://www.cnblogs.com/sheepswallow/p/4868519.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值