Search code examples
mysqllaraveldockerlaradock

Laradock: SQLSTATE[HY000] [2002] No such file or directory error when running php artisan migrate


I'm using Laradock for my Laravel project, and when I run php artisan migrate, I encounter the following error:

SQLSTATE[HY000] [2002] No such file or directory

I've checked my .env file and my MySQL database configuration, but I'm still facing this issue. Here are my configuration settings:

.env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravelApp
DB_USERNAME=root
DB_PASSWORD=root

Laravel configuration in config/database.php:

'mysql' => [
    'driver' => 'mysql',
    'url' => env('DB_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'laravel'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    //'unix_socket' => env('DB_SOCKET', ''),
    'charset' => env('DB_CHARSET', 'utf8mb4'),
    'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

I've tried changing DB_HOST to 127.0.0.1, but I still get the Connection refused error. Any suggestions on how to resolve this issue?"


Solution

  • The issue was due to the DB_HOST value in my .env file being set to localhost, while the MySQL container in Laradock was named mysql. Changing the DB_HOST value in my .env file from localhost to mysql, which is the name of my MySQL container, resolved the issue.

    It's important to note that the key point here is to use the name of your Docker MySQL container as the DB_HOST value in your .env file.

    I hope this solution helps others facing similar issues!