-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to connect to SQL Server using "localhost" as the server name #2075
Comments
@omfgicbf Thanks for bringing this up. we will look into it and update. |
@omfgicbf. We investigated the issue but we are unable to reproduce it. However, the following are my suggestions that you may want to try:
|
Hi, @arellegue, thanks for following up. Per your suggestions:
Either way, per the Prior to, and during all the above steps, Windows was able to resolve
I used Process Monitor and TCPView to see what it was doing. When I confirmed this with Wireshark. In Wireshark, I also noticed that connecting to other servers (e.g. Azure SQL) via hostname will cause an NBNS name query for the host part of the address, however, attempting to connect to Browsing to To reiterate, this does not happen with any other application (that I've tried so far, and there have been several now). |
@omfgicbf. Did you try to ping localhost using ipv4? i.e. ping localhost -4. However, this issue is not related to MDS driver. This issue is mostly related to your network dns not resolving localhost. Our suggestion is for you to contact your Network Administrator to try to fix the localhost resolution in your dns. |
@omfgicbf. Could you provide the docker command used to run your mssql-server container. |
Hi @arellegue, Resolution is working:
DNS is not used to resolve Here is the
I've included the
To reiterate, WSL is not seeing any network activity when SSMS is attempting to connect. SSMS is attempting to connect to all of the Windows interface IP addresses except the loopback address (which SQL Server is listening on). Even if Docker or the SQL Docker container were not running or misconfigured, SSMS is not even attempting to connect to the loopback address. Manually specifying the loopback address (i.e. To further isolate Docker, the SQL Docker container or WSL from being somehow related to the issue, I installed SQL Server 2022 in Windows and configured it to listen only on the loopback addresses (the default is to listen on ALL interfaces). I stopped the SQL Docker container, Docker and WSL and confirmed that the Windows SQL Server instance was listening on the loopback addresses (and only on those). I get the same result... I cannot connect using However, if I configure SQL Server to listen on at least one other interface IP address (e.g. an uplink on 192.168.1.x) and try to connect to It appears that SSMS is somehow treating I can however rule out SSMS, as I get the same results using a .NET application. |
@omfgicbf. Just to confirm, is the password in docker run command really "password" or just a place holder? If it's in fact the actual password for sa user then the mssql-server container may not be actually running due to a valid password format restriction for mssql-server. |
Hi @arellegue, No, the real password is not "password". If it was a problem with the Could this be the problem?
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs Line 732 in 5001097
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs Lines 759 to 760 in 5001097
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs Line 203 in 5001097
This can be confirmed with the following code:
See dotnet/runtime#27534 (comment) Hi @Kaur-Parminder, could you please review and relabel if appropriate? |
For what it's worth,
|
When specifying the server, to narrow it down, try |
Hi @David-Engel, Trying with I tried with I am probably way off, but I did try following it manually and as far as I got was the native call into If it matters, we can probably exclude Linux from the equation, as I can reproduce this by installing SQL Server in Windows and disabling all but the loopback addresses. As a side note (and I acknowledge I am probably completely and absolutely wrong on this, but thought it worth mentioning in case anyway) when I was looking through the call hierarchy for Notwithstanding all of the above, I acknowledge this is an edge case and has a workaround by using the addresses rather than the hostname, so has a low priority. |
That aligns with my testing. I see code in the native SNI library that appears to decide to use ComputerName when it sees localhost. (I'm not set up to debug it.) ComputerName doesn't resolve to the localhost IPs, thus the behavior. The MS ODBC Driver for SQL Server also has the same behavior. SQL Server installed locally, listening on TCP localhost IP addresses ONLY (127.0.0.1 and ::1):
I've inquired why it might behave this way. My thought is it might have to do with cluster or mirroring scenarios. Others have suggested it's to improve the connection encryption experience with regard to the name in certificates. It does seem odd. But as you said, it's easy to work around. |
@omfgicbf I felt a huge sigh of relief when I read through every word of your posts here, it's describing my last couple days of troubleshooting. I've been trying to use Docker Engine without Docker Desktop as well as Podman, but ran into this incredibly weird behavior you are seeing with localhost only with Microsoft SQL Server images. I've resorted to trying to diagnose this issue with pktmon of all tools because I thought that there was some obscure networking issue specifically with SSMS and localhost resolution or WSL 2 itself. It turns it out it was just this issue. Not a WSL 2 localhostForwarding issue or a mirrored networking issue or a WSL 2 NAT issue or a WSL 2 IPv6 issue. Unfortunately, if you're using DataGrip which is using the Microsoft JDBC driver, you won't see this behavior and you'll be able to connect on localhost while your peers on SSMS won't. |
I was experience that exact same behavior that was driving me crazy. used commands (as elevated priviledges)
More info on port forwarding: https://woshub.com/port-forwarding-in-windows/ |
I have the same problem. The workaround I found was disable IPV6 on WSL network adapter. This way I am able to connect to mssql using localhost via SSMS. Edit: I had to disable IPV6 on both WSL and principal adapter. |
Closing this issue as it is by design |
When trying to connect to SQL Server running in a Docker container in WSL I am unable to connect using
localhost
as the server name. I can, however, connect using127.0.0.1
or::1
.The application is a .NET 7.0.7 application using Microsoft.Data.SqlClient.dll version 5.0.2 as part of Microsoft.EntityFrameworkCore.SqlServer 7.0.8.
This connection string doesn't work:
This connection string does:
As does this one:
This is the exception detail:
From WSL, I can see that it's listening on the correct ports:
From Windows I can see the same:
This is also confirmed by checking Resource Monitor, which shows
wslrelay.exe
listening on port 1433 on both "1Pv6 loopback" and "IPv4 loopback" and lists the Firewall Status as "Allowed, not restricted".In WSL (i.e. not from inside the docker container), running
sudo nc 0.0.0.0 1433 -l
andsudo nc :: 1433 -l
shows connection attempts when using127.0.0.1
or::1
as the server address (as expected), but usinglocalhost
doesn't show any network activity.Using
tcpdump
in WSL doesn't show any traffic when attempting to connect usinglocalhost
.I also experience the same issue using SSMS and
sqlcmd
.From SSMS, I can connect using
127.0.0.1
or::1
, but cannot connect usinglocalhost
.Here is the error:
By default, SSMS attempts to use the Named Pipes Provider when trying to connect with
localhost
specified as the Server name. Changing tolocalhost,1433
or setting the Network protocol to TCP/IP (from default) in the connection Options / Connection Properties tab, shows it trying to use the TCP Provider (as expected), with the same results.It appears that
localhost
is not being resolved in any SQL application.Other applications, however, are able to connect (from Windows) using
localhost
to applications running in Docker, including MySQL, Apache, etc. In these instances,localhost
is resolving to::1
, as doesping
when run from a Command Prompt.For what it's worth, this used to work on a previous machine using Windows 10 and WSL2, so I'm not sure if this is a Windows issue, or a Microsoft SQL Data Provider issue, which may have also been a different version at the time.
In summary:
127.0.0.1
or::1
works, and it appears that WSL is not receiving any traffic when a connection attempt is made tolocalhost
.localhost
.I don't know the inner workings of the Microsoft SQL Data Provider, however, it feels like the provider itself is failing to resolve
localhost
or perhaps is trying to resolve it as a Named Pipes address and caching that (failed) result even when using the TCP/IP provider.I am not running any SQL Server instances in Windows. Microsoft SQL Server 2019 LocalDB is confirmed as stopped as reported by
sqllocaldb
.Other information:
sqlcmd
is using Microsoft ODBC Driver 17.10 for SQL Server version 2017.1710.3.1SSMS:
WSL
I was unable to find any similar issues in this repo, other than #1455 which appears to have the opposite problem to mine.
There is also an issue (microsoft/mssql-docker#813) in the mssql-docker repo which is the same issue as this.
My apologies in advance if this is the wrong place for this. Let me know and I'll move it if required.
The text was updated successfully, but these errors were encountered: