简体   繁体   中英

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

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.

I can not comment so have to answer.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM