49
  1. Pulled a perfectly working laravel project from a git into a mac running MAMP. Project ran perfectly on a linux machine.
  2. composer install
  3. php artisan migrate, got the following error:

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

NB: php -v is 5.5 and mysql -v is 5.5 from the terminal Here is part of my config/database.php

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'essays',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

I tried replacing localhost with 127.0.0.1 with no avail. Kindly help..

Edit: I added these three lines in my php.ini

mysql.default_socket = /var/run/mysqld/mysqld.sock

mysqli.default_socket = /var/run/mysqld/mysqld.sock

pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

I also added this symlink:

sudo mkdir /var/mysql
cd /var/mysql && sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

But that didnt solve. I also pulled a fresh new laravel project from git and ran into the same error after composer install then php artisan migrate

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

The mac version is 10.7.4

ForguesR
  • 3,558
  • 1
  • 17
  • 39
Timothy
  • 4,198
  • 6
  • 49
  • 59
  • This has been asked before, and searching for your error message gives a lot of questions and possible answers. Try them. http://stackoverflow.com/questions/2412009/starting-with-zend-tutorial-zend-db-adapter-throws-exception-sqlstatehy000 http://stackoverflow.com/questions/6001592/what-would-cause-intermittent-sqlstatehy000-2002-no-such-file-or-directory-e – Sven Oct 20 '13 at 09:20
  • Thanks for the edit and info. I had done some thorough research before, added the php.ini lines and the symlink at http://stackoverflow.com/questions/2412009/starting-with-zend-tutorial-zend-db-adapter-throws-exception-sqlstatehy000 But That didnt solve. I also pulled a fresh new laravel project from git and ran into the same error after composer install then php artisan migrate – Timothy Oct 20 '13 at 11:06
  • Check the edits in my question.... – Timothy Oct 20 '13 at 11:14
  • Where is the `config/database.php` file? – Francisco Maria Calisto Sep 02 '15 at 17:37
  • 1
    If you are using homestead/vagrant, make sure that you have `ssh`d into the server. And not running it from the outside. – Kamran Ahmed Nov 28 '15 at 14:24

8 Answers8

175

If you are using MAMP be sure to add the unix_socket key with a value of the path that the mysql.sock resides in MAMP.

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
Sethen
  • 11,140
  • 6
  • 34
  • 65
keithics
  • 8,576
  • 2
  • 48
  • 35
  • This worked for me. Everything was great until I brilliantly decided I wanted to install/configure PHP via Homebrew on Mavericks. Needless to say I've been using an alias phpmamp='/Applications/MAMP/../pathto/php' for the last 2 weeks. Not sure I feel good about this fix, but it got me back up and running without the alias. Thanks! – Erik Aybar Mar 27 '14 at 15:29
  • 1
    Same here i ve used homebrew too so the solution was to add the unix_socket path in `config/local/database.php` file. – Marios Fakiolas Sep 28 '14 at 13:00
  • I added that line, but got following error: `[PDOException] SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected` Any idea what issue is? I'm seeing that the key was updated in an `.env` file, not the `database.php` file. Not really sure if that has anything to do with it... – frshjb373 Aug 08 '15 at 00:54
  • You can use this answer instead of adding the sockets. http://stackoverflow.com/a/12146144/662581 – Michel Ayres Aug 13 '15 at 14:29
  • I can not find the `config/local/database.php` file. Where can I find it? – Francisco Maria Calisto Sep 02 '15 at 17:43
  • For xampp, you can use /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock. Or mysql and do "status" – hawshy Sep 22 '16 at 08:03
  • Where do I find the file to add this line/path? – Isengo Jan 24 '17 at 10:31
  • 2
    Thanks, it worked for me adding this to `.env`: `DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock` – Max Oriola Jan 23 '18 at 20:41
23

Do not assume your unix_socket which would be different from one to another, try to find it.

First of all, get your unix_socket location.

$ mysql -uroot -p

Enter your mysql password and login your mysql server from command line.

mysql> show variables like '%sock%';
+---------------+---------------------------------------+
| Variable_name | Value                                 |
+---------------+---------------------------------------+
| socket        | /opt/local/var/run/mysql5/mysqld.sock |
+---------------+---------------------------------------+

Your unix_soket could be diffrent.

Then you got 2 solution to solve your issue:

(1) Change your config/database.php

    'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'essays',
    'username'  => 'root',
    'password'  => 'root',
    'unix_socket'   => '/opt/local/var/run/mysql5/mysqld.sock', //Your sock got from above
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

(2) Change your php.ini, find your php.ini file from

<? phpinfo();

You maybe install many php with different version, so please don't assume your php.ini file location, get it from your 'phpinfo';

Change your php.ini:

mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

Then restart your apache or php-fpm.

Community
  • 1
  • 1
lijinma
  • 2,914
  • 1
  • 23
  • 22
  • I switched from MAMP's own mysql to mariadb and your solution was the only solution that worked. – H35am Jun 15 '16 at 14:01
  • the php.ini solution works efficiently if you work in many project. you don't need to change database.php file in each project, just edit the php.ini file. thanks for the solution – mustofa27 Dec 29 '19 at 07:43
16

Had the same problem, but it's working for me now.

If anybody is still having problems, try this:

  • Make sure your bootstrap/start.php contains your actual hostname, not the name of your virtual host. Enter hostname in the terminal to get your hostname. As it's an array I believe you can enter both your hostname and the name(s) of your virtual host(s).
  • Replace "localhost" with "127.0.0.1".
Nick
  • 2,576
  • 1
  • 23
  • 44
14

If you are using XAMPP, the solution is:

'mysql' => array(
        'driver'      => 'mysql',
        'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
        'host'        => 'localhost'
)
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
cristianojeda
  • 339
  • 2
  • 12
9

This works for me in Laravel 5.0, change the DB_HOST=127.0.0.1:33060 in .env file.

Others answers don't work...

kguest
  • 3,804
  • 3
  • 29
  • 31
Igor Trindade
  • 111
  • 2
  • 5
7

If you are using Laravel 5.1.11 version + MAC + MAMPP

you have to add "Unix_socket " in file "yourapp"/app/config/database.php

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

Unix_socket param was added to above mysql config drive.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
jai
  • 541
  • 5
  • 8
5

For Laravel 5.0+ change localhost to 127.0.0.1 in your .env file as well before you go playing around with Unix Sockets etc - this worked for me.

Noobs beware: For anyone using Laravel 5 and using older learning material be aware that there was quite a marked shift in folder structure from previous versions though this seems to be for the better- check out this article https://mattstauffer.co/blog/laravel-5.0-directory-structure-and-namespace

brianjlennon
  • 159
  • 1
  • 8
3

Another solution is to add the port number in the host key. In this case MAMP uses the 8889 by default:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost:8889',
    'database'  => 'essays',
    'username'  => 'root',
    'password'  => 'root',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),
sveggiani
  • 31
  • 3