如何查找Mongodb是否在shell脚本中以auth模式运行?

Jee*_*a G 7 unix shell mongodb mongodb-shell

我正在运行mongodb实例,它在我的服务器机器上运行auth模式.目前我使用shell scipt来判断是否有mongodb实例正在运行.如何检查mongodb是在auth模式还是非auth模式下运行.

Ste*_*nie 9

如果您只想测试是否可以通过身份验证连接到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
Run Code Online (Sandbox Code Playgroud)

示例输出:

$ ./isAuthEnabled.sh localhost:27017
mongod auth is ENABLED

$ ./isAuthEnabled.sh localhost:27777
mongod auth is NOT enabled
Run Code Online (Sandbox Code Playgroud)

此脚本的唯一参数是要连接的可选MongoDB地址(host:port/dbname); 所述mongo壳默认使用localhost:27017/test.

该脚本可以简单地检查是否可以在未经许可的情况下列出用户.

如果正确启用了auth,则该db.getUsers()命令应返回如下错误:

  "Error: not authorized on test to execute command { usersInfo: 1.0 }"
Run Code Online (Sandbox Code Playgroud)

注意:Localhost异常

默认情况下(如MongoDB 3.0)localhost exception,允许您通过连接via为部署创建第一个用户管理员localhost.一旦添加了至少一个用户,就会自动禁用localhost异常.

如果要检查部署的完整安全性,那么绝对值得查看MongoDB安全检查表.