如果您只是想测试是否可以通过以下方式连接到 MongoDB 服务器而无需身份验证bash
,您可以使用类似于以下内容的脚本:
#!/bin/bash
# Connect to MongoDB address (host:port/dbname) specified as first parameter
# If no address specified, `mongo` default will be localhost:27017/test
isAuth=`mongo --eval "db.getUsers()" $1 | grep "not auth"`
if [ -z "$isAuth" ] ;
then
echo "mongod auth is NOT enabled"
exit 1
else
echo "mongod auth is ENABLED"
exit 0
fi
输出示例:
$ ./isAuthEnabled.sh localhost:27017
mongod auth is ENABLED
$ ./isAuthEnabled.sh localhost:27777
mongod auth is NOT enabled
该脚本的唯一参数是要连接的可选 MongoDB 地址(主机:端口/数据库名称);这mongo
shell 默认使用localhost:27017/test
.
该脚本对是否可以在未经许可的情况下列出用户进行简单检查。
如果正确启用了身份验证,db.getUsers()
命令应该返回如下错误:
"Error: not authorized on test to execute command { usersInfo: 1.0 }"
新版本
请注意,错误消息在较新版本的 MongoDB 中已更改,因此您可能需要grep "requires authentication"
代替not auth
注意:本地主机异常
默认情况下(在 MongoDB 3.0 中)有一个localhost exception这使您可以创建第一个用户管理员通过连接进行部署localhost
。一旦添加了至少一个用户,本地主机例外就会自动禁用。
如果您想检查部署的完整安全性,那么绝对值得查看MongoDB 安全检查表.