Node.js服务连接SQL Server报ECONNRESET错误无法连接求助
问题核心原因
你使用了错误的数据库驱动包:mysql是专门用于连接MySQL数据库的依赖,而你实际使用的是SQL Server 2019,二者底层通信协议完全不兼容,这是触发ECONNRESET报错的根本原因。
修复步骤
替换驱动包
先卸载错误的MySQL驱动:npm uninstall mysql安装SQL Server官方适配的Node.js驱动:
npm install mssql修改数据库连接代码
参考示例如下: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); });额外配置检查项
- 确认SQL Server身份验证模式为混合模式(SQL Server和Windows身份验证),如果使用Windows账号登录,可在连接配置中添加
integratedSecurity: true参数,无需填写账号密码 - 确认SQL Server Browser服务处于运行状态
- 确认你的登录账号拥有目标数据库的访问、操作权限
内容的提问来源于stack exchange,提问作者Thom
相关产品推荐
相关产品推荐

