Laravel PHPUnit PDOException exception

2.3k views Asked by At

I don't know why when I run a PHPUnit test, I get the following error:

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

My testing environment database setting is:

return [

    'fetch' => PDO::FETCH_CLASS,

    'default' => 'mysql',

    'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'clg_test',
            'username'  => 'root',
            'password'  => 'veryHardPass',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ]
    ],

    'migrations' => 'migrations',

];

The reason why I am using MySQL as oppose to SQLite is because my migration files use dropColumn which is not supported by SQLite. I call the migration via Artisan::call('migrate') in the setup.

If I actually call migration manually in the terminal via php artisan migrate --env=testing then the migration IS successful and the databases are created.

Why am I then facing the above problem?

1

There are 1 answers

2
Joel Hinz On BEST ANSWER

Try changing localhost to 127.0.0.1. The message you're getting is indicative of the script not being able to connect to MySQL through a socket, but using an ip should work.