简体   繁体   中英

How to connect to SQL Server on Windows 10 from WSL 2 Linux C++ application

I tried different methods. SQL Server connection failed. All didn't work for me. I have a host running Windows 10, WSL 2. My guest is Ubuntu.

I'm trying to use sqlcmd to connect to SQL Server running on my host machine, but I'm not sure what IP to use in the connection?

I've exposed/enabled basically everything from the SQL Server Configuration Manager on the host Windows SQL Server.

I'm trying to use sqlcmd to connect to SQL Server running on my host machine, but I'm not sure what IP to use in the connection?

You can get the IP address to use from /etc/resolv.conf , as documented here , eg

david@xxxx:~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.21.224.1

You access the Windows host at 172.21.224.1 . But since the Windows host is configured as your nameserver, you should be able to resolve the hostname of your Windows host, and connect using that, or to your Windows host's "real" IP address.

And since you aren't accessing SQL Server via a loopback address you also have to add a Windows firewall rule.

But it's probably less common to need to do this directly than to use Docker Desktop, and rely on Docker's networking features .

Refering to the official documentation ,

If you want to obtain the IP Address of the Windows Machine, run the following steps:

  1. You need to check the contents of the resolv.conf : cat /etc/resolv.conf
  2. Look at the nameserver . It contains the required IP Address.

运行命令示例

I do recommend referring to this page for a brief overview on Resolving Name Server.

Basically, nameserver contains the IP Address of a name resolver. This name resolver helps in associating an IP Address with a domain name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM