You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Node.js连接Microsoft SQL Server问题咨询

命令行可连接SQL Server但Node.js代码超时的问题分析

我能通过命令提示符成功查询chem数据库的Questions表数据,执行命令如下:

sqlcmd -S DESKTOP-7OPSCVV\SQLEXPRESS -U test -P test -d chem -Q "SELECT * FROM Questions"

此前已完成以下配置:

  • 启用1433端口
  • SQL Server配置中允许TCP连接
  • 在SQL Server管理器中为test用户授予chem数据库权限

但使用以下Node.js代码时,出现连接超时错误:

const express = require('express');
const sql = require('mssql');

const config = {
    server: 'DESKTOP-7OPSCVV\\SQLEXPRESS',
    database: 'chem',
    user: 'test',
    password: 'test',
    options: {
      trustServerCertificate: true // 若未使用可信证书请改为'false'
    }
  };

const app = express();

app.get('/data', async (req, res) => {
  try {
    await sql.connect(config);
    const result = await sql.query('SELECT * FROM Questions');
    res.json(result.recordset);
  } catch (error) {
    console.error(error);
    res.status(500).send('Error retrieving data from the database2');
  }
});

错误信息如下:

> server@1.0.0 start
> node server.js

Server listening on port 3000
ConnectionError: Failed to connect to DESKTOP-7OPSCVV\SQLEXPRESS in 15000ms
    at D:\ochem\exam\server\node_modules\mssql\lib\tedious\connection-pool.js:85:17
    at Connection.onConnect (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:838:9)
    at Object.onceWrapper (node:events:629:26)
    at Connection.emit (node:events:514:28)
    at Connection.emit (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:959:18)
    at Connection.connectTimeout (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:1209:10)
    at Timeout._onTimeout (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:1154:12)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  code: 'ETIMEOUT',
  originalError: ConnectionError: Failed to connect to DESKTOP-7OPSCVV\SQLEXPRESS in 15000ms
      at Connection.connectTimeout (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:1209:26)
      at Timeout._onTimeout (D:\ochem\exam\server\node_modules\tedious\lib\connection.js:1154:12)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    code: 'ETIMEOUT'
  }
}

核心疑问:既然命令行可正常连接,为何Node.js代码无法建立连接?

可能的原因及解决办法

  • 明确指定TCP端口:SQL Server Express默认可能使用动态端口,而非固定1433。命令行的sqlcmd能自动发现实例,但mssql库需要明确端口。可以在配置中添加port: 1433,或者前往SQL Server配置管理器查看实例实际使用的TCP端口并填入。也可以直接将server改为DESKTOP-7OPSCVV,1433(去掉\SQLEXPRESS,用逗号分隔主机和端口)。
  • 检查实例名称配置:在config.options中添加instanceName: 'SQLEXPRESS',确保库能正确识别命名实例。
  • 验证TCP连接可达性:用telnet DESKTOP-7OPSCVV 1433或Test-NetConnection DESKTOP-7OPSCVV -Port 1433测试端口是否能通。命令行可能用了命名管道而非TCP连接,但mssql默认使用TCP,需确认TCP连接正常。
  • 排查防火墙规则:确保本地防火墙允许Node.js进程访问SQL Server的端口,部分情况下命令行工具被放行,但Node.js进程未被允许。
  • 调整连接超时时间:在config中添加connectionTimeout: 30000(设置为30秒),排除因超时时间过短导致的问题,但这只是临时排查手段,核心还是要修正连接配置。

内容的提问来源于stack exchange,提问作者KaneHarry729

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 17:57:11