kallis
kallis

Reputation: 133

How to connect azure mysql server with nodeJS

I am trying to connect to a remote mysql server with nodejs

So far I am able to connect with mysql workbench 8.0, so no problem with mysql server. And I can easily connect to my local mysql-server (with "mysql2" module).

Here is something I've been trying to connect with remote mysql server provided by Azure. I followed the guides that is provided.

const mysql = require("mysql");
const fs = require("fs")

var connection = mysql.createConnection({
    host: process.env.MYSQL_SERVER_HOST,
    user: process.env.MYSQL_SERVER_USER,
    password: process.env.MYSQL_SERVER_PASSWORD,
    database: process.env.MYSQL_SERVER_DATABASE,
    port:3306,
    ssl: { ca: fs.readFileSync("./certificates/DigiCertGlobalRootCA.crt.pem")}
});

// This is how I am connecting to my local mysql-server which is working fine
// // const connection = mysql.createConnection({
//     host: 'localhost',
//     user: 'root',
//     password: process.env.LOCAL_MYSQL_SERVER_PASSWORD,
//     database: 'db',
//     port: '3306'
// });

connection.connect((error) => {
    if (error) {
        console.log("mysql error")
        console.log(error)
    }
    else {
        console.log("Mysql Ready")
    }
});

module.exports = connection;

Here I am using mysql package because many articles on web suggested it but I even used mysql2 and nothing happened. I even changed the networking setting and allowed my IP address for access(this wasn't necessary though).

Error logs:

mysql error
Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (path\node_modules\mysql\lib\Connection.js:409:13)
    at Object.onceWrapper (node:events:641:28)
    at Socket.emit (node:events:527:28)
    at Socket._onTimeout (node:net:516:8)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)
    --------------------
    at Protocol._enqueue (path\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (path\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (path\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (path\packnary\database\mysql.js:26:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18) {
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true
}

Upvotes: 0

Views: 617

Answers (1)

Bhavani
Bhavani

Reputation: 5297

I connected Azure SQL database to the nodejs without using code.

I followed below procedure:

I have created azure blank nodeJs webapp in visual studio.

enter image description here

It run successfully. after that I go to overview of the webapp and selected connected services and clicked on a add a service dependency option and searched for azure sql database.

Image for reference:

enter image description here

enter image description here

select the azure sql data and click on next button. login to azure account and select which database we need to connect.

Image for reference:

enter image description here

Enter the details required in the next window and click on next.

Image for reference:

enter image description here

Select the changes according to the requirements and click on next.

Image for reference:

enter image description here

It is connected successfully.

Image for reference:

enter image description here

enter image description here

Upvotes: 0

Related Questions