【问题标题】:How do I fix the "ConnectionError: failed to connect to <server>:<port> in 15000ms" error?如何修复“ConnectionError: failed to connect to <server>:<port> in 15000ms”错误?
【发布时间】:2021-07-31 15:56:10
【问题描述】:

第一次为 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 配置管理器:

【问题讨论】:

  • 假设 Azure 中的防火墙允许您的 IP 连接到远程实例?
  • “我该如何解决”,这取决于问题所在。这不是编程问题。
  • @Larnu - 是的,Azure 防火墙已设置为允许我的 IP。我实际上可以在 SSMS 中成功运行对 Azure 数据库的查询。
  • 等等,我刚刚注意到,您提供了用户名和密码指定trustedConnection: true;那没有意义。您可以提供用户名和密码使用受信任的连接。
  • 我删除了trustedConnection: true,但仍然出现错误。

标签: node.js sql-server azure azure-sql-database


【解决方案1】:

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

假设你已经设置了合适的环境变量,你可以构造一个config对象如下:

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2018-10-04
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多