简体   繁体   中英

Failed to connect to mydb.database.windows.net:1433 in 15000ms (Microsoft Azure SQL Database)

I am able to retrieve data from Microsoft Azure SQL Database using below code: -

const sql = require("mssql");

var config = {
  user: "user_name",
  password: "Pass@1234",
  server: "mydb.database.windows.net",
  database: "db_name",
  options: {
    enableArithAbort: true,
  },
  stream: true,
};

module.exports = function getQueryResult(query) {
  return new Promise((res, rej) => {
    sql.connect(config).then((pool) => {
      pool.query(query, (err, result) => {
        if (err) rej(err);
        res(result);
      });
    });
  });
};

I am using getQueryResult function to get the data from database.

Everything is going perfect accept the thing that the below errors occurs in between.

  • Failed to connect to mydb.database.windows.net:1433 in 15000ms (Microsoft Azure SQL Database)

  • ConnectionError: Failed to connect to mydb.database.windows.net:1433 read ECONNRESET

  • ConnectionError: Failed to connect to mydb.database.windows.net:1433 socket hang up

I know this question has been asked before. But I have tried all the solutions. None of the solution was specifically for Microsoft Azure SQL Database so I thought might be there is some problem in database.

Thanks in advance.

Your code is a bit different from mine, my options is enclosed in double quotes. You also can download my sample code , it works for me, I have test it.

Tips:

You need set the rule of Firewalls . Make sure your local or webapp can access dbserver.

在此处输入图像描述

My code:

const sql = require('mssql')
const config = {
    user: 'username',
    password: 'pwd',
    server: '***sqlserver.database.windows.net', // You can use 'localhost\\instance' to connect to named instance
    database: 'yourdb',
    "options": {
        "encrypt": true,
        "enableArithAbort": true
    }
}

const poolPromise = new sql.ConnectionPool(config)
  .connect()
  .then(pool => {
    console.log('Connected to MSSQL')
    return pool
  })
  .catch(err => console.log('Database Connection Failed! Bad Config: ', err))

module.exports = {
  sql, poolPromise
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM