69

I have started to learn Zend Framework with the Book "Zend Framework in Action" in German.

Right there where it starts to get interesting, my PHP Unit Test throws this Error: "Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] No such file or directory"

I can't find any hints through Google searches. I did everything like it is in the book. Can anyone give me a hint as to where to search for the fault?

Is this a common beginner mistake?

matwr
  • 1,548
  • 1
  • 13
  • 23
Oliver
  • 695
  • 1
  • 6
  • 5
  • 68
    Use IP address for the MySQL host – Nguyen Sep 06 '12 at 16:02
  • 3
    @Nguyen you're da man! you helped more than the dude with 183k lol – abbood Mar 31 '14 at 20:10
  • 8
    @Nguyen - thanks this fixed it for me too (changing 'localhost' to 127.0.0.1). This was in PDO under Laravel, so its not specific to Zend Framework. – scipilot Jul 19 '14 at 02:25
  • 2
    Thank you! It helped me too. Important: not to use `http://`, just IP. – Liglo App Oct 05 '14 at 15:09
  • 2
    restarting the mysql server, did the job for me! – happyhardik Dec 13 '14 at 06:22
  • Possible duplicate of [PHP - MySQL connection not working: 2002 No such file or directory](http://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory) – kenorb Nov 13 '15 at 12:22
  • Why does this work? Where is it looking for localhost? – Danial Dec 05 '16 at 16:49
  • @Danial - explicit IP [can force](http://serverfault.com/a/337844/) a TCP connection, while 'localhost' tries via Socket, among [other issues, TCP can be slower](http://serverfault.com/questions/466155/when-to-use-a-mysql-socket-and-when-to-use-a-hostport), so that _dude with 183k_ does have a point... – kubi Jan 10 '17 at 11:16
  • First things first: Check if your mysqld is actually running fine `systemctl status mysql` – JepZ Dec 05 '17 at 07:26

7 Answers7

74

I would say that you have a problem connecting from PHP to MySQL...

Something like PHP trying to find some socket file, and not finding it, maybe ?
(I've had this problem a couple of times -- not sure the error I got was exactly this one, though)


If you are running some Linux-based system, there should be a my.cnf file somewhere, that is used to configure MySQL -- on my Ubuntu, it's in /etc/mysql/.

In this file, there might be something like this :

socket = /var/run/mysqld/mysqld.sock


PHP need to use the same file -- and, depending on your distribution, the default file might not be the same as the one that MySQL uses.

In this case, adding these lines to your php.ini file might help :

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

(You'll need to restart Apache so the modification to php.ini is taken into account)

The last one should be enough for PDO, which is used by Zend Framework -- but the two previous ones will not do any harm, and can be useful for other applications.


If this doesn't help : can you connect to your database using PDO, in another script, that's totally independant of Zend Framework ?

i.e. does something like this work (quoting) :

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

If no, the problem is definitly not with ZF, and is a configuration / installation problem of PHP.

If yes... Well, it means you have a problem with ZF, and you'll need to give us more informations about your setup (like your DSN, for instance ? )

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 6
    YEAH! Thank you so much!!! Without this hint I would have been sitting here for hours... Thank you for your fast reply. The problem: I have installed MAMP on my Mac (my ubuntu server is currently down) And for some reason my Unit-Test uses the /etc/php.ini instead of the /Applicaton/MAMP/:bla:/php.ini So to go on in my code i simply copied the socket paths from the Mamp-ini into the etc/php.ini and it works! – Oliver Mar 09 '10 at 20:11
  • 1
    Glad to see I could help, and that you solved your problem :-) ;; thanks for the comment and the explanation of the "why" it didn't work ! Have fun ;-) – Pascal MARTIN Mar 09 '10 at 20:13
  • 3
    Thanks for the hint and also the comment, I'm also on Mac, had the same problem. I don't use a any php.ini, so I just ran "sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock" and it's ok now. – Mathias Conradt Jul 08 '11 at 14:44
  • 1
    Great thanks, so strange is it, any way it is solved in this way. – Sswater Shi Feb 25 '14 at 08:52
  • 2
    super late to the party here, but wanted to say a big thanks to @PascalMARTIN for this post. life saver! – John Blythe Mar 28 '14 at 15:32
  • That is it! Answer is correct. Check the php.ini and add the correct socket path – Hanynowsky May 02 '14 at 10:38
  • Thanks - for me it was `localhost` vs `127.0.0.1` on a CircleCI build server – scrowler Nov 18 '15 at 22:33
  • @PascalMARTIN Please I have restarted my apache after updating all the parameters and yet the error still persist. What can I check again. I have Xampp installed on mac pro Catalina. I always have issue running migration on any Laravel application. – trustidkid Aug 07 '20 at 12:27
36

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 -u root -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 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
  • 1
    I had exactly the same problem: I had an empty value in mysql.default_socket, so I've changed the value to real socket location like lijinma pointed out and it worked! thanks! – nefski Nov 13 '14 at 13:24
15

Try setting host=127.0.0.1 on your db settings file, it worked for me! :)

Hope it helps!

Jordi Corominas
  • 1,244
  • 10
  • 15
6

i had this problem when running the magento indexer in osx. and yes its related to php problem when connecting to mysql through pdo

in mac osx xampp, to fix this you have create symbolic link to directory /var/mysql, here is how

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

if the directory /var/mysql doesnt exist, we must create it with

sudo mkdir /var/mysql
risnandar
  • 5,513
  • 1
  • 26
  • 17
  • 3
    works with mamp too; cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock – Mehmet Emre Portakal Nov 19 '13 at 19:54
  • 4
    Instead of this nasty hack, you should consider creating a `php.ini` file for OSX PHP (`sudo cp /private/etc/php.ini.default /private/etc/php.ini`) and then look for `pdo_mysql.default_socket` and set is as: `pdo_mysql.default_socket=/Applications/MAMP/tmp/mysql/mysql.sock` (for MAMP). – TCB13 Aug 07 '14 at 00:43
  • cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock Worked on my MAMP / Laravel setup too. – sotoz May 05 '15 at 18:55
4

Im also suffered from this problem & simply, by adding port number after the ip address saved me.

$dsn = 'mysql:dbname=sms_messenger;host=127.0.0.1:8889';
$user = 'root';
$password = 'root';

This works for Mac OS

If you don't know your mysql port number, then
You can easily find the port number on MAMP home page

OR

Type following command while running the MAMP server to switch the terminal into mysql

OMBP:mamp Omal$ /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot

Then type

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
Omal Perera
  • 2,971
  • 3
  • 21
  • 26
1

This error because mysql is trying to connect via wrong socket file

try this command for MAMP servers

cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock

or

cd /tmp && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock

and this commands for XAMPP servers

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

or

    cd /tmp && sudo ln -s /Applications/XAMPP/tmp/mysql/mysql.sock
1

It looks like mysql service is either not working or stopped. you can start it by using below command (in Ubuntu):

service mysql start

It should work! If you are using any other operating system than Ubuntu then use appropriate way to start mysql

Jitendra Mandloi
  • 283
  • 2
  • 12