当您尝试根据数据库对用户进行身份验证时,如果mongod未使用--auth参数启动,则会出现以下错误:身份验证失败!如何知道mongodb是否需要验证?
那么有没有办法知道数据库是否需要认证?
类似的东西:
DB db = moClient.getDB(moClientURI.getDatabase());
if (db.needsAuthentication()){
db.authenticate(username, password.toCharArray());
if (db.isAuthenticated()){
//do something
} else {} // authentication failed
}
,但[这](HTTP:// docs.mongodb.org/ecosystem/tutorial/authenticate-with-java-driver)和[this](http://www.mkyong.com/mongodb/java-authentication-access-to-mongodb)示例可能会有帮助。 –
不,已经检查过这些链接。它不会告诉你如何知道mongodb是否需要认证。当然,我可以运行一个命令并检查是否得到'认证失败',看看它是否需要授权,但这不是一个方便的方法。 –
您可以通过db.serverCmdLineOpts()或db.adminCommand('getCmdLineOpts')启动服务器参数 –