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?

9
  • 68
    Use IP address for the MySQL host
    – Nguyen
    Sep 6, 2012 at 16:02
  • 3
    @Nguyen you're da man! you helped more than the dude with 183k lol
    – abbood
    Mar 31, 2014 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, 2014 at 2:25
  • 2
    Thank you! It helped me too. Important: not to use http://, just IP.
    – Liglo App
    Oct 5, 2014 at 15:09
  • 2
    restarting the mysql server, did the job for me! Dec 13, 2014 at 6:22

7 Answers 7

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 ? )

8
  • 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 9, 2010 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 ;-) Mar 9, 2010 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. Jul 8, 2011 at 14:44
  • 1
    Great thanks, so strange is it, any way it is solved in this way. Feb 25, 2014 at 8:52
  • 2
    super late to the party here, but wanted to say a big thanks to @PascalMARTIN for this post. life saver! Mar 28, 2014 at 15:32
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.

1
  • 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, 2014 at 13:24
15

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

Hope it helps!

0
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
3
  • 3
    works with mamp too; cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock Nov 19, 2013 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 7, 2014 at 0:43
  • cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock Worked on my MAMP / Laravel setup too.
    – sotoz
    May 5, 2015 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';
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

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