Error: read ECONNRESET while connecting to azure sql

louixa 1 Reputation point
2022-02-20T05:44:54.967+00:00

I've created and tested MySql scripts it works fine on localhost but when I try to connect to azure I get the following errors:

errno: -4077, code: 'ECONNRESET', syscall: 'read', fatal: true;

Here's my code:

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

var config = {
    host: 'myhost',
    database: 'mydb',
    user: 'myusername',
    password: 'mypass',
    port: 1433, 
    ssl: {
        rejectUnauthorized: true,
        ca: fs.readFileSync("my path to ssl")
    }
}

function initializeConnection(config) {
    function addDisconnectHandler(connection) {
        connection.on("error", function (error) {
            if (error instanceof Error) {
                if (error.code === "PROTOCOL_CONNECTION_LOST") {
                    console.error(error.stack);
                    console.log("Lost connection. Reconnecting..."); 

                    initializeConnection(connection.config);
                } else if (error.fatal) {  
                    error; 
                }
            }
        });
    }

    var connection = mysql.createConnection(config); 

    addDisconnectHandler(connection);

    connection.connect(
        function (err) { 
        if (err) { 
            console.log("!!! Cannot connect !!! Error:");
            throw err;
        }
        else
        {
           console.log("Connection established.");
        }
    }); 
    return connection;
}

var connection = initializeConnection(config);

module.exports.connection = connection; 
Azure SQL Database
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ronen Ariely 15,191 Reputation points
    2022-02-20T09:28:01.353+00:00

    Good day,

    There is a lot of confusion in your single post...

    I've created and tested MySql scripts

    MySQL has nothing to do with SQL Server or Azure SQL Database! It is totally different product from different company.

    MySQL: own by Oracle Corporation; https://en.wikipedia.org/wiki/MySQL

    SQL Server: own by Microsoft; https://en.wikipedia.org/wiki/Microsoft_SQL_Server

    Note: There is a service of MySQL in the Azure which call Azure Database for MySQL : https://azure.microsoft.com/en-us/services/mysql/

    it works fine on localhost but when I try to connect to azure

    Obviously two different product from two different companies use different leagues

    MySQL uses SQL and SQL Server uses TSQL

    Here's my code:

    This code is not related to SQL Server or to MySQL. In first glance it looks like node.js code. Therefore, your best option is to ask the question in node.js forums or in MySQL forums


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.