How to connect WSL2 (with Laravel app) to XAMPP MySql Server on Windows?

You probably have a MySQL server running with XAMPP on Windows. You also have Wsl2 and you want to access that database from Wsl2 for your application. What do you do?

I assume that you have XAMPP already running and Apache and MySql started and you have also created a table that you want to access. Good.

You also have a Laravel app and you’re having trouble running:

$ php artisan migrate
enter fullscreen mode

exit fullscreen mode

And you are getting error like this:

$ Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel_survey and table_name = migrations and table_type="BASE TABLE")
enter fullscreen mode

exit fullscreen mode

This means that, when you connect to a remote system (and WSL2 is the remote system), you will need to create a user with access from Wsl2 and allow connections to Wsl2. You can get more detailed information from this reddit post.

After that you need to check whether you have Sql client on your Wsl2 machine. This client is different from Sql Server. The client is what you would use to connect to SQL Server on Windows.

Try any of the commands below. The second command worked for me and I am on an Ubuntu box. Source

$ sudo apt install mysql-client-core-8.0     # version 8.0.27-0ubuntu0.20.04.1, or
$ sudo apt install mariadb-client-core-10.3  # version 1:10.3.31-0ubuntu0.20.04.1
enter fullscreen mode

exit fullscreen mode

_Make sure you have also installed a Sql driver if you are running on Laravel app. run:
$ sudo apt-get install php7.4-mysql

Next, go into PhpMyAdmin, locate your created table and click on it. go to privilege in the middle of the menu at the top of the screen. find all usernames with Root and click edit privileges, Tick global privileges And click on Go.

Now, an alternative way to do this is to use Windows Cmd or Shell from XAMPP and connect directly to the MySql database and type some SQL code directly.

$ mysql -u root -p -h

Then type this command in:


What the command does is, as I wrote above, create a user with access from Wsl2 and allow connections to Wsl2 or any remote system and, as you can tell, may constitute a security risk as you may be taking all privileges are providing.

Next, on Wsl2, run:
$ mysql -u root -p -h "$(hostname).local"

Leave the password prompt by tapping Enter.

Congratulations, you have connected to your MySQL database on your Windows host from Wsl2.

Now, to be able to run migration for Laravel app, you need to go into .env File and edit the Sql section, but before that, run:
$ echo $(hostname).local

And copy or note down the result.

Next, edit your MySql section .env File for the following:

DB_HOST=DESKTOP-BB111.local #this should be the result of the last command above and should end with .local
DB_DATABASE=laravel_counter #this is the name of the database you created and as seen on your PhpMyAdmin
DB_PASSWORD= #leave this empty
enter fullscreen mode

exit fullscreen mode

Save that file and go back to your terminal and run:
php artisan migrate

See that your migration is loading and smile.

You have successfully connected your Wsl2 box to a service running on your Windows host. Now if you don’t mind, go give yourself a little reward. encourage.

Apparently, connecting to the Wsl2 service from Windows is much easier and comes set up, I think because of port forwarding.

I hope this post has helped you in some way. Thanks for reading.

Leave a Comment