mongodb3.2版本用户管理
2016-07-09 17:08
423 查看
以前只是对mongodb做了一些简单的操作,认识也并不深刻。总想着数据库无非就是存数据,增删改查之类的。这几天为了以后的工作需要特别看了下,发现它与传统的关系型数据库有很大的差别。虽然也是存储数据,但是人家用的是内存存储,增删改查也不再用SQL语句了,而用的基于JS的一些方法。言规正传,今天先研究一下他的用户管理怎么整的。
1、服务端
mongod --dbpath D:\MongoDB\data\
---------------------这是不加用户验证的服务端启用,测试用admin和test用户登陆都可以新增数据库,插入数据库数据
mongod --dbpath D:\MongoDB\data\ --auth
--------------------加数据库验证的服务端启用。
2、管理员
2.1、建立
>use admin
> db.createUser({user:'dy',pwd:'123',roles:['userAdminAnyDatabase','dbAdminAnyDa
tabase']}
建完以后会在admin数据库下看到一个system.users的集合,db.system.users.find()打开集合可以看到新建的用户
2.2、登陆后,需到如下才能验证 ----------------------------------如果是给某个库建立的,需要到那个库下验证才行。
> use admin
switched to db admin
> db.auth("dy","123")
1 2.3、所建的管理员只能管理用户,查看数据库,查看数据库内的集合,却不能查看和操作集合内的详细内容。
3、超级管理员
3.1、建立 db.createUser({user:"NO1",pwd:"123",roles:["root"]}) 3.2、登陆
4、单数据库读写
4.1、建立
db.createUser({user:"dy11",pwd:"123",roles:[{role:"readWrite",db:"text1"}]}) 4.2、登陆
> use text1
switched to db text1
> db.auth("dy11","123")
1 4.3、只能对text1库里的集合,及集合中的内容进行增删改查
5、单数据库读写
5.1、建立
db.createUser({user:"dy11",pwd:"123",roles:[{role:"read",db:"text1"}]})
5.2、登陆
6、多数据库读写
6.1、建立
> db.createUser({user:"text3",pwd:"123",roles:[{role:"readWrite",db:"text3"},{ro
le:"readWrite",db:"text4"}]})
6.2、登陆
> use text3
switched to db text3
> db.auth("text3","123")
1 6.3、可以对text3、text4进行操作
7、修改密码
use admin
db.changeUserPassword("username", "xxx")8. 查看用户信息
9、修改密码和用户信息
db.runCommand(
{
updateUser:"username",
pwd:"xxx",
customData:{title:"xxx"}
}
)
备注:
1. 和用户管理相关的操作基本都要在admin数据库下运行,要先use admin;
2. 如果在某个单一的数据库下,那只能对当前数据库的权限进行操作,验证也在那某个库下。
3、多用户权限之间切换的次数多了,可能权限会乱掉,最好重开,不要多次切换。
4、原先看网上说的,单个库用户会在单个某下面的system.user的集合,但实际这个版本都是在admin库下的system.user
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三、以下是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相似)
详细的可以参看官方文档:http://docs.mongodb.org/manual/reference/method/db.addUser/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AME
下方列出系统内置角色名称:
read
readWrite
dbAdmin 可以管理数据库
dbOwner 单数据库最大权限,dbAdmin,userAdmin
userAdmin 可管理当前数据库用户
clusterManager
clusterMonitor
hostManager
backup
restore
readAnyDatabase 在admin下建立,可以读取所有数据库的信息
readWriteAnyDatabase 在admin下建立,可以读写所有数据库的信息
userAdminAnyDatabase 在admin下建立,可以管理所有数据库的用户
dbAdminAnyDatabase 在admin下建立,可以管理所有数据库的信息(类似于所有数据库的dbAdmin账户)
root
参与文档:http://www.cnblogs.com/wingjay/p/3954430.html
http://blog.csdn.net/yenange/article/details/43966799
1、服务端
mongod --dbpath D:\MongoDB\data\
---------------------这是不加用户验证的服务端启用,测试用admin和test用户登陆都可以新增数据库,插入数据库数据
mongod --dbpath D:\MongoDB\data\ --auth
--------------------加数据库验证的服务端启用。
2、管理员
2.1、建立
>use admin
> db.createUser({user:'dy',pwd:'123',roles:['userAdminAnyDatabase','dbAdminAnyDa
tabase']}
建完以后会在admin数据库下看到一个system.users的集合,db.system.users.find()打开集合可以看到新建的用户
2.2、登陆后,需到如下才能验证 ----------------------------------如果是给某个库建立的,需要到那个库下验证才行。
> use admin
switched to db admin
> db.auth("dy","123")
1 2.3、所建的管理员只能管理用户,查看数据库,查看数据库内的集合,却不能查看和操作集合内的详细内容。
3、超级管理员
3.1、建立 db.createUser({user:"NO1",pwd:"123",roles:["root"]}) 3.2、登陆
> use admin switched to db admin > db.auth("NO1","123") 13.3、这个管理员有全部的全限,可以管理用户,查看数据库,查看数据库内的集合,详细内容,并且还可以对任何库增删查
4、单数据库读写
4.1、建立
db.createUser({user:"dy11",pwd:"123",roles:[{role:"readWrite",db:"text1"}]}) 4.2、登陆
> use text1
switched to db text1
> db.auth("dy11","123")
1 4.3、只能对text1库里的集合,及集合中的内容进行增删改查
5、单数据库读写
5.1、建立
db.createUser({user:"dy11",pwd:"123",roles:[{role:"read",db:"text1"}]})
5.2、登陆
> use text2 switched to db text2 > db.auth("text2","123")5.3、只对text2库里的集合可以读
6、多数据库读写
6.1、建立
> db.createUser({user:"text3",pwd:"123",roles:[{role:"readWrite",db:"text3"},{ro
le:"readWrite",db:"text4"}]})
6.2、登陆
> use text3
switched to db text3
> db.auth("text3","123")
1 6.3、可以对text3、text4进行操作
7、修改密码
use admin
db.changeUserPassword("username", "xxx")8. 查看用户信息
db.runCommand({usersInfo:"userName"})
9、修改密码和用户信息
db.runCommand(
{
updateUser:"username",
pwd:"xxx",
customData:{title:"xxx"}
}
)
备注:
1. 和用户管理相关的操作基本都要在admin数据库下运行,要先use admin;
2. 如果在某个单一的数据库下,那只能对当前数据库的权限进行操作,验证也在那某个库下。
3、多用户权限之间切换的次数多了,可能权限会乱掉,最好重开,不要多次切换。
4、原先看网上说的,单个库用户会在单个某下面的system.user的集合,但实际这个版本都是在admin库下的system.user
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三、以下是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相似)
详细的可以参看官方文档:http://docs.mongodb.org/manual/reference/method/db.addUser/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AME
DESCRIPTION | |
---|---|
db.addUser() | Deprecated. Adds a user to a database, and allows administrators to configure the user’s privileges. |
db.changeUserPassword() | Changes an existing user’s password. |
db.createUser() | Creates a new user. |
db.dropAllUsers() | Deletes all users associated with a database. |
db.dropUser() | Removes a single user. |
db.getUser() | Returns information about the specified user. |
db.getUsers() | Returns information about all users associated with a database. |
db.grantRolesToUser() | Grants a role and its privileges to a user. |
db.removeUser() | Deprecated. Removes a user from a database. |
db.revokeRolesFromUser() | Removes a role from a user. |
db.updateUser() | Updates user data. |
Database User Roles 普通用户角色
readreadWrite
Database Administration Roles 管理员角色
dbAdmin 可以管理数据库dbOwner 单数据库最大权限,dbAdmin,userAdmin
userAdmin 可管理当前数据库用户
Cluster Administration Roles 管理员角色
clusterAdminclusterManager
clusterMonitor
hostManager
Backup and Restoration Roles 备份和恢复角色
backuprestore
All-Database Roles 所有数据库角色
readAnyDatabase 在admin下建立,可以读取所有数据库的信息readWriteAnyDatabase 在admin下建立,可以读写所有数据库的信息
userAdminAnyDatabase 在admin下建立,可以管理所有数据库的用户
dbAdminAnyDatabase 在admin下建立,可以管理所有数据库的信息(类似于所有数据库的dbAdmin账户)
uperuser Roles
root参与文档:http://www.cnblogs.com/wingjay/p/3954430.html
http://blog.csdn.net/yenange/article/details/43966799
相关文章推荐
- mongodb 运行时添加新的分片复制集
- 利用Morphia框架工具类操作mongodb的实例。
- mongodb的修改器
- window平台安装MongoDB
- 用java连接mongodb并执行$sum和$avg结合的聚合函数的实例。
- linux32位mongodb启动报错【exception in initAndListen 】
- golang学习之mgo操作mongodb
- MongoDB增删查改
- MongoDB-基础-limit-skip-sort
- MongoDB-基础-条件操作符
- mongodb-基础-update-remove
- MongoDB 3.0 用户创建
- morphia查询mongodb内嵌文档-数组查询$elemMatch|hasThisElement实例
- mongodb 备份 还原 导出 导入
- 作业上传+学生互评打分系统
- mongodb find语法记录
- CentOS系统环境下安装MongoDB
- MongoDB自然排序
- mongodb备份
- MongoDB(2)