4

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

Sedky A
  • 184
  • 2
  • 15
  • Did you try to split the host and port in `spring.data.mongodb.port=`? From the documentation, it seems like both host and port are expected: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html#boot-features-connecting-to-mongodb – Vlad Topala Jan 11 '19 at 14:50
  • I didn't add the port because it is just default (27017). – Sedky A Jan 11 '19 at 14:51
  • You're right, it says that `If spring.data.mongodb.port is not specified, the default of 27017 is used. You could delete this line from the example shown earlier.`. Missed that part initially. – Vlad Topala Jan 11 '19 at 14:52
  • Does the URL form of the user or password contain any % or + characters? – Kenster Jan 11 '19 at 14:52
  • Nope! username and password are just numbers and letters. hostname has a "." – Sedky A Jan 11 '19 at 14:59
  • Can you add the complete uri ? does your property file include both authentication database and application database ? What is the authentication mechanism do you use ? Would it possible for you to create a github project ? – s7vr Jan 14 '19 at 23:38
  • I can't really add the complete uri because it belongs to my employer. ALSO, I should mention that it works in one environment but not the other. – Sedky A Jan 15 '19 at 06:13
  • What the spring-boot version ? – IMParasharG Jan 21 '19 at 13:46

3 Answers3

2

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.

fhery021
  • 317
  • 2
  • 7
  • The docs are incorrect. I can connect just fine using the .host and .port convention on my lower environment. It is my higher environment (setup the same, using the exact same docker image) that will not work. – Sedky A Jan 15 '19 at 17:54
  • @SedkyA, it seems that your database isn't reachable from your "higher environment". – Fabio Manzano Jan 15 '19 at 18:35
  • @SedkyA, Can you detail what higher environment means? – fhery021 Jan 15 '19 at 18:57
  • It's essentially an environment with more nodes in the Swarm. It's irrelevant, all it means is that it is another environment with the exact same architecture. One I can connect to using host/user/pwd, the other I have to use URI – Sedky A Jan 15 '19 at 19:50
0

I can not comment so have to answer.

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

ville
  • 32
  • 4
  • I have tried it with no luck. Notice I haven't added it to the URI yet I can still connect. – Sedky A Jan 11 '19 at 15:30
  • If I leave database I got authentication failure. But I have multiple databases. – ville Jan 11 '19 at 15:56
  • That makes sense. I've tried it with setting the datrabase and the authentication-database. Still getting auth failure. – Sedky A Jan 11 '19 at 16:23
  • Have you already read https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html. 31.2.1 Connecting to a MongoDB Database . – ville Jan 12 '19 at 07:02
0

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.

Oussama
  • 603
  • 6
  • 10
  • 18