Search code examples
mysqllaravelmigrationmamplaravel-artisan

Laravel Can't Connect to database - Migrations - Error 2002


I have searched for a couple of hours now, and still am unable to find this.

I get 2 errors, if I use the database host as 'localhost', I get this error:

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

and if I change the database host to '127.0.0.1' I get this error:

 [PDOException]                             
 SQLSTATE[HY000] [2002] Connection refused

Things I have tried:

  • changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
  • changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
  • changing the host of the connection from localhost to 127.0.0.1 and back.
  • creating a new user in phpmyadmin
  • turning off the firewall

Any ideas how to fix this?


Solution

  • I figured it out, add this after the 'host' => '127.0.0.1':

    'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock'
    

    So the connection would look like this:

    'mysql' => array(
            'driver'        => 'mysql',
            'host'          => '127.0.0.1',
            'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
            'database'      => 'dbname',
            'username'      => 'josh',
            'password'      => 'pass',
            'charset'       => 'utf8',
            'collation'     => 'utf8_unicode_ci',
            'prefix'        => '',
        )