鍍金池/ 問答/數(shù)據(jù)庫/ spring boot連接mongodb報錯

spring boot連接mongodb報錯

剛開始學(xué)習(xí)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=<hidden>, 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

這個是什么原因?

回答
編輯回答
浪婳

把認(rèn)證庫也放在esign中,而不是admin就好了。不知道為什么!??!

2018年4月8日 13:31
編輯回答
耍太極

首先SpringBoot連接mongoDB要導(dǎo)入相應(yīng)的pom配置信息

    <!-- 增加mongodb支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

在配置文件中我這邊用的是application.yml,不知道你用的是什么,

  data:
    mongodb:
      uri: mongodb:/用戶名:密碼@IP:端口/數(shù)據(jù)庫名稱

加這段就可以了,連接數(shù)據(jù)庫的話我們用的是SpringBoot,我推薦用SpringData的 MongoRepository 很方便curd.

2017年6月30日 11:07