Question

How to add TLS/SSL cert for PlanetScale DB to App Platform

Hello I have DB that I want to connect to my Nodejs app running in the DO App Platform. I followed the concept in this post to add the env vars and CA path for allow my app to connect: https://www.digitalocean.com/community/questions/how-can-we-configure-planetscale-with-app-platform

However I think the CA path and env var may be different for Nodejs app vs Laravel like in that post. Has anyone had any luck connecting to PlanetScale DB’s via mysql2 in a Nodejs app before?

Any help is greatly appreciated 🙏


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
April 8, 2023
Accepted Answer

Hi there,

What I would personally do is to follow the steps from the official Planescale guide here:

https://planetscale.com/docs/tutorials/connect-nodejs-app

You can add a DATABASE_URL environment variable with the following content:

mysql://<USERNAME>:<PLAIN_TEXT_PASSWORD>@<ACCESS_HOST_URL>/<DATABASE_NAME>?ssl={"rejectUnauthorized":true}

And then reference it in your create connection statement as follows:

require('dotenv').config();
const express = require('express')
const app = express()
const port = process.env.PORT || 3000

const mysql = require('mysql2')
const connection = mysql.createConnection(process.env.DATABASE_URL);

connection.connect()

app.get('/', (req, res) => {
  connection.query('SELECT * FROM users', function (err, rows, fields) {
    if (err) throw err

    res.send(rows)
  })
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

They also have an example repo here:

https://github.com/planetscale/express-example/tree/main

Let me know how it goes!

An alternative option here would be to add your database details as separate environment variables as described in the discussion that you’ve linked to and then reference the CA file as follows:

const fs = require('fs');
const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  port: process.env.DB_PORT,
  ssl: {
    ca: fs.readFileSync(process.env.MYSQL_SSL_CA)
  }
});

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

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel