2017-03-28 15 views
2

我得到這個異常:無效蒙戈的配置,無論是URI或主機/端口/憑證必須指定

Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials must be specified 
    at org.springframework.boot.autoconfigure.mongo.MongoProperties.createMongoClient(MongoProperties.java:207) 
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:73) 
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$15f9b896.CGLIB$mongo$1(<generated>) 
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$15f9b896$$FastClassBySpringCGLIB$$c0338f6a.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) 
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$15f9b896.mongo(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) 
    ... 25 common frames omitted 

這裏是我的application.yml內容:

spring: 
    data: 
    mongodb: 
     uri: mongodb://develop:[email protected]<my_host>:27017/SHAM 

這裏是我的配置分類:

package com.me.service.testservice.config; 

import com.mongodb.MongoClient; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.mongodb.core.MongoTemplate; 
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 

@Configuration 
@EnableMongoRepositories(basePackages = {"com.me.service.testservice.repository"}, considerNestedRepositories = true) 
public class SpringMongoConfiguration { 

    @Bean 
    public MongoTemplate mongoTemplate() throws Exception { 
     return new MongoTemplate(new MongoClient("<my_host>"), "SHAM"); 
    } 
} 

現在我得到這個堆棧跟蹤時啓動沒有失敗,它看起來像用戶開發沒有連接右:

Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server phelbwlabect003.karmalab.net:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" } 
    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) 
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) 
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) 
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117) 
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37) 
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50) 
    ... 9 common frames omitted 
+0

在我的情況下,在'application.properties'文件中設置了一個活動的配置文件。這個配置文件在'application.properties'中設置'spring.data.mongodb.host'等時設置'spring.data.mongodb.uri'。 – rocksteady

回答

3

你混合使用各個屬性的樣式設置的URI風格的連接設置。

要麼使用

spring: 
    data: 
     mongodb: 
      host: localhost 
      port: 27017 
      database: SHAM_TEST 
      username: develop 
      password: pass 

或者

spring: 
    data: 
     mongodb: 
      uri:mongodb://develop:[email protected]:27017/SHAM_TEST 
+0

現在我只使用uri格式。 – Sofiane

+0

我已經糾正了我的問題。並增加了另一個。 – Sofiane

+0

您是在SHAM數據庫中開發創建的嗎?我在代碼中看到SHAM和SHAM_TEST。 – Veeram

3

的問題是認證數據庫失蹤。

現在這裏是工作的配置:

spring: 
    data: 
    mongodb: 
     host: <my_host> 
     username: develop 
     password: d3VeL0p$ 
     port: 27017 
     database: SHAM 
     repositories: 
     enabled: true 
     authentication-database: admin 
+0

請將此問題標記爲已解決。 – halfer

0

如果使用application.properties文件來存儲您的配置,使用下面的結構。

spring.data.mongodb.host = localhost 
spring.data.mongodb.port = 27017 
spring.data.mongodb.database = SHAM_TEST 
spring.data.mongodb.username = develop 
spring.data.mongodb.password = pass 
相關問題