0

I started to develop a .NET Core Web API application with SQL Server. I use the Windows Subsystem for Linux (I store the files in the Ubuntu server).

Unfortunately I can't connect to SQL Server which is installed on Windows 10.

I enabled TCP/IP in SQL Server configuration but I don't know which is the correct connection string.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • We can't see your screen and don't know your network setup. Please edit your question to include the connection string (anonymize the server address, username and password as required) as well as the full and complete error message you're getting - as text. – AlwaysLearning Feb 09 '21 at 21:47
  • Likely a duplicate of [this question](https://stackoverflow.com/q/65625762/11810933), but as was mentioned, it's hard to know for sure without more detail in your question. If the linked question/answer help you, then please remember to upvote both, and close out your question here. Thanks. – NotTheDr01ds Feb 10 '21 at 21:01
  • Please have a look over here: https://github.com/microsoft/WSL/issues/4619 – wolszakp Apr 19 '22 at 13:41

1 Answers1

4

There is long issue that windows services are not accessible from WSL - github
Making long story short, you need to use IP address of your windows machine as server.

You can take it multiple ways e.g. calling this command from Windows commandline

ipconfig /all 

and taking your IP address.

You need to enable 1433 port to be accessible from WSL in Windows Defender:

netsh advfirewall firewall add rule name=WSL_SQL dir=in protocol=tcp action=allow localport=1433 remoteip=localsubnet profile=any

Over here there is an issue. Opening concrete port works for me.

From your WSL terminal you can verify that you can access sql using e.g. telnet.

telnet <windows_host_ip> 1433

It should return something like this:

Trying <windows_host_ip>...
Connected to <windows_host_ip>.
Escape character is '^]'.

If it doesn't work please also verify that you have TCP service enabled in you SQL Configuration Manager
-> here you can find guidance for it -> link

wolszakp
  • 1,109
  • 11
  • 25
  • This can be simplified by using the mDNS name `$(hostname).local` instead of IP address. Under most recent (3-4 years, at least) Windows releases, mDNS will return the correct (Hyper-V switch) address that can be used to access Windows services via WSL. See [my answer on this question](https://stackoverflow.com/a/69407064/11810933) for more details. – NotTheDr01ds Apr 19 '22 at 14:46
  • Thanks for sharing, but it doesn't work for me. -> nc -zv "$(hostname).local" 1433 nc: getaddrinfo for host "R90VXSZR.local" port 1433: Name or service not known and -> nc -zv 1433 Connection to 192.168.1.13 1433 port [tcp/ms-sql-s] succeeded! Any clues? – wolszakp Apr 20 '22 at 09:57
  • What's your `uname -a` show? – NotTheDr01ds Apr 20 '22 at 12:18
  • Linux R90VXSZR 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux – wolszakp Apr 20 '22 at 13:20
  • Very odd. You're the second person who has reported it hasn't worked, so I'd love to know what the difference is. Just to confirm, R90VXSZR is also the Windows "Computer Name", right? – NotTheDr01ds Apr 20 '22 at 14:11
  • Yes - this is my Windows host name. I am using Ubuntu 20.04. – wolszakp Apr 20 '22 at 14:16
  • @NotTheDr01ds any clues on that? – wolszakp Apr 28 '22 at 10:00
  • Well, I re-read your post, and I'm not sure mDNS would be any "easier" in this case anyway. But I'm still wondering also why it isn't working for you. Is there a chance you've turned off the WSL `/etc/resolv.conf` auto-generation? That might do it, since it's the Windows resolver that would handle local mDNS. – NotTheDr01ds Apr 28 '22 at 13:49
  • I am modifying `/etc/resolv.conf` while it doesn't resolve external addresses. It looks like this way: # generateResolvConf = false nameserver 172.28.160.1 nameserver 8.8.8.8 – wolszakp Apr 28 '22 at 14:26
  • That might be it - Where is `172.28.160.1` coming from? The Hyper-V switch (also the resolver) address changes each time WSL restarts. Is that address the same that is reported if you run `ip route show default | awk '{print $3}'`? – NotTheDr01ds Apr 28 '22 at 19:26
  • It is automatically generated via `wsl` - it's the same as result of `ip route...`. During start I am adding `nameserver 8.8.8.8`. – wolszakp May 04 '22 at 07:27
  • Ok, well that should still work, I would think. Still noodling, then ... – NotTheDr01ds May 04 '22 at 11:40
  • Will be thankful if you will bring some light to it :) Thanks @NotTheDr01ds – wolszakp May 05 '22 at 12:21
  • Hmm. I did have another thought - does mDNS (`ping R90VXSZR.local`) work in PowerShell? – NotTheDr01ds May 05 '22 at 22:09
  • Yup from PowerShell it works: ping -4 R90VXSZR.local gives output Pinging R90VXSZR. [172.24.0.1] with 32 bytes of data: Reply from 172.24.0.1: bytes=32 time<1ms TTL=128 Any thought? – wolszakp Apr 20 '23 at 07:35