Sofiane
Sofiane

Reputation: 950

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

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

Upvotes: 8

Views: 24272

Answers (5)

Akhilesh
Akhilesh

Reputation: 109

This issue appears with mongo version 4.x, spring or spring boot supports version 2.x only so when we try to connect with mongo 4 this error appears:

Downgrade your mongo from 4.x to 3.x or lower.

If not, then change these placeholders.

Change from :

spring.data.mongodb.host= localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database= your db name
spring.data.mongodb.username= something
spring.data.mongodb.password= ***

-to-

mongo.replica.hosts=localhost:27017
mongo.dbname= your db name
mongo.username= something
mongo.password= ***

Upvotes: 1

Pallav
Pallav

Reputation: 342

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.

Upvotes: 10

Lanil Marasinghe
Lanil Marasinghe

Reputation: 2915

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

Upvotes: 4

Sofiane
Sofiane

Reputation: 950

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

Upvotes: 6

s7vr
s7vr

Reputation: 75914

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

Upvotes: 22

Related Questions