加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_郴州站长网 (http://www.0735zz.com/)- 云通信、区块链、物联设备、云计算、站长网!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

MongoDB 访问权限控制

发布时间:2016-11-12 10:47:51 所属栏目:教程 来源:站长网
导读:副标题#e# MongoDB的访问控制能够有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用账户和密码登录 一,访问控制的参数 1,绑定IP地址 mongod 参数:--bind_ip ip address 默认值是所有的IP地址都能访问,该参数指定Mong

MongoDB的权限包由:资源(Resource)和操作(Action)两部分组成,Privilege Actions 定义User能够在资源上执行的操作,例如:MongoDB在文档级别(Document-Level)上执行的读写操作(Query and Write Actions)列表是:

  • find
  • insert
  • remove
  • update

3,创建角色

使用db.CreateRole()在当前DB中创建角色,创建的语法示例如下:

use admin
db.createRole(
   {
     role: "new_role",
     privileges: [
       { resource: { cluster: true }, actions: [ "addShard" ] },
       { resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
       { resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
       { resource: { db: "", collection: "" }, actions: [ "find" ] }
     ],
     roles: [
       { role: "read", db: "admin" }
     ]
   },
   { w: "majority" , wtimeout: 5000 }
)

在roles数组中,指定被继承的role,即,新建的new_role从roles数组中继承权限:

  • 如果被继承的role在当前DB中,定义的格式是:roles:["role"];
  • 如果被继承的role不在当前DB中,需要使用doc,指定该role所在的DB,定义的格式是:roles:[{role:"role_name", db:"db_name"}];

4,自定义角色管理函数

  • db.createRole() :Creates a role and specifies its privileges.
  • db.updateRole() :Updates a user-defined role.
  • db.dropRole() :Deletes a user-defined role.
  • db.dropAllRoles() :Deletes all user-defined roles associated with a database.
  • db.grantPrivilegesToRole() :Assigns privileges to a user-defined role.
  • db.revokePrivilegesFromRole() :Removes the specified privileges from a user-defined role.
  • db.grantRolesToRole() :Specifies roles from which a user-defined role inherits privileges.
  • db.revokeRolesFromRole() :Removes inherited roles from a role.
  • db.getRole() :Returns information for the specified role.
  • db.getRoles() :Returns information for all the user-defined roles in a database.

三,管理用户和权限

1,创建用户

use db_name
db.createUser( { user: "user_name", pwd: "user_pwd", roles: [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite"
] } )

为新建的User,授予一个或多个角色,通过roles数组来实现:

  • 如果role存在于当前DB中,roles的格式:roles:["role"];
  • 如果role不存在于当前DB中,roles的格式:roles:[Role:"role_name", db:"db_name"];

2,权限认证(Authenticate)

mongo连接到mongod,有两种权限认证的方式:

  • 在连接时认证用户访问的权限,mongo 使用参数 --authenticationDatabase <dbname> 指定认证数据库;
  • 在连接后,认证用户访问的权限,mongo 没有使用参数 --authenticationDatabase <dbname>,在连接到mongod之后,切换到验证数据库(authentication database)中,使用db.auth() 验证User是否有权限访问当前数据库;
use db_name
db.auth("user_name", "user_pwd" )

3,用户管理函数

  • db.auth() :Authenticates a user to a database.
  • db.createUser() :Creates a new user.
  • db.updateUser() :Updates user data.
  • db.changeUserPassword() :Changes an existing user’s password.
  • db.dropAllUsers() :Deletes all users associated with a database.
  • db.dropUser() :Removes a single user.
  • db.grantRolesToUser() :Grants a role and its privileges to a user.
  • db.revokeRolesFromUser() :Removes a role from a user.
  • db.getUser() :Returns information about the specified user.
  • db.getUsers() :Returns information about all users associated with a database.

 

参考文档:

Role-Based Access Control

Built-In Roles

Collection-Level Access Control

db.createRole()

db.createUser()

Enable Auth

Manage Users and Roles

mongod

(编辑:开发网_郴州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读