0

I started a new small project on MAC OS X Yosemite and i have a problem to connect to MySQL with PDO in PHP 5.5.22 like this:

$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();
}
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=LOYER;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
    die('Erreur : ' . $e->getMessage());
}

The first respond:

Connection failed: SQLSTATE[HY000] [2002] Connection refused

and the second :

Erreur : SQLSTATE[HY000] [2002] No such file or directory

Anyone got an idea ?

I already looked for the socket and nothing seems wrong.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Damien Christophe
  • 115
  • 1
  • 1
  • 10
  • Have you actually downloaded and installed the latest MySQL version available ? Yosemite don't come with MySQL...If you have MySQL installed already, go to System Preferences and check if MySQL server instance is started. – Whirlwind Apr 06 '15 at 20:31
  • 4
    connection refused = mysql's not running. 'no such file' = socket file not where it's supposed to be. – Marc B Apr 06 '15 at 20:33
  • If you have installed mysql try starting it – Arlind Hajredinaj Apr 06 '15 at 21:06
  • try to naviguate to http://localhost:3306/ if your browser says connection refused mysql isn't running on the standard port (or isnt running at all); if it tries to download a file or display some strange text, mysql is running. – mathieu Apr 06 '15 at 22:42
  • I use MAMP so MySQL is running i can access PhpMyAdmin on localhost:3306 – Damien Christophe Apr 06 '15 at 23:09
  • when i do php -i | grep pdo i can see the line : pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock but there is no file mysql.sock in /tmp in MAMP the file is here : /Applications/MAMP/tmp/mysql/mysql.sock how can i change the path ? like this : pdo_mysql.default_socket => /Applications/MAMP/tmp/mysql/mysql.sock – Damien Christophe Apr 06 '15 at 23:29
  • Did you ever figure this out because I am having the same problem – robk27 Jul 20 '16 at 18:37
  • Sorry, it was just a mistake from my side – Damien Christophe Mar 12 '18 at 11:29

0 Answers0