Home / Snippets / How to Fix Laravel PDOException SQLSTATE[HY000] [2002] No such file or directory
How to Fix Laravel PDOException SQLSTATE[HY000] [2002] No such file or directory cover

How to Fix Laravel PDOException SQLSTATE[HY000] [2002] No such file or directory

3.7K

3 years ago

0 comments

Often times you may come across an error that outputs "PDOException SQLSTATE[HY000] [2002] No such file or directory" in Laravel. This error typically appears when you want to run a database migration and there are several possible ways to fix this.

Step 1: Check if MySQL is installed and Running on Your Machine


The first thing to note is that you need to have MySQL installed and running on your machine. Depending on your operating systems there are several possible ways to install MySQL.
  1. Using MySQL provided by Laravel Homested
  2. Using MySQL provided by DBngin (macOS only)
  3. Using MySQL provided by the Native Installer
  4. Using MySQL provided by XAMPP / WAMP & etc

Step 2: Update Your Laravel .env Environment File


Now make sure to update your Laravel environment file (.env) DB_* configs. Do note that for the DB_HOST it's specified as 127.0.01 instead of the default "localhost".
DB_CONNECTION=mysql
DB_HOST=127.0.01
DB_PORT=3306
DB_DATABASE=your_db_name_here
DB_USERNAME=root
DB_PASSWORD=

Step 3: Clear Config and Run Migration Command


Now that you have done the steps above you need to clear your configuration file cache by running the code below.
php artisan config:clear
And try to migrate once again.
php artisan migrate

Other Options: Restart MySQL


Sometimes this can be caused by not having MySql running on the machine. If that's the case do start MySql and try the code once again. If you are using Ubuntu then you can run the code below.
sudo service mysqld start
If you are on macOs and you are using homebrew run the service command.
brew services restart mysql
On windows, if you are using a GUI app like XAMPP just start it using the start button. If you do have any other suggestions do comment down below and let's start the discussion, Cheers!.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this