MongoDB 3.0.8 权限管理

MongoDB 3.0虽然管理上有所改变,但是还是没有太明显。
千万不要相信百度上各种,3.0起没有root用户,授个userAdminAnyDatabase角色权限的用户就是root用户。
不然用客户端连接会报下面一些错误
mongo shell 中db.auth 授权后删除普通数据库报错
> db.dropDatabase()
{
        "ok" : 0,
        "errmsg" : "not authorized on guo to execute command { dropDatabase: 1.0 }",
        "code" : 13
}
这是因为userAdminAnyDatabase不包括删除数据库权限
客户端连接查看数据报错
{
    "code" : 13,
    "errmsg" : "not authorized on admin to execute command { listCollections: true, filter: {}, cursor: {} }",
    "message" : "not authorized on admin to execute command { listCollections: true, filter: {}, cursor: {} }",
    "name" : "MongoError",
    "ok" : 0
}


Error encountered
Element 'inprog' not found.
Type: System.Collections.Generic.KeyNotFoundException
Stack:    在 MangoUI.ComServerView.RefreshCurrentOps(MMongo mo, TimeSpan& data, TimeSpan& gui, Boolean throwEx)
   在 MangoUI.ComServerView.RenderMe()

不仅有root角色,还有个超级无敌的__system角色不过官方不推荐使用,一般起用root角色即可。
详见:
https://docs.mongodb.org/manual/reference/built-in-roles/#userAdminAnyDatabase


3.0版本还是有root的,如下面第5种方式建出的才是root超级权限的用户。


mongodb从2.4版本中对用户权限管理做了全新的调整,把权限细化了,增强了安全性,越来越像mysql的权限管理了。

1. 创建一个超级用户

use admin
db.createUser(
  {
    user: "adminUserName",
    pwd: "userPassword",
    roles:
    [
      {
        roles: "userAdminAnyDatabase",
        db: "admin"
      }
    ]
  }
)

超级用户的role有两种,userAdmin或者userAdminAnyDatabase(比前一种多加了对所有数据库的访问)。

db是指定数据库的名字,admin是管理数据库。

2. 用新创建的用户登录

mongo --host xxx -u adminUserName -p userPassword --authenticationDatabase admin
3. 查看当前用户的权限

db.runCommand(
  {
    usersInfo:"userName",
    showPrivileges:true
  }
)
4. 创建一般用户,也是用createUser

use db01
db.createUser(
  {
    user:"oneUser",
    pwd:"12345",
    roles:[
      {role:"read",db:"db01"},
      {role:"read",db:"db02"},
      {role:"read",db:"db03"}
    ]
  }
)

5. 创建一个不受访问限制的超级用户

use admin
db.createUser(
  {
    user:"superuser",
    pwd:"pwd",
    roles:["root"]
  }
)

6. 修改密码

use admin
db.changeUserPassword("username", "xxx")
7. 查看用户信息

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

db.runCommand(
  {
    updateUser:"username",
    pwd:"xxx",
    customData:{title:"xxx"}
  }
)

注:

1. 和用户管理相关的操作基本都要在admin数据库下运行,要先use admin;

2. 如果在某个单一的数据库下,那只能对当前数据库的权限进行操作;

3. db.addUser是老版本的操作,现在3.0版本已经不通使用,创建出来的user是带有root role的超级管理员。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24742969/viewspace-1981570/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24742969/viewspace-1981570/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值