简体   繁体   English

节点服务器未连接到 SQL Server

[英]Node server not connecting to SQL Server

This is my first time trying to setup a NodeJS API to connect my Vue webapp to my SQL database.这是我第一次尝试设置 NodeJS API 来将我的 Vue webapp 连接到我的 SQL 数据库。

Running my server with this config:使用此配置运行我的服务器:

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."); });

Where dbConfig is: dbConfig 在哪里:

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

I'm getting this error after a minute or so when trying to open the connection:尝试打开连接时,大约一分钟后我收到此错误:

    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:*

I've tried the following:我尝试了以下方法:

I'm clueless as to why this keeps happening.我不知道为什么这种情况不断发生。 I'm sure it's something dumb I'm somehow missing but for the life of me I can't figure this out, so any and all suggestions are more than welcome.我确定这是我不知何故想念的愚蠢的东西,但对于我的生活,我无法弄清楚这一点,因此非常欢迎任何和所有建议。

Thanks in advance and sorry if something isn't clear, I'll clarify if needed!提前致谢,如果有不清楚的地方,我会在需要时澄清!

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

  2. If you can connect on port 1433 then the config should be:如果您可以在端口 1433 上连接,那么配置应该是:

:

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

Using the instance name requires connecting to the SQL Browser on UDP port 1434, and the Browser must be running.使用实例名称需要在 UDP 端口 1434 上连接到 SQL 浏览器,并且浏览器必须正在运行。 But you can bypass this by using a port number (1433 is the default so may be omitted) instead of an instance name.但是您可以通过使用端口号(1433 是默认值,因此可以省略)而不是实例名称来绕过它。

And Powershell's Test-NetConnection is your friend. Powershell 的Test-NetConnection是您的朋友。 eg例如

test-netconnection someserver -port 1433  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM