Question

Connect MongoDB DATABASE_URL on the App Platform

I have been trying everything to get my app to connect to the managed mongodb database. I have the client (app), server and the mongodb all configured in the same app. I have connected to the db just fine from my local machine with the certificate and everything.

In the env settings for the server, DO added one that says DATABASE_URL and the value is within ${db.DATABASE_URL} and another with ${db-mongodb.DATABASE_URL}. I pointed my server to connect with this env in the code and it seems to try to pull it out, but just won’t connect. I have trusted sources all set up.

First error I was getting was: “self signed certificate in certificate chain” Lastest error is: “All values of tls and ssl must be the same”

I have messed around and my latest attempt is like this:

${db-mongodb.DATABASE_URL}:${db-mongodb.PASSWORD}@db-mongodb.mongo.ondigitalocean.com/testing?authSource=admin&replicaSet=db-mongodb&tls=true&tlsCAFile=${db-mongodb.CA_CERT}

locally, connecting I can use the following and it connects: mongodb+srv://username:${process.env.MONGOPWD}@db-mongodb.mongo.ondigitalocean.com/testing?authSource=admin&replicaSet=db-mongodb&tls=true&tlsCAFile=${process.env.MONGOCRT}


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 11, 2022

Hi there,

You need to actually pass the path to the certificate file rather than the certificate string itself.

One way to do that is by creating the file first in your application’s code before actually initializing the MongoDB connection. For example:

fs.writeFileSync(path.resolve(__dirname, "mongodb.ca.crt"), process.env.CA_CERT)

Then pass the mongodb.ca.crt file to the connection string.

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up