David White
David White

Reputation: 11

MAMP on Yosemite - connection error : SQLSTATE[HY000] [2002] No such file or directory

I know that I have seen this question before but have not seen an answer that has been able to fix the issue on my machine.

I'm relatively new to PHP and have no experience with Apache settings.

I am using the latest version of MAMP (not pro) with all the default settings on the latest version of OS X (10.3.3).

When I try to connect via PDO I receive the following error:

Unable to connect to the database server.exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/resources/config.php:7 Stack trace: #0 /Applications/MAMP/htdocs/resources/config.php(7): PDO->__construct('mysql:host=loca...', 'USERNAME', 'PASSWORD') #1 /Applications/MAMP/htdocs/WEBSITE/index.php(2): include_once('/Applications/M...') #2 {main}

The connection code is from Kevin Yank's Novice to Ninja book, modified to apply to my database, username, etc.

try
{
  $pdo = new PDO('mysql:host=localhost;dbname=db', $DBUser,$DBPass);
}
catch (PDOException $e)
{

  $error = 'Unable to connect to the database server.' . $e;
  include 'error.html.php';
  exit();
}

I have tried localhost and 127.0.0.1 and get the same error regardless.

I don't see anything in Console that could help but I may not be looking for the right thing.

Any help is appreciated. Thanks

Upvotes: 1

Views: 2683

Answers (2)

Rohit Kaushik
Rohit Kaushik

Reputation: 1302

try this command in mac

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Upvotes: 4

YvesLeBorg
YvesLeBorg

Reputation: 9079

This could happen if mysqld is not running (stuff happens). It could also happen if you have a configuration error.

For example, on my installation, i reproduce the error with this DSN :

mysql:unix_socket=/tmp/mysqle.sock;dbname=notifications;charset=utf8 

the config calls for /tmp/mysql.sock

Check the config to see what method is configured (port or socket) and which (port or socket). If you have the wrong port or socket in your DSN, you will produce the same error report in the question.

Upvotes: 1

Related Questions