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

Node.js服务连接SQL Server报ECONNRESET错误无法连接求助

问题核心原因

你使用了错误的数据库驱动包:mysql是专门用于连接MySQL数据库的依赖,而你实际使用的是SQL Server 2019,二者底层通信协议完全不兼容,这是触发ECONNRESET报错的根本原因。

修复步骤
  1. 替换驱动包
    先卸载错误的MySQL驱动:

    npm uninstall mysql
    

    安装SQL Server官方适配的Node.js驱动:

    npm install mssql
    
  2. 修改数据库连接代码
    参考示例如下:

    const sql = require("mssql");
    const dbConfig = require("../config/db.config.js");
    
    // 连接配置适配mssql格式
    const connectionConfig = {
        server: dbConfig.HOST,
        user: dbConfig.USER,
        password: dbConfig.PASSWORD,
        database: dbConfig.DB,
        port: 1433,
        options: {
            trustServerCertificate: true // 本地开发环境忽略证书校验
        }
    };
    
    // 建立连接
    sql.connect(connectionConfig).then(pool => {
        if (pool.connected) {
            console.log("Successfully connected to the database.");
        }
    }).catch(error => {
        console.error("Database connection failed:", error);
    });
    
  3. 额外配置检查项

  • 确认SQL Server身份验证模式为混合模式(SQL Server和Windows身份验证),如果使用Windows账号登录,可在连接配置中添加integratedSecurity: true参数,无需填写账号密码
  • 确认SQL Server Browser服务处于运行状态
  • 确认你的登录账号拥有目标数据库的访问、操作权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 01:24:04