mongodb 验证MongoCredential和Uncategorized Mongo Db异常

xe55xuns  于 2023-05-17  发布在  Go
关注(0)|答案(5)|浏览(230)

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

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

登录凭据是正确的,但我仍收到此错误。我也得到Uncategorized Mongo Db异常
请帮帮我!

v1l68za4

v1l68za41#

根据#46934427,设置spring.data.mongodb.authentication-database=admin可能会解决您的问题。
MongoDB中的默认数据库是test,因此,您可能正在尝试对test数据库进行身份验证。切换到admin数据库(用于创建用户的公共位置)可能会解决您的问题。

mutmk8jj

mutmk8jj2#

你可以试试这个:

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

mlmc2os53#

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" } ]
}
)
62lalag4

62lalag44#

将这些配置放在应用程序属性文件中:

spring.data.mongodb.host= YOUR_HOST like 192.168.x.x
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=admin
spring.data.mongodb.password=PASSWORD
spring.data.mongodb.database=DB_NAME
dgjrabp2

dgjrabp25#

为了补充其他答案-我遇到这个问题是因为Mongo同时在我的本地机器Docker中运行。这两个示例都在监听默认的27017端口,我连接到了我的本地Mongo,而不是容器中的那个。
如果你正在连接到dockerized Mongo -尝试临时关闭容器,并找出是否有任何进程也在监听27017。如果确实有这样的进程-确保你不需要它,然后终止(LinuxWindows)。您可能还希望从自动启动中删除已终止的进程,以便它们在重新启动后不会再次出现(LinuxWindows)。

相关问题