DEV Community

Kenneth Isaac
Kenneth Isaac

Posted on

How to Connect WSL2 (with Laravel App) to a XAMPP MySql Server on Windows

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

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

You also have a Laravel app and you are having problem running:

$ php artisan migrate
Enter fullscreen mode Exit fullscreen mode

and you are getting such error as:

$ 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 from a remote system (and WSL2 is remote system), that you'll need to create a user with access from Wsl2 and allow connections from Wsl2. You can get more detailed info from this reddit post.

Next you have to check if you have a Sql client on your Wsl2 machine. This client is different from a Sql Server. The client is what you would use to connect to the Sql server on windows.

Try any of the command below. The second command worked for me and I'm 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've also installed a Sql driver if you are running on a Laravel app. Run:
$ sudo apt-get install php7.4-mysql
_

Next, go into PhpMyAdmin, locate your created table and click on it. Navigate to Privileges among the menu at the top of the screen. Find all username with root and click on Edit privileges. Tick on Global privileges and click Go.

Now, there is an alternative method to do this, using Windows Cmd or Shell from XAMPP and connecting to the MySql database directly and type in some SQL code directly.

$ mysql -u root -p -h 127.0.0.1

then type this command in:

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

As I've written above, what the command does is, create a user with access from Wsl2 and allow connections from Wsl2 or any remote system and as you might tell, could constitute a security risk since you are granting all privileges.

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

Skip the password prompt by tapping enter.

Congratulations, you've connected to your MySql database on your Windows host from Wsl2.

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

and copy or note the result.

Next, edit the MySql section of your .env file to the following:

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

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

Watch as your migration loads and smile.

You've successfully connected your Wsl2 box to a service running on your Windows host. Now if you don't mind, go reward yourself a bit. Cheers.

Apparently, connecting to a Wsl2 service from windows is much easier and comes set up, I guess because of Port Forwarding.

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

Top comments (0)