Solution for “Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’” error when using PDO
If you’re trying to get up-and-running with PDO, you may experience a “Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’” error when trying to connect to your database, even if your php.ini and my.cnf files contain a different default socket path for MySQL.
It’s counterintuitive to a degree, but it’s actually expected behavior. The good news is that the fix is easy. Simply include the unix socket path in your connection string.
$connection = new PDO("mysql:host=localhost;dbname=test;unix_socket=/path/to/socket/found_in_php.ini_or_my.cnf",'username','password');
Related: Notes from “Introduction to PDO,” a talk by Ilia Alshanetsky at NYPHPCon 2006.
Comments are closed.


















