Saturday, October 22, 2022
HomeWordPress DevelopmentThe right way to Join WSL2 (with Laravel App) to a XAMPP...

The right way to Join WSL2 (with Laravel App) to a XAMPP MySql Server on Home windows


You may have a MySql server operating on Home windows maybe with XAMPP. You even have Wsl2 and need to entry that database from Wsl2 to your to your utility. What do you do?

I assume you’ve got XAMPP operating already and have Apache and MySql began and you have additionally created a desk you need to entry. Good.

You even have a Laravel app and you’re having drawback operating:

$ php artisan migrate
Enter fullscreen mode

Exit fullscreen mode

and you’re getting such error as:

$ IlluminateDatabaseQueryException

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

Exit fullscreen mode

Which means that, once you join from a distant system (and WSL2 is distant system), that you will must create a consumer with entry from Wsl2 and permit connections from Wsl2. You will get extra detailed data from this reddit put up.

Subsequent you need to examine in case you have a Sql consumer in your Wsl2 machine. This consumer is completely different from a Sql Server. The consumer is what you’ll use to connect with the Sql server on home windows.

Strive any of the command beneath. The second command labored for me and I am on an Ubuntu field. supply

$ sudo apt set up mysql-client-core-8.0     # model 8.0.27-0ubuntu0.20.04.1, or
$ sudo apt set up mariadb-client-core-10.3  # model 1:10.3.31-0ubuntu0.20.04.1
Enter fullscreen mode

Exit fullscreen mode

_make positive you have additionally put in a Sql driver if you’re operating on a Laravel app. Run:
$ sudo apt-get set up php7.4-mysql
_

Subsequent, go into PhpMyAdmin, find your created desk and click on on it. Navigate to Privileges among the many menu on the prime of the display. Discover all username with root and click on on Edit privileges. Tick on World privileges and click on Go.

Now, there’s an alternate technique to do that, utilizing Home windows Cmd or Shell from XAMPP and connecting to the MySql database immediately and kind in some SQL code immediately.

$ mysql -u root -p -h 127.0.0.1

then kind 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 consumer with entry from Wsl2 and permit connections from Wsl2 or any distant system and as you would possibly inform, may represent a safety danger since you’re granting all privileges.

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

Skip the password immediate by tapping enter.

Congratulations, you have linked to your MySql database in your Home windows host from Wsl2.

Now, for the Laravel app to have the ability to run migrations, it’s worthwhile to go into the .env file and edit the Sql part however earlier than that, run:
$ echo $(hostname).native

and duplicate or notice the consequence.

Subsequent, edit the MySql part of your .env file to the next:

DB_CONNECTION=mysql
DB_HOST=DESKTOP-BB111.native #this needs to be the results of the final command above and will finish with .native
DB_PORT=3306
DB_DATABASE=laravel_counter #that is the title of the database you created and as seen in your PhpMyAdmin
DB_USERNAME=root
DB_PASSWORD= #go away this empty
Enter fullscreen mode

Exit fullscreen mode

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

Watch as your migration masses and smile.

You’ve got efficiently linked your Wsl2 field to a service operating in your Home windows host. Now for those who do not thoughts, go reward your self a bit. Cheers.

Apparently, connecting to a Wsl2 service from home windows is way simpler and comes arrange, I assume due to Port Forwarding.

I hope this put up has helped you not directly. Thanks for studying.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments