Skip to content

Instantly share code, notes, and snippets.

@ssg
Last active June 7, 2025 01:25
Show Gist options
  • Save ssg/5f54c685df5f3bf3799a25667e6d2a2b to your computer and use it in GitHub Desktop.
Save ssg/5f54c685df5f3bf3799a25667e6d2a2b to your computer and use it in GitHub Desktop.
Use MySQL with WSL from Windows

Use MySQL with WSL from Windows

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:

  1. Rename root host
  2. Change root authentication mechanism
  3. Allow remote access to MySQL
  4. Create port forwarding between Windows and WSL
  5. Create MySQL connection

Rename root host

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)

Change root authentication mechanism

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.

Allow remote access to MySQL

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

Create port forwarding between Windows and WSL

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

Create MySQL connetion

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment