How to Change MySQL Port in WSL (Default 3306 → Custom Port)

Hello in this writing I am going to show you how to create a MySql on Windows Subsystem for Linux and to change default port (3306) for MySql server in Windows Subsystem for Linux.

Let’s begin by deleting MySql installation (if there is any).

sudo systemctl stop mysql: Stops the MySql service.

sudo apt purge mysql* -y: Remove all packages starting with mysql (e.g., mysql-server, mysql-client) and their configuration files.

sudo rm -rf /etc/mysql /var/lib/mysql: Force-delete MySQL’s configuration directory (/etc/mysql) and data directory (/var/lib/mysql).

sudo apt autoremove -y: Remove unused dependencies (packages installed automatically but no longer needed).

dpkg -l | grep mysql: dpkg -l | grep List all installed packages containing mysql in their names.

We removed all the mysql files and configurations i got errors because I haven’t got mysql files if you do have you won’t get error messages. In the last command I am checking whether it remains mysql files.

 

sudo apt update: Updates the system.

sudo apt install mysql-server -y: Installs the MySql.

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf: This command opens the MySQL server configuration file (mysqld.cnf) for editing using the nano text editor with superuser (sudo) privileges. We are going to make changes in this file.

When you open the file you will see commented line:

#port         = 3306

uncomment it and change it to

port      =4444

sudo systemctl restart mysql: It restarts the mysql server.

NOTE: After this step you can create a password for root user I will skip that part since I am making that server to show.

sudo mysql -u root: This command wil help you to connect to mysql server.

SHOW VARIABLES LIKE ‘PORT’; : This is a mysql command it makes us to see port number. We can see port number has changed.