Mongodb 3权限管理

配置文件:
vi  /etc/mongod.conf
storage:
    dbPath: "/opt/mongodb/data"
    journal:
        enabled: true
    directoryPerDB: true
    engine: "wiredTiger"
    wiredTiger:
        engineConfig:
            cacheSizeGB: 3


systemLog:
    logAppend: true
    destination: file
    path: "/opt/mongodb/log/mongodb.log"
processManagement:
    pidFilePath: "/opt/mongodb/log/mongo.pid"
    fork: true
net:
    port: 40001
    maxIncomingConnections: 5000


security:
    keyFile: "/opt/mongodb/mykey"
    authorization: "enabled"


replication:
    replSetName: "gewadb"
    oplogSizeMB: 8192
operationProfiling:
    slowOpThresholdMs: 200
    mode: "slowOp"

初始安装,要注释掉security项目,不然不能创建账号

创建管理员账号:
mongo --port 40001
use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

授权:
db.grantRolesToUser("admin",[{ role: "dbAdmin", db: "admin" },{role:"clusterAdmin",db:"admin"}])

创建账号后,还要在各自的DB上创建相应的账号
通过管理员谁后,就可以创建其它DB的账号了

 use abc

db.createUser({

    user: "abc",

    pwd: "abc",

    roles: [{role: "dbOwner", db: "abc" }]

  }

);

db.auth('abc','abc')

启用权限认证后,如果该库没有创建账号,则不能操作该库。

MongoDB实例之间的身份验证

你可以验证的成员副本集分片的集群。 进行身份验证的成员 一个MongoDB部署,MongoDB可以使用密钥文件x机制。 使用密钥文件身份验证 成员也可以授权

MongoDB使用基于角色的访问控制(RBAC)管理访问 MongoDB系统。 一个用户授予一个或多个角色那 确定用户的访问和操作数据库资源。 外 的角色分配,用户没有访问系统。
MongoDB在默认情况下不启用授权。 您可以启用 授权使用——身份验证——密钥文件选项,或者使用一个配置文件,security.authorizationsecurity.keyFile设置。
角色总是授予特权,从不限制访问。 例如,如果一个用户 既有 和 readWriteAnyDatabase上的角色 数据库、更大的访问。

1. read角色

数据库的只读权限,包括:

aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats,count,dataSize,dbHash,dbStats,distinct,filemd5,mapReduce (inline output only.),text (beta feature.)geoNear,geoSearch,geoWalk,group 

2. readWrite角色

数据库的读写权限,包括:

read角色的所有权限

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() 

3. dbAdmin角色

数据库的管理权限,包括:

clean,collMod,collStats,compact,convertToCappe 
create,db.createCollection(),dbStats,drop(),dropIndexes 
ensureIndex(),indexStats,profile,reIndex 
renameCollection (within a single database.),validate 

4. userAdmin角色

数据库的用户管理权限

5. 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() 

6. readAnyDatabase角色

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

7. readWriteAnyDatabase角色

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

8. userAdminAnyDatabase角色

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

9. dbAdminAnyDatabase角色

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


请使用浏览器的分享功能分享到微信等