MySQL on Windows 11 using WSL

Lenin Elio
3 min readJan 17, 2023

--

I’ve been using XAMPP for several years to run MySQL on Windows (7, 8, 8.1, 10, 11), a recurring problem in each version is that I get errors with port 3306, forcing me to uninstall some programs that use the same port and making me afraid to install new applications because of the same problem.

Recently I was looking for ways to run MySQL on Windows without the need for XAMPP, LAMP, etc. Among the group of results I found WSL (Windows Subsystem for Linux allows developers to run a GNU/Linux environment). As far as I understand WSL works as or is a “virtual machine” where we can install applications and “connect” to our work environment in Windows. I do not want to delve into this area (WSL) and I am not an expert on the subject and I just share with you what I could understand and the results I got.

By reviewing a few lines of code in some pages I was able to install MySQL inside WSL, at the moment I am running MySQL from Ubuntu 20.04 (WSL) and connect it with my applications that I develop in Windows.

Here is a compilation of the commands that I ran for the installation and the subsequent execution of the program:

WSL Installation:

Open the Windows command prompt (CMD) as administrator and execute the following command:

<# This command will install the tools needed to run WSL. #>
wsl --install

If you need a custom installation, see the documentation here.

During the installation process, you will be prompted for a user name and password; enter the information and avoid using spaces or symbols in the user name.

Once the process is completed, restart the system.

When the reboot is complete, start CMD as an administrator and run the following command:

Install and configure MySQL:

<# Start WSL #>
wsl
<# (optional) Elevating privileges #>
sudo su

<# Enter the password #>
<# Update system packages #>
sudo apt update
<# Install MySQL #>
sudo apt install mysql-server
<# start MySQL service #>
sudo service mysql start
<# Access MySQL for the first time #>
sudo mysql
/* Execute the following SQL command */
SELECT user, authentication_string, plugin, host FROM mysql.user;
/* in BY 'password'; the word [password] will be the password of the MySQL root user*/
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
sudo mysql -uroot –p exit;

Close CMD and restart the system.

Starting WSL and MySQL:

After the system reboots, open CMD and run the following command:

<# Starts WSL #>
wsl
<# Start MySQL #>
sudo service mysql start
<# Access MySQL #>
mysql --host=localhost --port=3306 --user=root --password

<# Enter the password of the root user you previously configured #>

You can now access MySQL from the command line:

Or from a graphical interface (in my case I use the Navicat tool).

If you have any questions or suggestions, don’t forget to leave a comment, thanks :).

--

--