rakamakafo
rakamakafo

Reputation: 1154

NodeJS Connecting to SQL Server Port Not Found error

I'm trying to connect to SQL Server:

var sql = require("mssql");

var dbConfig = {
    server: "LAP12\\INSTANCE1",
    database: "SampleDb",
    port: 1433,
    options: {
        trustedConnection: true
    }
};

// connect to your database
sql.connect(dbConfig, function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from SampleTable', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});

But I receive this error:

ConnectionError: Port for INSTANCE1 not found in ServerName...

I've tried to follow instructions here https://github.com/patriksimek/node-mssql/issues/130 , but that didn't help. TCP is enabled.

Changing config to this didn't help either:

var dbConfig = {
    server: "LAP12",
    port: 1433,
    options: {
        instanceName: 'INSTANCE1',
        database: 'SampleDb',
        trustedConnection: true,
    }
};

Upvotes: 2

Views: 13280

Answers (5)

It worked perfectly for me on SQL Server 2022 and NodeJS v18.17.1. Just don't forget to setup the port on your TCP/IP connection.

Menu: SQL Server Configuration Manager > SQL Server Network Configuration > TCP/IP > Properties > IP Address > TCP Port: 1433

This is my DB Config setting on NodeJS:

const dbconfig = {
  user: "YourUser", 
  password: "YourPass",
  server: "YourServer", 
  port: 1433, 
  database: "YourDatabase",
  options: {
    trustServerCertificate: true, 
  }
}

Hope it helps!

Upvotes: 1

Amine Kadache
Amine Kadache

Reputation: 1

you have to set the port number in the sql server Management Studio Management Studio => TCP/IP enter image description here

Upvotes: 0

Michael Maier
Michael Maier

Reputation: 238

In my case the "SQL Browser" is not reachable because the firewall port is not open. Solution for me is to open the port or change dbConfig to (no instance name, no port):

var dbConfig = {
    server: "LAP12",
    database: "SampleDb",
    options: {
        trustedConnection: true
    }
};

The port is still closed to SQL-Browser, with that change I can connect to my DB.

My nodejs Package 'mssql'

Upvotes: 0

Sheela K R
Sheela K R

Reputation: 79

after adding this encrypt: false, application not showing any error neither responding, Its not connecting to DB

Upvotes: 0

Cassius Horvath
Cassius Horvath

Reputation: 119

Ok, I had the same issue, will try to help. this is my config exemple

const config = {
    user: 'sa',
    password: '****',
    server: 'DESKTOP-Q5TO47P',
    database: 'dbname',
    options: {           
        encrypt: false
    }
};

You need to turn on the SQL Server Browser. Go to start up menu or the search and look for SQL Server Configuration Manager. Run it! (Im using 2018 version)

  • In the left Tab click on SQL Server Services
  • now in the right tab double click on SQL Server Browser
  • will open a window, you will see 3 tabs, go for the Service tab
  • change start mode to Automatic and apply
  • left click on SQL Server Browser and click restart

  • Back to the right tab click on SQL Server Network Configuration

  • then Client Protocols
  • change TCP/IP to enable

Let me know if it works.

Upvotes: 2

Related Questions