DevProf
DevProf

Reputation: 874

Nodejs connection with mssql showing error

I am trying to connect with my sql server using nodejs, with the following code.

const sql = require('mssql');

var config = {
user: 'user',
password: 'psw',
server: 'localhost', 
database: 'sumit' 
};


var mssql = new sql.ConnectionPool(config);
mssql.connect().then(function(){
var req = new sqlDb.Request(conn);
req.query("SELECT * from Category").then(function(res){
    console.table(res);
});
}).catch(function(err){
console.log(err);
})

but everytime it is showing

ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)

how should i connect with my sql server? i have tried to change config also

var config = {
  user: 'user',
  password: 'psw',
  server: 'DESKTOP-VVN4PRG\\SQLEXPRESS', 
  database: 'sumit' 
 };

but still getting same error.

Upvotes: 3

Views: 14697

Answers (3)

Marlon Fez
Marlon Fez

Reputation: 41

In my case I had to do both things mentioned on the 2 answers above. I had to fix the IPALL on step 2 for answer 1. And turn on the Sql Server Browser service too. Without this service the mssql would not connect. This makes sense to me after read what this service does. Found this Microsoft article How SQL Server Browser works

Upvotes: 0

Isaac Gachugu
Isaac Gachugu

Reputation: 149

I struggled for more than 2 hours and finally landed on this solution to enable SQLBrowser and it worked like magicenter link description here

Upvotes: 1

DevProf
DevProf

Reputation: 874

I resolved my problem. I am putting my answer here, So maybe it will help someone.

Step 1: Check if you have selected SQL Server and Windows Authentication mode on Server Authentication section

Path: SMSS>Go to object explorer properties> Security(Check SQL Server and Windows Authentication mode, if not, then check it)

Step 2: Check if you have TCP Port 1433 and TCP Dynamic Port is Blank(not 0)

Path: SQL Server Configuration Manager>SQL Server Network Configuration>Protocols for SQLEXPRESS> Double click on TCP/IP>IP Addressess> Check for IPALL and for 127.0.0.1

Step 3: If you have performed any of the above or both the operation then please restart all your SQL Services by going

Path: Window + R> type services.msc and hit enter> Check for your SQL Services and restart all

Hope this would help someone.

Upvotes: 16

Related Questions