2

I'm trying to create a connection with SQL Server and NodeJS as shown below (SQL Server installed locally -> localhost):

It's throwing the following error: ERROR: Failed to connect to MY_SERVER:1433 - Could not connect (sequence)

This didn't work either.

I thought it was a problem with the SQL Server installation, but via Python It works well on the same database. I'm using SQL Server 2019 and latest NodeJS version.

REFERENCE: https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15

How to fix this error?

var config = {  
    server: 'MY_SERVER', 
    authentication: {
        type: 'default',
        options: {
            userName: 'sa',
            password: 'admin'  
        }
    },
    options: {
        encrypt: false,  // I've already tried with "true" also.
        database: 'MyDataBase'
    }
};

var connection = new Connection(config);  
connection.on('connect', function(err) {  
    if (err) {
        console.log("Error: ", err.message);  
    } else {
        console.log("Connected");  
    }    
});

connection.connect();
6
  • Since you haven't specified either a port or an instance name in the config, is the "SQL Server Browser" service running on the target machine? Commented Aug 6, 2021 at 2:24
  • Yes. I'm connected on database with SQL Server Management Studio. I can read data, create table, etc. Commented Aug 6, 2021 at 2:37
  • SSMS connects using a different database driver. Please check that the "SQL Server Browser" service is running, as opposed to the "SQL Server" service that's responsible for the database storage. You can see these and other SQL-related services in the SQL Server Configuration Manager tool. Commented Aug 6, 2021 at 7:05
  • Now all services are running, but before SQL Server Browser service was stopped. I tried again and the error remains :( . In Python I keep accessing the same database without any problems. Commented Aug 6, 2021 at 11:43
  • 2
    I got it!! Basically, for NodeJS get connection to SQLServer, the TCP/IP Protocol must be enabled for the MSSQLSERVER Instance, in the SQL Server network settings. Ref.: stackoverflow.com/questions/50853516/… and store.oceansystems.com/knowledgebase/quickdme-faqs/…. Thanks! Commented Aug 7, 2021 at 0:06

1 Answer 1

4

I got it!! Basically, for NodeJS get connection to SQLServer, the TCP/IP Protocol must be enabled for the MSSQLSERVER Instance, in the SQL Server network settings.

Ref.:

Nodejs connection with mssql showing error

https://store.oceansystems.com/knowledgebase/quickdme-faqs/sql-server-sql-express/configure-sql-express-server-host-enable-tcp-ip-firewall-settings/

Sign up to request clarification or add additional context in comments.

1 Comment

The answer you linked is a little unclear. Did you need to go to the IPALL section and set the TCP dynamic port to blank and the TCP port to 1433?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.