Home > Net >  Node server not connecting to SQL Server
Node server not connecting to SQL Server

Time:11-15

This is my first time trying to setup a NodeJS API to connect my Vue webapp to my SQL database.

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:

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!

CodePudding user response:

  1. Use SQL Server drivers, not MySql Drivers.

  2. If you can connect on port 1433 then the config should be:

:

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. But you can bypass this by using a port number (1433 is the default so may be omitted) instead of an instance name.

And Powershell's Test-NetConnection is your friend. eg

test-netconnection someserver -port 1433  
  • Related