WARNING: These instructions override many secure defaults for your MySQL setup by assuming that your WSL instance and your dev machine isn't ever exposed to an external network. Don't use these instructoins in production instances or with databases with sensitive data.
In order to be able to use MySQL running in WSL from Windows you need to perform these steps:
- Rename
root
host - Change
root
authentication mechanism - Allow remote access to MySQL
- Create port forwarding between Windows and WSL
- Create MySQL connection
The root
user in MySQL has a host of localhost
by default. This makes it impossible to connect to it from any other host. So,
run this command in MySQL to allow connections from any host:
RENAME USER 'root'@'localhost' TO 'root'@'%';
('%
' means a wildcard here and allows connections from any host. Make sure that you don't expose your WSL instance outside world)
By default root on MySQL doesn't have any password. This prevents it from being accessed remotely. You need to set a root password to it and change the auth mechanism by using this command in MySQL shell:
ALTER USER 'root'@'%' IDENTIFIED WITH caching_sha2_password BY '<NEW_ROOT_PASSWORD>';
This will change the auth mechanism, and change the root password at the same time. Remember the root password. And try connecting to your MySQL instance with the new password now. You shouldn't be able to login as root without this password anymore.
MySQL by default listens on 127.0.0.1. Change this by editing /etc/mysql/mysql.conf.d/mysqld.conf
and commenting out this line:
bind-address = 127.0.0.1
by prefixing it with "#
", so it looks like this:
#bind-address = 127.0.0.1
This is quite straightforward. First learn your WSL IP address with ip addr show
. It's the IP address assigned for eth0
interface.
Run this command on an Administrator shell:
netsh interface portproxy add v4tov4 listenport=3306 listenaddress=0.0.0.0 connectport=3306 connectaddress=<YOUR_WSL_IP_ADDRESS>
Test the connectivity to WSL:
Test-NetConnection 127.0.0.1 -p 3306
The output should show TcpTestSucceeded
as True
:
ComputerName : 127.0.0.1
RemoteAddress : 127.0.0.1
RemotePort : 3306
InterfaceAlias : Loopback Pseudo-Interface 1
SourceAddress : 127.0.0.1
TcpTestSucceeded : True
Open MySQL workbench. Add a new connection. Set the user to root, and host to 127.0.0.1. Set the password to your <NEW_ROOT_PASSWORD>
.
Connect, and enjoy!
Sedat Kapanoglu - June 2025