3.安全
MongoDB支持对单个连接的认证。
3.1 认证的基础知识
每个MongoDB实例中的数据库都可以有很多用户。开启认证后,只有数据库认证用户才能执行读写操作。
认证后,管理员可以读写所有的数据库,执行特定的管理命令。
开启安全认证前,需要有管理员帐号。
> use admin switched to db admin > db.addUser("root", "root123"); { "user" : "root", "readOnly" : false, "pwd" : "81c5bca573e01b632d18a459c6cec418", "_id" : ObjectId("530bd17622cceb4323a2b500") } > use test switched to db test > db.addUser("test_user", "root123", true); { "user" : "test_user", "readOnly" : true, "pwd" : "d436badec207e3821abbaf337fcbdd06", "_id" : ObjectId("530bd24322cceb4323a2b501") }
在shell中创建只读用户将adduser的第三个参数设为true。调用addUser()必须对数据库有写权限。
addUser不仅可以增加新用户,还能修改用户口令或只读状态。
重启服务器,加入--auth选项,开启安全检查。
> use admin switched to db admin > db.auth("root", "root123"); 1
3.2 认证的工作原理
数据库用户帐户以文档形式存储在system.users集合里面。
> use admin switched to db admin > db.system.users.find(); { "_id" : ObjectId("530bd17622cceb4323a2b500"), "user" : "root", "readOnly" : false, "pwd" : "81c5bca573e01b632d18a459c6cec418" }
可以执行
db.system.users.remove({"user":"root"});
删除帐号。
用户认证时,服务器将认证和连接绑定来跟踪认证。
3.3 其他安装考虑
除了认证还有许多选项来锁定MongoDB实例。即便使用认证,MongoDB传输协议是不加密的。如需加密,需要使用SHH隧道或类似做客户端和服务器之间的加密。
MongoDB服务器建议布置在防火墙或内网中,但是如果需要被外部访问,使用--bindip选项,可以指定mongod绑定在本机IP地址。
可以用--noscripting完全禁止服务器端JavaScript的执行。