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.