简体   繁体   中英

Cannot connect to SQL Server with Node.js and Tedious

When I try to use Node.js and Tedioius to connect to a local SQL Server instance I get this error:

{ [ConnectionError: Failed to connect to XXXXX:1433 - connect ECONNREFUSED]
  name: 'ConnectionError',
  message: 'Failed to connect to XXXXX:1433 - connect ECONNREFUSED',
  code: 'ESOCKET' }

Here is my connection object:

var config = {
  userName: 'username',
  password: 'password',
  server: 'XXXXX',

  options: {
    database: 'databasename',
    instancename: 'SQLEXPRESS'
  }
};

I have checked and TCP/IP is enabled and broadcasting on port 1443 according to Configuration Manager. The SQL Server Browser service is also running, which I read may be causing this type of issue if not. I have disabled my antivirus and firewall and that hasn't helped either.

Any insight?

So what I am guessing happens is that even though Tedious lets you include instance name in 'options' it either doesn't use it or can't use it as it needs to be used. After doing some research, what should be happening is when you give SQL Server the instance name, it redirects you from port 1433 to the dynamic port it is using for that instance. I didn't know it was using a dynamic port, but if your instance is named the port will always be dynamic. I don't know where I saw it broadcasting on 1433, that was my mistake.

To check the dynamic port, look here:

在此处输入图片说明

From this information, I changed my code to this:

var config = {
  userName: 'username',
  password: 'password',
  server: 'XXXXX',

  options: {
    port: 49175,
    database: 'databasename',
    instancename: 'SQLEXPRESS'
  }
};

All is good now, hope this helps someone.

If anyone else is new to SQL Server like I am, and is dealing with this issue, once you enable TCP/IP in SQL Server Config Manager by following these steps:

> SQL Server Network Config

> Protocols for YOURSQLSERVERINSTANCE

> TCP/IP

> Enable

you get a warning message that looks like this:

Any changes made will be saved; however, they will not take effect until the service is stopped and restarted.

I took this to mean, disconnect from the database service in SQL Server Management Studio and reconnect, but this needs to happen in SQL Server Config Manager under the SQL Server Services tab. Find you SQL Server instance, stop and restart it, and hopefully you will be golden! This worked like a charm for me. Oddly, enabling the Named Pipes protocol seemed to work without a restart (I could see the difference in the error message), so I thought for sure it had stopped and restarted as needed.

Also, be sure to enable SQL Server Browser services as well. This and enabling TCP/IP and restarting the service were the keys for me.

If you still have problems after enabling TCP/IP protocol, I would suggest you check that SQL Server Browser Service is running. In my case I spent a lot of time till I realised it wasn't running.

This configuration run fine for me:

 var config = {
     user: 'user',
     password: 'userPwd',
     server: 'localhost',
     database: 'myDatabase',
     options: {
         truestedConnection: true,
         instanceName: 'SQLEXPRESS'
    }

If you still got this error,

" ...'Failed to connect to Server:1433 - connect ECONNREFUSED Server IP:1433', code: 'ESOCKET' } "

and you've checked all the following:

  1. Enable TCP/IP
  2. Open Port 1433
  3. Config setup correctly (database, server, username and password}
  4. No Dynamic ports configured

Check your SQL server version. In my case, I discovered that I could connect to SQL 2012, but not SQL server 2016 with the same code. It appears SQL Server 2016 is not supported by the tedious driver yet.

... You have to enabled tcp/ip on protocol for MSSQLSERVER

在此处输入图片说明

and activate both authentication

在此处输入图片说明

here is the complete code

const {
    Request
} = require('tedious');

var Connection = require('tedious').Connection;
var config = {
    server: 'DESKTOP-RU9C12L', //update me
    authentication: {
        type: 'default',
        options: {
            userName: 'begaak', //update me
            password: 'begaak@123', //update me
        }
    },
    options: {
        encrypt: true,
        enableArithAbort: true,
        integratedSecurity: true,
        trustServerCertificate: true,
        rowCollectionOnDone: true,
        database: 'selvapoc' //update me
    }
};


var connection = new Connection(config);


connection.connect(function(err) {
    console.log('testing')
        // var request = new Request("Select * from products", function(err, rowCount, rows) {
        //     console.log(rowCount);
        //     console.log(JSON.stringify(rows))
        // });
        // connection.execSql(request);

    connection.execSql(new Request('SELECT * FROM Products', function(err, rowCount, rows) {
            if (err) {
                throw err;
            }
        })
        .on('doneInProc', function(rowCount, more, rows) {
            console.log(more, rows[0], rowCount); // not empty
        }));

});
connection.on('connect', function(err) {
    // If no error, then good to proceed.  
    if (err) console.log(err)
    console.log("Connected");
});

before starting the code configure these with SQL SERVER CONFIGURATION MANAGER在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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