Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Laravel and Databases Migrations & Schema Builder

Mike Zhu
Mike Zhu
1,840 Points

" SQLSTATE[HY000] [2002] No such file or directory" Error when Migrate

I am connecting to my MySQL database using Laravel Framework. This is what I write in the database.php: 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'letsport_laravel'), 'username' => env('DB_USERNAME', 'xxxxxxxx'), 'password' => env('DB_PASSWORD', 'xxxxxxxxx'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],

It is a MySQL database I bought in the Bluehost. However, when I run the php artisan migrate, it shows the error like this: SQLSTATE[HY000] [2002] No such file or directory

Is there any solutions?

ravindra barthwal
ravindra barthwal
2,271 Points

Hello, First make sure you connection is showing success (you can check by getting some value from db)

Secondly, If problem still remain , then make sure your all code is correct you are working on. This happen because artisan command work only after having everything ok. For me mainly packages makes problem, sometime i delete packages but don't remove them from config/app.php

1 Answer

like ravindra barthwal mentioned, first check connection. So simply add a route with a closure in your routes.php file like this:

1.)

Route::get('/db-test', function() {
   if(DB::connection()->getDatabaseName())
   {
      echo "conncted sucessfully to database ".DB::connection()->getDatabaseName();
   }
}

Go to /db-test, if you see the echo statement your db is connected successfully.

2.) sounds stupid, but happens to me all the time: make sure you execute the command in the terminal that has database connection, so in example your Homestead ssh connection