Tiffany B. Brown

A web log about web development and internet culture with frequent detours into other stuff.
On perception and evidence
Review: Learning PHP Data Objects by Dennis Popel

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.

Share this entry:
  • TwitThis
  • Digg
  • Technorati
  • del.icio.us
  • Ma.gnolia
  • Mixx
  • NewsVine
  • Reddit
  • StumbleUpon
  • TailRank
  • Furl
  • Slashdot
  • Global Grind
  • YahooMyWeb
  • Facebook
  • Google
  • Live
  • LinkedIn
  • MySpace
Comments are closed.
previous post: On perception and evidence
next post: Review: Learning PHP Data Objects by Dennis Popel