Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure MongoDB in Spring Boot using environment variables

I'm new to Spring Boot and am trying to configure a connection to MongoDB using environment variables - i.e. I have followed an example online showing how to configure my mongo database/host/port via application.properties, but I want my configuration to come from environment variables - what is the best approach to do this? Thanks

like image 485
finbin Avatar asked Dec 19 '22 08:12

finbin


1 Answers

After some digging, I've managed to get it working by using an application.properties file, but specifying the env variable names within the file, e.g.

spring.data.mongodb.database = ${SPRING_DATA_MONGODB_DATABASE}
spring.data.mongodb.host = ${SPRING_DATA_MONGODB_HOST}
spring.data.mongodb.port = ${SPRING_DATA_MONGODB_PORT}
like image 194
finbin Avatar answered May 09 '23 08:05

finbin