Question

How do I access a mysql managed db via mysql2 from node.js

i am currently trying to make a raffle ticket system and need to write the ticket numbers to a sql database. However I keep getting an error about self signing certificates. I have downloaded the CA certificate and put its contents into a envelope variable. Everything works fine in development but in production I am getting the self signing certificate error. I am using vercel to host the Web app. Below is the error and the database config.

[nuxt] [request error] [unhandled] [500] self-signed certificate in certificate chain
  at Object.createConnectionPromise [as createConnection] (./node_modules/mysql2/promise.js:18:31)  
  at Object.handler (./chunks/routes/api/webhook.mjs:60:34)  
  at Object.handler (./chunks/runtime.mjs:2837:24)  
  at Object.handler (./chunks/runtime.mjs:3146:34)  
  at Object.handler (./chunks/runtime.mjs:2908:31)  
  at async toNodeHandle (./chunks/runtime.mjs:3178:7)  
  at async Server.<anonymous> (/opt/rust/nodejs.js:9:6760)

const dbConfig={
    host:process.env.MYSQL_HOST,
    user:process.env.MYSQL_USER,
    password:process.env.MYSQL_PASSWORD,
    database:process.env.MYSQL_DATABASE,
    port:process.env.MYSQL_PORT,
    ssl:{
        ca:process.env.CA_CERT,
    }
};

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.

KFSys
Site Moderator
Site Moderator badge
December 21, 2024

Heya,

No an expert here but Try adjusting your dbConfig to add rejectUnauthorized: true explicitly. This ensures strict verification.

const dbConfig = {
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE,
    port: process.env.MYSQL_PORT,
    ssl: {
        ca: process.env.CA_CERT, // Your CA certificate content
        rejectUnauthorized: true, // Ensures the client validates the server's certificate
    },
};

and make sure CA_CERT environment variable contains the full content of your downloaded CA certificate.

export CA_CERT=$(cat /path/to/ca-certificate.pem)
Bobby Iliev
Site Moderator
Site Moderator badge
December 22, 2024

Hi there,

You’ve already added the CA certificate to your environment variable, which is great! Now, you need to tweak your ssl configuration slightly to ensure Node.js trusts the certificate chain.

Here’s the updated configuration for your dbConfig:

const dbConfig = {
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE,
    port: process.env.MYSQL_PORT,
    ssl: {
        rejectUnauthorized: true,
        ca: process.env.CA_CERT,
    },
};

Let me know how it goes!

- Bobby

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.