Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post http://stackoverflow.com/a/9715164/1231563

The issue that arose here is that “localhost” uses a UNIX socket and can not find the database in the standard directory. However “127.0.0.1” uses TCP (Transmission Control Protocol), which essentially means it runs through the “local internet” on your computer being much more reliable than the UNIX socket in this case.

————————————————-/OR/———————————————————

Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:

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

The reason I found is just because PHP and MySQL can’t get connected themselves. To get this problem fixed, I follow the next steps:

Open a terminal and connect to the mysql with:

mysql -u root -p

It will ask you for the related password. Then once you get the mysql promt type the next command: