Sedky A
Sedky A

Reputation: 174

Java Spring / MongoDb only works when using URI field

I have a very weird situation. I'm running a Java spring-boot app that connects to a MongoDb instance. When I use the

spring.data.mongodb.uri=mongodb://{username}:{password}@{host}

field, I connect fine. But when I use:

spring.data.mongodb.host={host}
spring.data.mongodb.username={username}
spring.data.mongodb.password={password}

I get an authentication failure. What gives? Of course the values in the braces are exactly the same.

I included these in both settings:

spring.data.mongodb.authentication-database={auth-db}
spring.data.mongodb.database={mydb}

Other Details

The non-uri method works in one environment but not the one I am trying to fix.

Running both servers in the same Docker Swarm

Mongo ReplicaSet version 4.0.4

Java 8

Upvotes: 4

Views: 4232

Answers (3)

Oussama
Oussama

Reputation: 633

Try this

spring.data.mongodb.host={host}
spring.data.mongodb.port={port}
spring.data.mongodb.username={username}
spring.data.mongodb.password={password}
spring.data.mongodb.database={database}

This is working for me.

Upvotes: 0

fhery021
fhery021

Reputation: 317

It looks like this is not supported anymore. Citing from docs.spring.io - 31.2.1 Connecting to a MongoDB Database:

If you use the Mongo 3.0 Java driver, spring.data.mongodb.host and spring.data.mongodb.port are not supported. In such cases, spring.data.mongodb.uri should be used to provide all of the configuration.

Upvotes: 2

ville
ville

Reputation: 57

I can not comment so have to answer.

Try to add your database name? Hope that work. spring.data.mongodb.database=

Upvotes: 0

Related Questions