簡體   English   中英

節點服務器未連接到 SQL Server

[英]Node server not connecting to SQL Server

這是我第一次嘗試設置 NodeJS API 來將我的 Vue webapp 連接到我的 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:\Users\Tomas\Documents\WebServerv2\AppServer\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\Tomas\Documents\WebServerv2\AppServer\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\Tomas\Documents\WebServerv2\AppServer\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (C:\Users\Tomas\Documents\WebServerv2\AppServer\models\index.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:*

我嘗試了以下方法:

我不知道為什么這種情況不斷發生。 我確定這是我不知何故想念的愚蠢的東西,但對於我的生活,我無法弄清楚這一點,因此非常歡迎任何和所有建議。

提前致謝,如果有不清楚的地方,我會在需要時澄清!

  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  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM