Skip to content
ohsudo, Home
Databases

SQL Server "A network-related or instance-specific error" (error 40, error 26): how to fix it

Fix SQL Server "A network-related or instance-specific error occurred": check the service, TCP/IP, the instance name, SQL Browser, port 1433 and the Windows firewall.

Intermediate4 min readApplies to SQL Server 2017+ · SQL Server Express · Windows Server · SQL Server on Linux · SSMS · sqlcmd

by OhSudo ··

Not sure this is your cause?Find it step by step: Can't connect to the database →

A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

The client didn’t reach SQL Server at all, so your login and password aren’t the problem: they were never checked. Something between the client and the server, or the server’s own network configuration, stopped the connection.

The end of the message gives the first clue:

Ends with Points to
Named Pipes Provider, error: 40 The server can’t be reached. Generic: start at section 1
TCP Provider, error: 0 The TCP connection failed or was cut: service, port or firewall
SQL Network Interfaces, error: 26 The named instance wasn’t found: instance name or SQL Browser
  1. Clientserver\instance
  2. Name, port, firewallSQL Browser for named instances, TCP 1433
  3. SQL Serverstopped, or TCP/IP off(breaks here)
The client never reached SQL Server, so the login was never checked. Something on the way, or the server's own network setup, stopped the connection.

TL;DR

Terminal window
Get-Service *SQL* # is the service running?
Test-NetConnection -ComputerName db01 -Port 1433 # can the client reach the port?
sqlcmd -S tcp:db01,1433 -U app -Q "SELECT @@VERSION" # try with TCP and an explicit port

Express is the classic case: TCP/IP is off by default, and the instance is named SQLEXPRESS.

1. Is the service running?

On the server:

Terminal window
Get-Service *SQL*
Status Name DisplayName
------ ---- -----------
Stopped MSSQLSERVER SQL Server (MSSQLSERVER)
Stopped SQLBrowser SQL Server Browser

MSSQLSERVER is the default instance; a named one is MSSQL$SQLEXPRESS. If it’s stopped, start it:

Terminal window
Start-Service MSSQLSERVER

If it won’t start, read the error log, usually C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Log\ERRORLOG (the folder name depends on the version and instance), and the Windows Application log.

2. Is the instance name right?

Instance Connect to
Default db01, or db01,1433
Named db01\SQLEXPRESS
On your own machine localhost, .\SQLEXPRESS or (localdb)\MSSQLLocalDB

A default installation of SQL Server Express is a named instance. So db01 fails with error 26 or 40, and db01\SQLEXPRESS is what you want. To see what’s installed:

Terminal window
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL'

3. Is TCP/IP enabled?

It’s off by default in Express and Developer editions, which only allow local connections out of the box.

Open SQL Server Configuration Manager → SQL Server Network Configuration → Protocols for [instance], enable TCP/IP, and restart the service. Under the IP addresses tab, the IPAll section shows the port: TCP Port for a fixed one, TCP Dynamic Ports for one chosen at startup.

4. Which port does it listen on?

A default instance uses 1433. Named instances use a dynamic port unless you set one. Ask the server:

Terminal window
Get-NetTCPConnection -State Listen -OwningProcess (Get-Process sqlservr).Id | Select-Object LocalAddress, LocalPort

SQL Server also writes it to its error log at startup, in a line starting with Server is listening on. Over a TCP connection you can ask it too (a local shared memory connection shows NULL here):

SELECT local_net_address, local_tcp_port FROM sys.dm_exec_connections WHERE session_id = @@SPID;

A named instance needs one of these two things:

  • The SQL Server Browser service running, and UDP port 1434 open. It tells clients which port the instance uses. This is what error 26 usually points to.
  • Or a fixed port in Configuration Manager, and the port in the connection string: db01,1500. With a port, the Browser isn’t needed. This is the better choice on servers.

5. The firewall

Test from the client:

Terminal window
Test-NetConnection -ComputerName db01 -Port 1433

TcpTestSucceeded : False while the service is running means a firewall: Windows Firewall on the server, or a cloud security group. On the server, allow only the networks that need it:

Terminal window
New-NetFirewallRule -DisplayName 'SQL Server' -Direction Inbound -Protocol TCP -LocalPort 1433 -RemoteAddress 10.0.0.0/24 -Action Allow

For a named instance with the Browser, also open UDP 1434. Timeouts and refusals tell different stories: see “Connection timed out” vs “Connection refused”.

Warning

Don’t open 1433 to the whole internet: exposed SQL Servers get password attempts constantly, as the Port Lookup notes. Allow specific addresses, or go through a VPN.

6. Remote connections and DNS

  • Allow remote connections must be on: in SSMS, server Properties → Connections. It’s on by default.
  • The name must resolve: try the IP address. If the IP works and the name doesn’t, it’s DNS: see Temporary failure in name resolution.

7. On Linux and in Docker

There’s no Configuration Manager on Linux, and TCP is on by default. Check the service and the port, and connect with an explicit port:

Terminal window
sudo systemctl status mssql-server
sudo ss -tlnp | grep 1433
sqlcmd -S 127.0.0.1,1433 -U sa

In Docker, publish the port (-p 1433:1433), and connect to the service name between containers. If the container exits right after it starts, read docker logs: an MSSQL_SA_PASSWORD that’s too weak is the usual reason.

Shell, we have a problem!

Similar, but different: Login failed for user 'app' (error 18456) means the network is fine, and you reached SQL Server: it’s about the login. And newer client drivers fail with The certificate chain was issued by an authority that is not trusted, because encryption is now on by default: that’s about the server’s certificate, not the network. If you reach the server and it refuses the login, see error 18456.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles