mongo 配置无效,必须指定 uri 或 host/port/credentials [英] Invalid mongo configuration, either uri or host/port/credentials must be specified

查看:15
本文介绍了mongo 配置无效,必须指定 uri 或 host/port/credentials的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此异常:

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 内容:

Here is my application.yml content :

spring:
  data:
    mongodb:
      uri: mongodb://develop:d3VeL0p$@<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");
    }
}

现在我在没有失败的情况下启动时得到这个堆栈跟踪,看起来用户开发没有连接的权利:

Now I'm getting this stack trace when starting without failing, it looks like user develop doesn't have the right to connect:

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

推荐答案

您将 uri 样式连接设置与各个属性样式设置混合在一起.

You are mixing the uri style connection settings with the individual properties style settings.

要么使用

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

spring:
    data:
        mongodb:
            uri:mongodb://develop:pass@localhost:27017/SHAM_TEST

这篇关于mongo 配置无效,必须指定 uri 或 host/port/credentials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆