Unable to connect Azure MySQL DB to nodejs app, SSL Error

Adrien Zeixal 26 Reputation points
2022-12-25T20:10:11.397+00:00

Hey guys o/

So, I'm trying to connect my Azure MySQL DB to my nodejs app.

After following the microsoft tutorial on how to create an Azure MySQL server and a MySQL DB, I started to apply this guide : https://learn.microsoft.com/en-us/azure/mysql/single-server/connect-nodejs.

The guide worked just fine until I tried to execute this code :

   const mysql = require('mysql');  
   const fs = require('fs');  
     
   var config =  
   {  
       host: 'mydemoserver.mysql.database.azure.com',  
       user: 'myadmin@mydemoserver',  
       password: 'your_password',  
       database: 'quickstartdb',  
       port: 3306,  
       ssl: {ca: fs.readFileSync("your_path_to_ca_cert_file_BaltimoreCyberTrustRoot.crt.pem")}  
   };  
     
   const conn = new mysql.createConnection(config);  
     
   conn.connect(  
       function (err) {   
       if (err) {   
           console.log("!!! Cannot connect !!! Error:");  
           throw err;  
       }  
       else  
       {  
          console.log("Connection established.");  
              queryDatabase();  
       }  
   });  
     
   function queryDatabase(){  
       conn.query('DROP TABLE IF EXISTS inventory;', function (err, results, fields) {   
           if (err) throw err;   
           console.log('Dropped inventory table if existed.');  
       })  
           conn.query('CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);',   
               function (err, results, fields) {  
                   if (err) throw err;  
           console.log('Created inventory table.');  
       })  
       conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['banana', 150],   
               function (err, results, fields) {  
                   if (err) throw err;  
           else console.log('Inserted ' + results.affectedRows + ' row(s).');  
           })  
       conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['orange', 154],   
               function (err, results, fields) {  
                   if (err) throw err;  
           console.log('Inserted ' + results.affectedRows + ' row(s).');  
           })  
       conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['apple', 100],   
       function (err, results, fields) {  
                   if (err) throw err;  
           console.log('Inserted ' + results.affectedRows + ' row(s).');  
           })  
       conn.end(function (err) {   
       if (err) throw err;  
       else  console.log('Done.')   
       });  
   };  

It gives me back this error :

   !!! Cannot connect !!! Error:  
   /home/edecia/Documents/PTrans/termite/index.js:20  
           throw err;  
           ^  
     
   Error: self-signed certificate in certificate chain  
       at TLSSocket.<anonymous> (/home/edecia/Documents/PTrans/termite/node_modules/mysql/lib/Connection.js:317:48)  
       at TLSSocket.emit (node:events:513:28)  
       at TLSSocket._finishInit (node:_tls_wrap:959:8)  
       at ssl.onhandshakedone (node:_tls_wrap:743:12)  
       --------------------  
       at Protocol._enqueue (/home/edecia/Documents/PTrans/termite/node_modules/mysql/lib/protocol/Protocol.js:144:48)  
       at Protocol.handshake (/home/edecia/Documents/PTrans/termite/node_modules/mysql/lib/protocol/Protocol.js:51:23)  
       at Connection.connect (/home/edecia/Documents/PTrans/termite/node_modules/mysql/lib/Connection.js:116:18)  
       at Object.<anonymous> (/home/edecia/Documents/PTrans/termite/index.js:16:6)  
       at Module._compile (node:internal/modules/cjs/loader:1218:14)  
       at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)  
       at Module.load (node:internal/modules/cjs/loader:1081:32)  
       at Module._load (node:internal/modules/cjs/loader:922:12)  
       at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)  
       at node:internal/main/run_main_module:23:47 {  
     code: 'HANDSHAKE_SSL_ERROR',  
     fatal: true  
   }  
     
   Node.js v19.3.0  

The SSL certificate seems to be working and readable since I tried to connect MySQL Workbench with my Azure DB and it worked.

BTW, I'm on ubuntu 22.04.

So what's wrong ? How do I get access to my DB with my node app ? I could surely disable the SSL requirement but it would be safer if I could make it work with it.

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
986 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2022-12-26T06:47:14.783+00:00

    Hi @Adrien Zeixal ,

    Welcome to Microsoft Q&A platform and thanks for using Azure services.

    As I understand from the error "self-signed certificate in certificate chain".

    If the environment is Dev, add NODE_TLS_REJECT_UNAUTHORIZED='0' as an environment variable wherever you are running node or running node directly with NODE_TLS_REJECT_UNAUTHORIZED='0' node app.js

    If working in Production, see for a proper SSL Cert from a trusted source. letsencrypt.org is free, easy to set up and the keys can be automatically rotated.

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you.
      Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.