Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring connect to mongodb using username and password

I've got mongo DB running on docker. To set username and password I set MONGO_INITDB_DATABASE, MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD variables.

And now on spring boot application a can use URI: mongodb://username:password@host:27017/db_name?authSource=admin and it's working.

Now I want changed properties and it looks like:

spring:
  data:
    mongodb:
      authentication-database: admin
      host: host
      database: db_name
      password: password
      port: 27107
      username: username

Any ideas what is wrong?

springboot: 2.2.5 spring-boot-starter-data-mongodb-reactive

like image 588
Arek Szast Avatar asked Sep 17 '25 13:09

Arek Szast


1 Answers

I found that on the documentation:

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.

So probably this is the problem and for now I just stay with the URI.

like image 80
Arek Szast Avatar answered Sep 20 '25 05:09

Arek Szast