When installing MySQL on WSL2, you might encounter isues where the service fails to start, reporting an error code.
Initial attempts might involve using apt-get to install or upgrade the MySQL server package, for example:
sudo apt-get install ./mysql-server_5.7.32-1ubuntu18.04_amd64.deb
Upon failure, the logs may indicate a port conflict. However, standard Linux tools like netstat, ss, or lsof might not show any process actively using the default MySQL port (3306) within the WSL2 environment. This can be misleading, suggestting a dependency issue.
The root cause is often that WSL2, by default, uses a host network mirroring mode. This means WSL2 shares the host's IP adress and port space. Consequently, the default MySQL port might already be in use by a process on your Windows host, even though netstat or lsof inside WSL2 won't detect this.
To resolve this, you need to configure MySQL to use a different port. Edit the MySQL configuration file, typically located at /etc/mysql/my.cnf or a similar path within your WSL2 distribution:
# /etc/mysql/my.cnf
[mysqld]
port=3307
After changing the port directive to an unused port (e.g., 3307), restart the MySQL service. This change circumvents the conflict with the host's port usage, allowing the MySQL service to start successfully within WSL2.