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

Crystal
  • 28,460
  • 62
  • 219
  • 393

7 Answers7

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
HappyCoder
  • 5,985
  • 6
  • 42
  • 73
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
F3L1X79
  • 2,575
  • 2
  • 29
  • 45
  • 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 '15 at 02: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 '15 at 08: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 '15 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 '15 at 14:09
  • Worked like a charm - thanks. PS I used it in MAMP with the MAMP path. – Isengo Jan 24 '17 at 10:55
  • when you say open terminal, should i open the terminal at any particular folder? should i be openning this at the folder of my laravel application? – Screwtape007 Jun 06 '20 at 22:26
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.

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Steve
  • 71
  • 1
  • 1
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.

Community
  • 1
  • 1
Crystal
  • 28,460
  • 62
  • 219
  • 393
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"
        ],
gamal
  • 1,587
  • 2
  • 21
  • 43
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.

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
Stef
  • 1
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.

Martian.titan
  • 466
  • 5
  • 19