Invalid mongo configuration, either uri or host/port/credentials must be specified

16,379

Solution 1

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

Either use

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

Or

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

Solution 2

If your project has more then one application.properties/application.yml file, sometime they both conflicts if one has URI and other has Host/Port/Credential.

This result in the error "Invalid mongo configuration, either uri or host/port/credentials must be specified"

To Avoid the conflict, make sure you either use URI or Host/Port/Credential in all.

Solution 3

The issue was authentication-database was missing.

Here is now the working configuration :

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

Solution 4

If you are using application.properties file to store your configuration, Use the following structure.

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
Share:
16,379
Sofiane
Author by

Sofiane

Updated on July 11, 2022

Comments

  • Sofiane
    Sofiane almost 2 years

    I'm getting this exception :

    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
    

    Here is my application.yml content :

    spring:
      data:
        mongodb:
          uri: mongodb://develop:d3VeL0p$@<my_host>:27017/SHAM
    

    Here is my configuration class :

    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