mongodb3.X版本 用户管理

来源:互联网 发布:淘宝首页全屏海报尺寸 编辑:程序博客网 时间:2024/06/05 05:02

以前只是对MongoDB做了一些简单的操作,认识也并不深刻。总想着数据库无非就是存数据,增删改查之类的。这几天为了以后的工作需要特别看了下,发现它与传统的关系型数据库有很大的差别。虽然也是存储数据,但是人家用的是内存存储,增删改查也不再用SQL语句了,而用的基于JS的一些方法。言规正传,今天先研究一下他的用户管理怎么整的。

    1、服务端

          mongod --dbpath D:\MongoDB\data\          ---------------------这是不加用户验证的服务端启用,测试用admin和test用户登陆都可以新增数据库,插入数据库数据

          mongod --dbpath D:\MongoDB\data\ --auth      --------------------加数据库验证的服务端启用。

   2、管理员

         2.1、建立

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.     >use admin  
  2.     > db.createUser({user:'dy',pwd:'123',roles:['userAdminAnyDatabase','dbAdminAnyDa  
  3. tabase']}  
       建完以后会在admin数据库下看到一个system.users的集合,db.system.users.find()打开集合可以看到新建的用户

        2.2、登陆后,需到如下才能验证  ----------------------------------如果是给某个库建立的,需要到那个库下验证才行。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > use admin  
  2. switched to db admin  
  3. > db.auth("dy","123")  
  4. 1  
       2.3、所建的管理员只能管理用户,查看数据库,查看数据库内的集合,却不能查看和操作集合内的详细内容。
  3、超级管理员

       3.1、建立   

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. db.createUser({user:"NO1",pwd:"123",roles:["root"]})  
      3.2、登陆
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > use admin  
  2. switched to db admin  
  3. > db.auth("NO1","123")  
  4. 1  
      3.3、这个管理员有全部的全限,可以管理用户,查看数据库,查看数据库内的集合,详细内容,并且还可以对任何库增删查

  4、单数据库读写

      4.1、建立

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. db.createUser({user:"dy11",pwd:"123",roles:[{role:"readWrite",db:"text1"}]})  
     4.2、登陆

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > use text1  
  2. switched to db text1  
  3. > db.auth("dy11","123")  
  4. 1  
    4.3、只能对text1库里的集合,及集合中的内容进行增删改查

 5、单数据库读写 

    5.1、建立

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. db.createUser({user:"dy11",pwd:"123",roles:[{role:"read",db:"text1"}]})  

   5.2、登陆
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > use text2  
  2. switched to db text2  
  3. > db.auth("text2","123")  
  5.3、只对text2库里的集合可以读

6、多数据库读写

   6.1、建立

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > db.createUser({user:"text3",pwd:"123",roles:[{role:"readWrite",db:"text3"},{ro  
  2. le:"readWrite",db:"text4"}]})  

  6.2、登陆

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. > use text3  
  2. switched to db text3  
  3. > db.auth("text3","123")  
  4. 1  
  6.3、可以对text3、text4进行操作

7、修改密码

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. use admin  
  2. db.changeUserPassword("username", "xxx")  
8. 查看用户信息

db.runCommand({usersInfo:"userName"})
9、修改密码和用户信息

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. db.runCommand(  
  2.   {  
  3.     updateUser:"username",  
  4.     pwd:"xxx",  
  5.     customData:{title:"xxx"}  
  6.   }  
  7. )  

备注:

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

 DESCRIPTIONdb.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 普通用户角色

read
readWrite

Database Administration Roles 管理员角色

dbAdmin        可以管理数据库
dbOwner        单数据库最大权限,dbAdmin,userAdmin
userAdmin     可管理当前数据库用户
 

Cluster Administration Roles  管理员角色

clusterAdmin
clusterManager
clusterMonitor
hostManager

Backup and Restoration Roles  备份和恢复角色

backup
restore

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

0 0
原创粉丝点击