Connect Windows (XAMPP) MySQL from WSL2 Ubuntu

Table of contents

No heading

No headings in the article.

Install MySQL client on WSL Ubuntu.

sudo apt install mysql-client-core-8.0

Back yo Windows, run:

mysql -u root -p

Enter this command to enable root account for any host:

> CREATE USER 'root'@'%' IDENTIFIED BY 'root'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
> FLUSH PRIVILEGES;
> exit;

Next, on Wsl2, run:

mysql -u root -p -h "$(hostname).local"

This should works.

If still shows:

ERROR 2003 (HY000): Can't connect to MySQL server on xxx

Try install libnss-mdns

sudo apt install libnss-mdns

Now you may want to enter the hostname to .env file, just type:

echo $(hostname).local

And copy the result to your .env file.