简体   繁体   中英

How do I fix the "ConnectionError: failed to connect to <server>:<port> in 15000ms" error?

First time creating a Node/Express api to an Azure SQL Server database. It seems like this is just a matter of extending the timeout limit, but it's also just a connection to the database and not an actual query. Is this normal for connecting to Azure and is it actually an issue that extending the timeout will fix or is something else the problem?

index.js

var express = require('express');
var router = express.Router();
const sql = require("../dboperation")

/* GET root route */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// Test db connection
router.get('/testconnect', function(req, res, next) {
  sql.getdata();
  res.render('index', { title: 'Express' });
});

module.exports = router;

dboperation.js

var config = require("./dbconfig")
const sql = require("mssql")


async function getdata(){
    try {
        let pool = await sql.connect(config)
        console.log("SQL Server connnected...")
    } catch(error) {
        console.log("error: " + error)
    }
}

module.exports = {
    getdata: getdata,
}

dbconfig.js

const config = {
    user : "username",
    password : "password",
    server : "server-name.database.windows.net",
    database : "database-name",
    options: {
        trustedConnection: true, 
        enableArithAort: true,
        encrypt: true
    },
    port: 49678
}

module.exports = config;

SQL Server Configuration Manager:

在此处输入图片说明

在此处输入图片说明

You can try by extending timeout once.

Assuming you have set the appropriate environment variables, you can construct a config object as follows:

const sql = require('mssql')

const sqlConfig = {

  user: process.env.DB_USER,

  password: process.env.DB_PWD,

  database: process.env.DB_NAME,

  server: 'localhost',

  pool: {

    max: 10,

    min: 0,

    idleTimeoutMillis: 30000

  },

  options: {

    encrypt: true, // for azure

    trustServerCertificate: false // change to true for local dev / self-signed certs

  }

}



async () => {

 try {

  // make sure that any items are correctly URL encoded in the connection string

  await sql.connect(sqlConfig)

  const result = await sql.query`select * from mytable where id = ${value}`

  console.dir(result)

 } catch (err) {

  // ... error checks

 }

}

Reference link - https://tediousjs.github.io/node-mssql/

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