3

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?

4 Answers 4

6

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'        => '',
    )
5
  • This works for me but it would be nice if someone could explain why this was needed?
    – Kevin Jung
    Apr 9, 2015 at 19:09
  • Works for me too! thanks. but i don't know why we have to add this. I checked php.ini and the default unix_socket it's the same as this one that we set. Jun 20, 2015 at 18:14
  • @KevinJung just make sure to take it out when deploying on the server
    – Haring10
    Jun 22, 2015 at 11:12
  • Wow this had me befuzzled. Thanks Jul 17, 2016 at 23:48
  • I have been looking for a solution to this for an hour. When I saw this i thought, surely not... Then it worked. THANK YOU!
    – wuno
    Feb 4, 2017 at 18:16
1

fist clear your config file by typing

php artisan config:clear

Then modify your .env file

APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1

Hope this works for you.

0

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

0

I had to do the following in ubuntu 15.10

copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf

Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')

'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'

That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.