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

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


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'        => '',    )


fist clear your config file by typing

php artisan config:clear

Then modify your .env file

APP_URL = 127.0.0.1DB_HOST = 127.0.0.1

Hope this works for you.


IF you use lampp . You can try this:

Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).

Find and see "socket =/opt/lampp/var/mysql/mysql.sock",

Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:

'mysql' => [        'driver'    => 'mysql',        'unix_socket'      => '/opt/lampp/var/mysql/mysql.sock',        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE', 'luanvan'),        'username'  => env('DB_USERNAME', 'huuthang'),        'password'  => env('DB_PASSWORD', '123456'),        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',        'strict'    => false,    ],

File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck