提问者:小点点

验证MongoCredential和未分类Mongo Db异常的异常


我在运行我的应用程序时收到此连接引发的错误套接字异常。

INFO 5231 --- [nio-8087-exec-1] org.mongodb.driver.connection: Closed connection [connectionId{localValue:2}] to 192.168.0.2:27017 because there was a socket exception raised by this connection.**

    org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}
        at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138)
        at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2756)
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2666)
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2409)
        at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:214)

        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

    Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='campbell', password=<hidden>, mechanismProperties={}}

    Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 192.168.0.2:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

**I have declared properties in application properties-**
spring.data.mongodb.database=dbname
spring.data.mongodb.username=admin
spring.data.mongodb.password=*******
spring.data.mongodb.port=27017
spring.data.mongodb.host=192.168.0.2

登录凭据仍然正确,我收到了这个错误。异常验证MongoCredential也我得到了未分类的Mongo Db异常

请帮帮我!


共3个答案

匿名用户

根据#46934427,设置spring.data. mongoDB.验证数据库=admin可能会解决您的问题。

MongoDB中的默认数据库是test,因此,您可能正在尝试针对test数据库进行身份验证。切换到admin数据库(创建用户的常用位置)可能会解决您的问题。

匿名用户

你可以试试这个:

spring.data.mongodb.uri=mongodb://user:passwod@localhost/test?authSource=admin

匿名用户

Add user admin into your database. 
Open installed folder and open the mongo.exe file and run below command : 
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" } ]
}
)