MongoDB 的访问控制能够有效保证数据库的安全,访问控制是指绑定 Application 监听的 IP 地址,设置监听端口,使用账户和密码登录
一,访问控制的参数
1,绑定 IP 地址
mongod 参数:--bind_ip <ip address>
默认值是所有的 IP 地址都能访问,该参数指定 MongoDB 对外提供服务的绑定 IP 地址,用于监听客户端 Application 的连接,客户端只能使用绑定的 IP 地址才能访问 mongod,其他 IP 地址是无法访问的。
2,设置监听端口
mongod 参数:--port
MongoDB 默认监听的端口是 27017,该参数显式指定 MongoDB 实例监听的 TCP 端口,只有当客户端 Application 连接的端口和 MongoDB 实例监听的端口一致时,才能连接到 MongoDB 实例。
3,启用用户验证
mongod 参数:--auth
默认值是不需要验证,即 --noauth,该参数启用用户访问权限控制;当 mongod 使用该参数启动时,MongoDB 会验证客户端连接的账户和密码,以确定其是否有访问的权限。如果认证不通过,那么客户端不能访问 MongoDB 的数据库。
Enables authorization to control user's access to database resources and operations. When authorization is enabled, MongoDB requires all clients to authenticate themselves first in order to determine the access for the client.
4,权限认证
mongo 参数:--username
mongo 参数:--password
mongo 参数:--authenticationDatabase
在连接 mongo 时,使用参数 --authenticationDatabase,会认证 -u 和 -p 参数指定的账户和密码。如果没有指定验证数据库,mongo 使用连接字符串中指定的 DB 作为验证数据块。
二,基于角色的访问控制(Role-Based Access Control)
角色是授予 User 在指定资源上执行指定操作的权限,MongoDB 官方手册对角色的定义是:
A role grants privileges to perform the specified actions on resource.
MongoDB 为了方便管理员管理权限,在 DB 级别上预先定义了内置角色;如果用户需要对权限进行更为细致的管理,MongoDB 允许用户创建自定义的角色,能够在集合级别上控制 User 能够执行的操作。
MongoDB 使用角色(Role)授予 User 访问资源的权限,Role 决定 User 能够访问的数据库资源和执行的操作。一个 User 能够被授予一个或多个 Role,如果 User 没有被授予 Role,那么就没有访问 MongoDB 系统的权限。
A user is granted one or more roles that determine the user's access to database resources and operations. Outside of role assignments, the user has no access to the system.
1,内置角色(Built-In Roles)
内置角色是 MongoDB 预定义的角色,操作的资源是在 DB 级别上。MongoDB 拥有一个 SuperUser 的角色:root,拥有最大权限,能够在系统的所有资源上执行任意操作。
数据库用户角色(Database User Roles):
数据库管理角色(Database Administration Roles):
备份和还原角色(Backup and Restoration Roles):
跨库角色(All-Database Roles):
集群管理角色(Cluster Administration Roles):
2,用户自定义的角色(User-Defined Roles)
内置角色只能控制 User 在 DB 级别上执行的操作,管理员可以创建自定义角色,控制用户在集合级别(Collection-Level)上执行的操作,即,控制 User 在当前 DB 的特定集合上执行特定的操作。
在创建角色时,必须明确 Role 的四个特性:
2.1 角色作用的范围(Scope)
在 admin 数据库中创建的角色,Scope 是全局的,能够在 admin,其他 DB 和集群中使用,并且能够继承其他 DB 的 Role;而在非 admin 中创建的角色,Scope 是当前数据库,只能在当前 DB 中使用,只能继承当前数据库的角色。
A role created in the admin database can include privileges that apply to the admin database, other databases or to the cluster resource, and can inherit from roles in other databases as well as the admin database. Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.
2.2 权限的操作(Privilege actions)
MongoDB 的权限包由:资源(Resource)和操作(Action)两部分组成,Privilege Actions 定义 User 能够在资源上执行的操作,例如:MongoDB 在文档级别(Document-Level)上执行的读写操作(Query and Write Actions)列表是:
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 数组中继承权限:
4,自定义角色管理函数
三,管理用户和权限
1,创建用户
- use db_name db.createUser({
- user: "user_name",
- pwd: "user_pwd",
- roles: [{
- role: "clusterAdmin",
- db: "admin"
- },
- {
- role: "readAnyDatabase",
- db: "admin"
- },
- "readWrite"]
- })
为新建的 User,授予一个或多个角色,通过 roles 数组来实现:
2,权限认证(Authenticate)
mongo 连接到 mongod,有两种权限认证的方式:
- use db_name db.auth("user_name", "user_pwd")
3,用户管理函数
参考文档:
来源: http://www.cnblogs.com/ljhdo/p/4863318.html