Access MySQL Localhost on Windows Host from WSL2
Last updated on July 7, 2023 am
Access MySQL Localhost on Windows Host from WSL2
It’s common to have a MySQL server running on localhost for development. But when using WSL2, it’s not that easy to access the localhost on Windows host. This post will show how to do it.
Check Private Network
Run command ip route
on WSL2 to get the IP address of the private network.
1 |
|
The first line represents the default route, the IP address 172.23.144.1
is the gateway or next-hop IP address. Actually, this is also the IP address of the host machine, where our MySQL server is running.
The second line represents a specific route for the network 172.23.144.0/20
, which is a subnet for WSL2. The source src 172.23.151.37
is the IP address that will be used when sending traffic to the network, i.e., the IP address of WSL2.
Run following command can also validate the IP address of the host machine.
1 |
|
Actually, the WSL2 and the Windows host machine are all on a shared Hyper-V switch, which is a virtual network switch.
However, the IP address of the host machine is not static, it will change after rebooting. So it’s not a good idea to have the IP address hard-coded in the configuration.
And the solution is to use the hostname of the host machine, which is also the name of the host machine. The hostname can be found by running hostname
on Windows PowerShell, or echo $(hostname)
on WSL2. The .local
suffix is also needed. See the python script for connection testing.
It is also noteworthy that the private network IPv4 addresses all begin with 172
. This would be helpful when we configure MySQL user.
Create MySQL User
Since all users on MySQL server are on localhost
by default, it is required to create a new user for WSL2.
Note that both user name user
and host name host
need to match to log in, otherwise, it will be denied. For example, if we try to log in as root in Python from WSL2, it will raise an an mysql.connector.errors.ProgrammingError
, with an error message that 1045 (28000): Access denied for user 'root'@'172.23.151.37'
.
1 |
|
Log in MySQL Command Line Client with root privilege, and create a user for WSL2, say, wsl2
.
1 |
|
or, if password is needed,
1 |
|
Here %
percentage sign is a wildcard which means any IP address that begins with 172
.
We could see at this stage the user wsl2
has no privilege.
1 |
|
Grant Privilege
Grant privilege to the user wsl2
as per requirement.
1 |
|
Rerun the query, we could see the user wsl2
has the privilege now.
1 |
|
Test Connection
Here is a simple python script. Run it on WSL2 to test the connection.
Remember to replace the user
, password
and host
with your own. The host
should be exactly the same as what echo $(hostname).local
returns on WSL2.
1 |
|
References
- ip(8) - Linux manual page
- Accessing network applications with WSL | Microsoft Learn
- networking - Connect to host machine from WSL2 - Unix & Linux Stack Exchange
- MySQL :: MySQL 8.0 Reference Manual :: 6.2.1 Account User Names and Passwords
- MySQL :: MySQL 8.0 Reference Manual :: 6.2.3 Grant Tables
- MySQL :: MySQL 8.0 Reference Manual :: 13.7.1.3 CREATE USER Statement
- How to grant all privileges to root user in MySQL 8.0 - Stack Overflow
- mysql - Not Allowed to Create User with GRANT - Ask Ubuntu
- How to Modify User Privileges in MySQL Databases :: DigitalOcean Documentation