开发者社区> 问答> 正文

spring boot连接MongoDB报错

刚开始学习MongoDB,编写了一个简单的spring boot程序连接mongo
,使用如下配置可以正确连接到mongo:

spring.data.mongodb.host=10.159.14.19
spring.data.mongodb.port=9001
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=esign
spring.data.mongodb.username=esign_write
spring.data.mongodb.password=NTc2OTQ1YjcwN2Z
但是使用下面的配置就会报错:

spring.data.mongodb.uri=mongodb://esign_write:NTc2OTQ1YjcwN2Z@10.159.14.19:9001/esign
spring.data.mongodb.authentication-database=admin
错误信息如下:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='esign_write', source='esign', password=, mechanismProperties={}}

at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

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

at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:95) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45) ~[mongodb-driver-core-3.2.2.jar:na]
... 6 common frames omitted

这个是什么原因?

本问题及下方已被采纳的回答均来自云栖社区【Redis&MongoDB 社区大群】。https://yq.aliyun.com/articles/690084 点击链接欢迎加入社区大社群。

展开
收起
李博 bluemind 2019-04-08 16:45:08 6106 0
1 条回答
写回答
取消 提交回答
  • 云栖社区Java、Redis、MongoDB运营小编,有意合作请联系钉钉:15810436147

    spring.data.mongodb.uri=mongodb://esign_write:NTc2OTQ1YjcwN2Z@10.159.14.19:9001/esign
    spring.data.mongodb.authentication-database=admin
    改成
    spring.data.mongodb.uri=mongodb://esign_write:NTc2OTQ1YjcwN2Z@10.159.14.19:9001/
    spring.data.mongodb.database=esign
    spring.data.mongodb.authentication-database=admin

    我也不知道为什么 试了下 可以

    2019-07-17 23:32:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载