Last active
May 1, 2022 05:11
-
-
Save k3karthic/33419aedb4ba0f70c64b250b28f1be00 to your computer and use it in GitHub Desktop.
Allow WSL2 to connect to SQL Server on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# This script proxies connections from localhost:1433 to | |
# the dynamic WSL2 host IP | |
# | |
# Requirement: | |
# Run sqlServerListenOnWSL2IP.ps1 to allow WSL2 to connect to SQL Server | |
# | |
uniqname="/tmp/sqlproxy-$(date +%s)" | |
# From Philipp Scheit (https://pscheit.medium.com/get-the-ip-address-of-the-desktop-windows-host-in-wsl2-7dc61653ad51) | |
wslhostip=$(ipconfig.exe | grep 'vEthernet (WSL)' -A4 | cut -d":" -f 2 | tail -n1 | sed -e 's/\s*//g') | |
echo "Windows host is at: $wslhostip..." | |
echo " | |
load_module /usr/lib/nginx/modules/ngx_stream_module.so; | |
daemon off; | |
events { } | |
stream { | |
server { | |
listen 1433; | |
proxy_pass $wslhostip:1433; | |
} | |
} | |
" > $uniqname | |
function cleanup() { | |
rm $uniqname | |
exit | |
} | |
trap cleanup INT | |
sudo nginx -c $uniqname |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This script configures SQL Server on Windows to listen for connections | |
# from the network adapter connected to WSL2 | |
# | |
# Assumptions: | |
# 1) There is only a single SQL Server instance | |
# 2) SQL Server should only listen for local and WSL2 connections | |
# 3) The network adapter connected to WSL2 has the phrase "(WSL)" in the name | |
# | |
# Requirement: | |
# 1) Run the script as admin | |
# 2) Configure firewall to allow WSL2 to connect to Windows | |
# | |
## | |
# Imports | |
## | |
Import-Module sqlps | |
## | |
# Variables | |
## | |
# Fetch the ip address of the network adapter connected to WSL2 | |
$wslNetIp = Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -like "*(WSL)*"} | Select -First 1 | |
$smo = 'Microsoft.SqlServer.Management.Smo.' | |
$wmi = new-object ($smo + 'Wmi.ManagedComputer'). | |
## | |
# Main | |
## | |
if ($wmi.ServerInstances.Count -eq 0) { | |
Write-Output "No SQL Server instances found" | |
exit | |
} | |
# Assumption: local machine only has a single SQL Server instance | |
$instance = $wmi.ServerInstances[0] | |
$tcp = $instance.ServerProtocols['Tcp'] | |
# Enable Tcp/Ip protocol | |
if ($tcp.IsEnabled -ne $true) { | |
$tcp.IsEnabled = $true | |
$tcp.Alter() | |
} | |
$ipAddresses = $tcp.IPAddresses | |
Foreach ($ip in $ipAddresses) { | |
$addr = $ip.IPAddress.IPAddressToString | |
$props = $ip.IPAddressProperties | |
if ( | |
$addr -eq "127.0.0.1" -or | |
$addr -eq "::1" | |
) { | |
# Listen on localhost | |
if ($props['Enabled'].Value -ne $true) { | |
$props['Enabled'].Value = $true | |
$tcp.Alter() | |
} | |
} else { | |
# Disable on other IPs | |
if ($props['Enabled'].Value -eq $true) { | |
$props['Enabled'].Value = $false | |
$tcp.Alter() | |
} | |
} | |
} | |
$wslIP = $ipAddresses | Where-Object {$_.IPAddress.IPAddressToString -eq $wslNetIp.IPAddress} | |
if ($wslIP -eq $null) { | |
# Did not find an existing IP configuration, using the first non-local IP config | |
$wslIP = $ipAddresses | | |
Where-Object { | |
$_.IPAddress.IPAddressToString -ne "127.0.0.1" -and | |
$_.IPAddress.IPAddressToString -ne "::1" | |
} | | |
Select -First 1 | |
$wslIP.IPAddressProperties['IpAddress'].Value = $wslNetIp.IPAddress | |
$tcp.Alter() | |
} | |
# Listen for connections from WSL2 | |
$wslIPStat = $wslIP.IPAddressProperties["Enabled"] | |
if ($wslIPStat.Value -ne $true) { | |
$wslIPStat.Value = $true | |
$tcp.Alter() | |
} | |
Write-Output "Restarting SQL Server..." | |
Restart-Service -Force MSSQLSERVER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment