Windows Services For Linux (WSL) is the nifty tool from Microsoft to run what is a pretty good Linux environment on Windows. There used to exist a tool with a similar name long ago on Windows, but this is something else.
WSL has been a godsend for people who prefer using a Windows system, but also want access to a proper Linux userland for development. The first version of it, now called WSL1, is what I have been using for a while and it has been really good for me.
In 2019, Microsoft announced WSL2, which is the next version of WSL. It promises far better filesystem performance (provided you use the Linux filesystem) and a significantly less resource utilization on the host machine.
The major trade-offs are:
- Slower access to the host filesystem. WSL > Windows filesystem access is painfully slow; in fact, it is much slower than WSL1.
- Networking works very differently in WSL2, compared to WSL1.
The second point is the cause for a lot of headaches for people like me who split the stack between the host and the Linux instance on WSL1. I used to run MySQL server on Windows and the rest of the PHP stack (Laravel, mostly) in the Linux instance.
This used to work wonderfully well both the host and the Linux instance used to share the same IP address. The output of ifconfig would be the same as the one you would get using ipconfig on Windows.
This changed in WSL2 and now each WSL2 instance gets a different IP address of its own and localhost is no longer the host machine.
To make matters more complicated, the IP address the Linux instance would get changes each time you run it. Which means that, to allow connections in MySQL becomes all the more difficult.
I have managed to make this work by allowing connections on the host 172.%.%.% for the user I want to provide access to in MySQL. WSL2 seems to allocate IP addresses for the host machine and the Linux instance in the 172.22.x.x space. The range that I’m allowing access for seems to be a touch too broad, but I have not seen any documentation regarding the specific range that is used by WSL2 and I’ll change it if I get more information about it.
Another issue is that you cannot ping the host machine from the Linux instance. This comment on Github fixes that.
This still leaves the tricky issue of how do you make sure that you don’t have to deal with the changing IP address of the host to connect to? It would appear that the hostname of your Windows machine (‘rey’, in my case) resolves as a sub-domain with the ‘mshome.net’ domain. If I ping rey.mshome.net from either Powershell or the Linux shell, it will always pick up the IP address of the WSL2 ethernet adapter (Ethernet adapter vEthernet (WSL)).
This seems to be working well for me for now. I will update the post if I find out more.