Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED
Solution 1
The solution is to enable TCP connections which are disabled by default.
Solution 2
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.
Solution 3
If after enabling the TCP connection and your configuration is still not working. Here's my own-configuration.
var config = {
"user": 'admin',
"password": 'password',
"server": 'WINDOWS-PC',
"database": 'database_name',
"port": 61427, // make sure to change port
"dialect": "mssql",
"dialectOptions": {
"instanceName": "SQLEXPRESS"
}
};
Solution 4
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.
[
Solution 5
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:
- SQL Configuration Manager SQL Server Network Configuration
- Protocols for {Instance}
- TCP/IP - Enabled (double-click)
- IP Address (on all desired interfaces)
- TCP Dynamic Ports = BLANK! (not zero)
- TCP Port - 1433 (or desired port)
- Protocols for {Instance}
Matt Carrier
I am a pixel connoisseur who enjoys the finer details of a Bresenham line. When not staring at arrays of smoothly rendered pixels I enjoy playing guitar and consuming coffee. #SOreadytohelp
Updated on July 05, 2022Comments
-
Matt Carrier almost 2 years
I am trying to connect to MSSQL 2012 using NodeJS with the mssql connection interface.
When attempting to connect I get the following error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED] name: 'ConnectionError', message: 'Failed to conncet to localhost:1433 - connect ECONNREFUSED', code: 'ESOCKET' }
Any ideas on how to fix this?
-
Gibryon Bhojraj almost 7 years+1 for the screenshot. I thought I had it enabled because I saw remote connections were enabled in SSMS, but this was not.
-
Rajaraman Subramanian about 6 yearsIt worked for me but only when I set
1433
toTCP Port
ofIPAll
section. Setting theTCP Dynamic port
toBLANK
andTCP Port
to1433
for Ioopback address i.e 127.0.0.1 alone did not work. I am using MS SQL Server 2014 -
gh0st almost 6 yearsHow do you do this in Docker?
-
Freddy Bonda about 5 yearsThis really did help!
-
Lothre1 over 4 yearsOn windows 10 you can quickly access the configuration window by pressing "window button + R" then type: "SQLServerManager13.msc"
-
Cristi Priciu over 4 yearsThank you, this answer is what helped me.
-
tristankoffee about 4 yearsFor anyone who got here - my issue was that TCP/IP wasn't enabled, even though SSMS could connect! Enabling it in the configuration manager at the above location got it working for me.
-
Allwyn Dsouza about 3 yearsI had to restart the SQL Server service after updating this, and it worked!