MongoDB的访问控制可以有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用帐户和密码登陆mongodb
一,访问控制的参数数据库
1,绑定IP地址数组
mongod 参数:--bind_ip <ip address>安全
默认值是全部的IP地址都能访问,该参数指定MongoDB对外提供服务的绑定IP地址,用于监听客户端 Application的链接,客户端只能使用绑定的IP地址才能访问mongod,其余IP地址是没法访问的。app
2,设置监听端口ide
mongod 参数:--port <port>函数
MongoDB 默认监听的端口是27017,该参数显式指定MongoDB实例监听的TCP 端口,只有当客户端Application链接的端口和MongoDB实例监听的端口一致时,才能链接到MongoDB实例。工具
3,启用用户验证ui
mongod 参数:--auth this
默认值是不须要验证,即 --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 <username>, -u <username>
mongo 参数:--password <password>, -p <password>
mongo 参数:--authenticationDatabase <dbname> 指定建立User的数据库;在特定的数据库中建立User,该DB就是User的authentication database。
在链接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,用户管理函数
参考文档: