MSSQL PHP on WSL setup
I found that I couldn’t connect to MSSQL on Windows from the WSL side. The ODBC drivers were working as I could connect to a remote instance. source code sample.
So I got MSSQL working on WSL2 which isn’t supported but it works.
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver16 this is SQL2022 Preview v16 which needs SSMS tools 19 preview to get the profiler working
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-microsoft-sql-server
# run mssql manually
sudo -u mssql /opt/mssql/bin/sqlservr -c -d/var/opt/mssql/data/master.mdf -l/var/opt/mssql/data/mastlog.ldf -e/var/opt/mssql/log/errorlog -x
Then connect from Windows via the IP
172.24.49.17
which I found doing ifconfig
from wsl side.
Then PHP side:
$serverName = "127.0.0.1";
database = "OSR4Rights" ;
username = "sa";
password = "secret";
connectionOptions = array(
"database" => $database,
"uid" => $username,
"pwd" => $password,
"TrustServerCertificate" => true
);
source code contains gotchas including getting the cursor correct to get the correct rowcount.