23

I am trying to run a laravel project on OS X. I installed MySql with homebrew. I tried googling around for why I'm getting this error:

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

when I try to do

php artisan migrate

And most of the answers I see say php and MySql cannot communicate to each other. I've been trying to see where I went wrong since I think I followed previous posts about it, and it still doesn't work. I created a page that just does this:

<?php
    phpinfo();
?>

And I see these types of things: In the Configure area, I see:

'--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=mysqlnd' '--with-mysql=mysqlnd' '--with-pdo-mysql=mysqlnd' '--enable-pcntl' '--enable-zend-signals' '--enable-dtrace'

loaded configuration file:

/usr/local/etc/php/5.4/php.ini

If I ls /tmp, I do see mysql.sock.

In that php.ini file, I set these lines: pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket = /tmp/mysql.sock mysqli.default_socket = /tmp/mysql.sock

And then in /usr/local/Cellar/mysql/5.6.16/my.cnf, I added the following line:

socket = /tmp/mysql.sock

I DID restart apache, as well as check mysql is running. I haven't seen any other advice besides the php and mysql connection. I think there are two versions of php on my system because when I installed mcrypt or something else I can't remember, it installed another php version. But when I do look at the output of phpinfo, I'm editing the appropriate php.ini file. I didn't know if there is another piece I am missing. Any thoughts? Thanks

2
  • i just got this error by trying to enable the "mysql general query log" to debug some doctrine/php code i'm working with.
    – Randy L
    Feb 18, 2015 at 19:25
  • I just had to enable mysqli.so extension in php.ini
    – Mehulkumar
    Apr 23, 2017 at 9:42

7 Answers 7

50

I had a similar error. I am using MAMP and what solved my issue was:

  1. creating a symbolic link in: /var/mysql (create the directory if it does not exist)
  2. cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock
8
  • 7
    This fixed it for me. I'm not using MAMP though, so it was cd /var/mysql && sudo ln -s /tmp/mysql.sock
    – mire
    Nov 20, 2014 at 15:43
  • @MikeRentas You just saved me some sanity... Every reference i see is using MAMP and I just set up everything manually -- yours was the only thing that worked, though i'm getting a permissions error now. Mar 25, 2015 at 23:34
  • @MikeRentas Your particular solution worked great for me trying to set up a legacy Symfony 1.4 app on Mac OS X Yosemite. I had installed MariaDB via Homebrew, which I guess places the mysql.sock file in /tmp.
    – Blake B.
    Apr 28, 2015 at 18:03
  • solved the problem for me, but can you explain why it works please?
    – dkonayuki
    Jun 23, 2015 at 14:32
  • @HappyCoder Thank you, this solved it for me on MAC OSX using MAMP Oct 30, 2016 at 14:53
10

For users which are looking for a solution on XAMPP (Tested and worked on Mac OS X),

Open terminal and create a symbolic link :

sudo mkdir /var/mysql
cd /var/mysql
sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
6
  • 1
    Hello, i tried this but no luck, i get the error SQLSTATE[HY000] [2002] Connection refused. I am using slim php framework, xampp on Mac OS. Any ideas to fix this?
    – Pathros
    Jun 28, 2015 at 2:00
  • 3
    Try using "localhost" instead of a "127.0.0.1" or vice-versa when connecting to your DataBase to see if it helps.
    – F3L1X79
    Jul 15, 2015 at 8:50
  • Thanks! Now it works with localhost. Then what should I do to make it work with 127.0.0.1 as well?
    – Pathros
    Jul 17, 2015 at 13:34
  • I don't know the slim php framework, but it might have something to do with your configuration file?
    – F3L1X79
    Jul 17, 2015 at 14:09
  • Worked like a charm - thanks. PS I used it in MAMP with the MAMP path.
    – Isengo
    Jan 24, 2017 at 10:55
7

I found PHP on CentOS 6 built with mysql.default_socket=/var/lib/mysql/mysql.sock yet for some reason pdo_mysql.default_socket=/tmp/mysql.sock, which doesn't exist. Building it using --with-mysql-sock=/var/lib/mysql/mysql.sock should force both to be the same.

Alternatively edit php.ini to contain:

pdo_mysql.default_socket="/var/lib/mysql/mysql.sock"

...or your correct socket location, of course.

6

After searching, I found this post:

Setting up Laravel on a Mac php artisan migrate error: No such file or directory

Once I added

'unix_socket' => '/tmp/mysql.sock’,

to my database.php in the mysql array, it worked. Not sure exactly why that works, but it does.

5

i fixed it like this.Just add a line. in laravel project APP=>Config=>database.php update the
Connections=>mysql after strict add below line

'unix_socket' => "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"

sample:-

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            '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' => "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"
        ],
1
  • I added to my .env DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock Jan 20, 2021 at 5:29
0

This happened to me out of nowhere, my app (Laravel) was working fine yesterday and dropped today with this error (SQLSTATE[HY000] [2002] No such file or directory).

After checking that my config file had not been not corrupted, I connected to my control panel (I am using a VPS) and restarted MySQL server. That did the trick, everything is back online.

0

Found the solution for Ampps user:
I had the same issue and I did a lot of searches but couldn't solve the issue until I looked at my MySql config file. In my config file socket was in line 21 and the path was
"/Applications/AMPPS/var/mysql.sock"

In Ampps application click on MySql tab and then click on configuration button.

You can also find the config file in "/Applications/AMPPS/mysql/etc" folder

So simply I added
'unix_socket' => '/Applications/AMPPS/var/mysql.sock',
to my database.php in mysql array and it's worked for me.
After host line. I have tested with localhost and 127.0.0.1 and both worked
Hopefully it's working for you too.

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