节点服务器未连接到SQL server



这是我第一次尝试设置NodeJS API来将我的Vue Web应用程序连接到SQL数据库。

使用以下配置运行我的服务器:

const mysql = require("mysql"); const dbConfig = require("../config/db.config.js");
// Create a connection to the database const connection = mysql.createConnection({
host: dbConfig.HOST, //localhost
user: dbConfig.USER,    //LAPTOP-*********
password: dbConfig.PASSWORD, // ""
database: dbConfig.DB, //Applaudeme
port: 1433 });
// open the MySQL connection connection.connect(error => {
if (error) throw error;
console.log("Successfully connected to the database."); });

dbConfig所在位置:

module.exports = {
HOST: "localhost",
USER: "LAPTOP-**********",
PASSWORD: "",
DB: "*****",
};

大约一分钟后,当我试图打开连接时,我收到了这个错误:

if (error) throw error;
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:220:20)
--------------------
at Protocol._enqueue (C:UsersTomasDocumentsWebServerv2AppServernode_modulesmysqllibprotocolProtocol.js:144:48)
at Protocol.handshake (C:UsersTomasDocumentsWebServerv2AppServernode_modulesmysqllibprotocolProtocol.js:51:23)
at Connection.connect (C:UsersTomasDocumentsWebServerv2AppServernode_modulesmysqllibConnection.js:116:18)
at Object.<anonymous> (C:UsersTomasDocumentsWebServerv2AppServermodelsindex.js:14:12)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18) {   errno: -4077,   code: 'ECONNRESET',   syscall: 'read',   fatal: true } [1]+  Exit 127                SET DEBUG=webserver2:*

我试过以下几种:

  • SQL配置管理器服务

  • SQL配置管理器网络配置协议

  • 我已经重新安装了SQL Server(SQL Server 2019 Developer(和SQL Server Management Studio(v17.9(

  • 我已允许在防火墙上使用1433端口。

  • 不同的端口或默认端口没有改变任何东西(1433不是默认设置的(

我不知道为什么这种情况不断发生。我确信我不知何故错过了一些愚蠢的东西,但就我的一生而言,我无法理解这一点,所以任何建议都非常受欢迎。

提前感谢,如果有什么不清楚的地方,我会在需要的时候澄清!

  1. 使用SQL Server驱动程序,而不是MySql驱动程序。

  2. 如果您可以在端口1433上连接,那么配置应该是:

:

module.exports = {
HOST: "localhost",
USER: "LAPTOP-*****",
PASSWORD: "",
DB: "*****",
};

使用实例名称需要连接到UDP端口1434上的SQL浏览器,并且浏览器必须正在运行。但是,您可以使用端口号(1433是默认值,因此可能会被省略(而不是实例名称来绕过它。

Powershell的Test NetConnection是你的朋友。例如

test-netconnection someserver -port 1433  

最新更新