如何修复“ConnectionError: failed to connect to <server>:<port> in 15000ms”错误?

问题描述 投票:0回答:2

首次创建 Azure SQL Server 数据库的 Node/Express api。看起来这只是延长超时限制的问题,但它也只是与数据库的连接而不是实际的查询。连接到 Azure 时这是正常现象吗?延长超时实际上可以解决问题还是其他问题?

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 配置管理器:

enter image description here

enter image description here

node.js sql-server azure azure-sql-database
2个回答
1
投票

您可以尝试延长一次超时时间。

假设您已经设置了适当的环境变量,您可以按如下方式构建配置对象:

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

 }

}

参考链接 - https://tediousjs.github.io/node-mssql/


0
投票

奇怪的设定

new sql.ConnectionPool({
                                 database: DB_DATABASE
                                ,server: DB_SERVER
                                ,requestTimeout: 0
                  ---> this     ,connectionTimeout: 10*1000 <-- this
                                ,driver: "msnodesqlv8"
                                ,options: {
                                        trustedConnection: true
                                }

非零为我解决了这个问题。它正在为不同的 AD 帐户使用 0 值。仅特定用户需要设置为非零值。

© www.soinside.com 2019 - 2024. All rights reserved.