Node.js连接SQL Server SQLEXPRESS端口查找失败求助
问题重现
连接时触发以下错误:
Failed to connect to SQL Server: ConnectionError: Port for SQLEXPRESS not found in DESKTOP-RE32RAN
[0] at C:\Users\lenovo\Desktop\fyp-main\node_modules\mssql\lib\tedious\connection-pool.js:85:17
[0] at Connection.onConnect (C:\Users\lenovo\Desktop\fyp-main\node_modules\tedious\lib\connection.js:838:9)
[0] at Object.onceWrapper (node:events:628:26)
[0] at Connection.emit (node:events:513:28)
[0] at Connection.emit (C:\Users\lenovo\Desktop\fyp-main\node_modules\tedious\lib\connection.js:959:18)
[0] at C:\Users\lenovo\Desktop\fyp-main\node_modules\tedious\lib\connection.js:995:16
[0] at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
[0] code: 'EINSTLOOKUP',
[0] originalError: ConnectionError: Port for SQLEXPRESS not found in DESKTOP-RE32RAN
[0] at C:\Users\lenovo\Desktop\fyp-main\node_modules\tedious\lib\connection.js:995:32
[0] at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
[0] code: 'EINSTLOOKUP'
[0] }
[0] }
同时存在SQL Server Network Configuration中TCP/IP选项缺失的情况,当前连接代码如下:
const config = { host: "localhost", server: "DESKTOP-RE32RAN\\SQLEXPRESS", database: "fyp", options: { trustedConnection: true, }, }; // SQL Server connection pool const pool = new sql.ConnectionPool(config); const poolConnect = pool.connect();
解决方案
1. 修复TCP/IP选项缺失问题
- 打开SQL Server安装中心(开始菜单搜索即可),选择「维护」→「添加功能到现有实例」。
- 在功能选择界面,勾选「数据库引擎服务」下的TCP/IP协议组件,完成安装后重启SQL Server服务。
- 若找不到安装中心,可通过控制面板→程序和功能→找到对应SQL Server实例→右键选择「更改」,进入功能添加流程。
2. 手动指定SQL Server端口
如果暂时无法恢复TCP/IP选项,或SQL Browser服务仍无法解析端口,可手动指定端口:
- 打开SSMS,连接
DESKTOP-RE32RAN\SQLEXPRESS实例。 - 右键实例→「属性」→「连接」,查看「TCP端口」;若使用动态端口,可在SQL Server配置管理器的「SQL Server网络配置」→「SQLEXPRESS的协议」→TCP/IP→「IP地址」标签下,查看IPAll的TCP动态端口。
- 修改连接配置,添加
port字段:
const config = { server: "DESKTOP-RE32RAN\\SQLEXPRESS", database: "fyp", port: 1433, // 替换为实际查到的端口 options: { trustedConnection: true, trustServerCertificate: true // 出现证书验证错误时添加 }, };
3. 验证关键服务状态
- 按Win+R输入
services.msc打开服务列表:- 确认
SQL Server (SQLEXPRESS)服务处于运行状态。 - 确认
SQL Server Browser服务处于运行状态(已启用则检查是否停止)。
- 确认
- 检查Windows防火墙:允许SQL Server端口(1433或动态端口)和SQL Server Browser的UDP端口1434通过。
4. 简化连接配置测试
尝试用localhost\SQLEXPRESS代替完整服务器名,减少解析问题:
const config = { server: "localhost\\SQLEXPRESS", database: "fyp", port: 1433, options: { trustedConnection: true, trustServerCertificate: true }, };
内容的提问来源于stack exchange,提问作者Ahmad Imran

