I had setup a WSL2 Ubuntu. Now I am running a local SQL Server instance on the 1401 port using Docker.
Container port:
0.0.0.0:1401->1433/tcp
I would like to connect this instance from SSMS but I am getting following error:
Server name: localhost, 1401
Error:
Cannot connect to localhost,1401.
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: TCP Provider, error: 0 – The wait operation timed out.)
(Microsoft SQL Server, Error: 258)
[Solution]
I am able to connect it via the WSL2 IP. I run "hostname -I" command in WSL2 and use the same IP in SSMS. And, I am able to make a connection
3
Answers
First question — Is there a VPN running/connected in Windows? If so, ignore the rest of this and suspect that first. Make sure the VPN is not running, stop Docker, issue a
wsl --shutdown
, restart and try again.Assuming that’s not the problem …
Normally, WSL2 provides a feature known as "localhost forwarding" which allows services/apps on Windows to communicate with the virtualized WSL2 IP using
localhost
. It essentially takes any localhost traffic that isn’t directed to a port bound under Windows and forwards it to the Hyper-V virtual network for WSL2.All WSL2 instances (including the Docker instance) share the same WSL2 network interface as they are all running in the same virtual machine/kernel.
So you seem to be doing the right thing in attempting to connect to
localhost
from SSMS.But … sometimes that localhost forwarding breaks. There are two common (related) scenarios that can cause this (and perhaps others):
First check to make sure you can access 1401 from within WSL2:
^^^ assumes netcat is installed, which it is by default in the WSL2 Ubuntu distribution. For other distributions, install it or check connectivity via other methods.
If that doesn’t succeed, then I’d suspect some configuration issue in SQL Server.
If that does succeed, then run the same test from the Windows host in PowerShell:
If that doesn’t succeed, then I’d suspect a localhost forwarding issue.
Side note: I’m assuming you are running Docker Desktop, but if you are just running Docker Engine in a WSL2 instance, that’s no problem. Just ignore the Docker Desktop instructions below.
/etc/wsl.conf
in any of your running WSL2 instances that mention disablinglocalhostForwarding
. I’m assuming no, since that is not the default. However, if you happen to, make sure you set these totrue
.If localhost doesn’t work, try use [::1] in the server name. In WSL2, port 1433 is using IP/TCPv6, SSMS some times is not able to resolve localhost to loopback IP [::1].
Source: https://jayfuconsulting.wordpress.com/2020/11/14/sql-server-2019-docker-wsl-2/
One last thing which you could try is to modify the windows host file. I almost tried all the steps mentioned over different link, but all goes in vain. Then I opened the host file which could be accessed using
C:WindowsSystem32DriversEtc
Open the host file and uncomment(remove # sign) from the localhost name resolution section