I am trying to connect to MSSQL 2012 using NodeJS with the mssql connection interface.
When attempting to connect I get the following error:
{ [Conn
Apart from setting TCP port no to 1433. If you are getting "Connection lost - Cannot call write after a stream was destroyed" error
This error will also come if you use options.encrypt: true on Node v12+ with old SQL Server versions.
This is caused by Node v12 requiring TLS 1.2.
run node with backwards compatibility flag:
node --tls-min-v1.0
eg: node --tls-min-v1.0 app.js
disable encrypted communication by setting
options.encrypt: false
(optional)
Config Object:
const config = {
user: '...',
password: '...',
server: 'localhost',
database: '...',
'options.encrypt': false
}
Ref: https://github.com/tediousjs/tedious/issues/903#issuecomment-614597523
Best practice is to first verify the connection to the SQL server using a query analyzer (SQL Management Studio (Windows) or SQLPro for MSSQL (Mac)) using the same protocol, port and credentials as you wish to use via your application.
In Management Studio, the format is Server,Port (e.g. 192.168.1.10,1433); and you'll probably be using SQL Server Authentication instead of Windows Authentication.
Steps to configure the SQL Server:
Install with Mixed Authentication, if you intend to use SQL Server Authentication.
Setup SQL Server to listen on TCP on a fixed port number:
My case wasn't exactly the same as Matt's, but his screenshot was enough to remember me what was missing.
As it is said here, when you are using the SQL Server Instance Name to connect to it, you must have SQL Server Browser running.
options.instanceName
The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1434 on the database server must be reachable.
(no default)
Mutually exclusive with options.port.
If somebody still struggles to connect despite doing all that was proposed.
In my case I had to manually set TCP Port property to 1433 in SQL Server Network Configuration -> Protocols for ... -> TCP/IP -> IP Addresses -> IPAll.
[
The solution is to enable TCP connections which are disabled by default.
In my case there was a configuration issue.This was the wrong configuration
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
}}
Then I have added encrypt variable to the options.It solved my issue.Corrected configuration
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
encrypt: false,
}
}