2

I want to connect to an online MongoDB database hosted at Mlab, from inside a Spring Boot application.

I have configured the application.properties with the URI:

spring.data.mongodb.uri = mongodb://myuser:mypassword@ds129532.mlab.com:29532/consilium-dev

However, the application still connects to the local MongoDB database. How can I make it connect to the Mlab database?

SOLUTION: The resources folder was not situated in the right folder. It should be in src/java/resources

Andrei Margeloiu
  • 849
  • 2
  • 12
  • 17
  • which version of spring data and mongodb java driver are you using? – harshavmb Jun 26 '17 at 13:59
  • @harshavmb, I'm using Spring Boot 1.5.4, and the MongoDB version from mLab is 3.2.14 – Andrei Margeloiu Jun 26 '17 at 14:00
  • Can you try this `# mongo connections spring.data.mongodb.host=ds129532.mlab.com spring.data.mongodb.port=29532 spring.data.mongodb.username=myuser spring.data.mongodb.password=mypassword spring.data.mongodb.database=consilium-dev` in your application.properties? – harshavmb Jun 26 '17 at 14:03
  • You can try this as well `spring.data.mongodb.uri=mongodb://myuser:mypassword@ds129532.mlab.com:29532 spring.data.mongodb.database=consilium-dev` – harshavmb Jun 26 '17 at 14:11
  • 1
    that is just a different way of writing the same thing. Your app is probably not picking up the config file correctly. Are you sure it is in the right location? You can test it easily by adding a `@Value("${something}") String something;` and adding the something tag to the properties file then printing it out. – p.streef Jun 26 '17 at 14:15
  • @p.streef, I think the application.poperties is not in the right directory. Now it's in src/main/resources. Is this correct? – Andrei Margeloiu Jun 26 '17 at 14:43
  • That is correct for a default spring boot app. I will delete that answer that should have been a comment when I'm able because the app won't let me... – p.streef Jun 26 '17 at 14:46
  • @p.streef, you were right. That was the problem. Thank you!!! – Andrei Margeloiu Jun 26 '17 at 15:09

2 Answers2

2

In my case, spring boot was connecting to the uri specified in application.properties file while testing in my PC but once deployed to heroku, it always connected to the localhost irrespective of what was specified in application.properties. Solution was to pass the database uri as command line arguments while deploying the jar to the server because this will take precedence over the properties. To do so, create a Procfile like:

web: java -Dserver.port=$PORT -Dspring.data.mongodb.uri=mongodb://<user>:<pass>@<host>:<port>/<db> -jar my-app.jar

And using heroku toolkit, run following command.

heroku deploy:jar -j my-app.jar -i Procfile --app <host-name>
realpac
  • 537
  • 6
  • 13
1

Using database values in the application.properties didn't work for me for online mongodb. It works fine for local db. But Once I google and found an example online where they added it in this way below and it worked for me.

spring.data.mongodb.uri=mongodb://<USERNAME>:<PASSWORD>@ds261828.mlab.com:61828/springdb.
Sanjay
  • 117
  • 4
  • 13