Report this

What is the reason for this report?

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

Posted on December 20, 2024

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,
    }
};


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!

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.

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)

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

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.