PHP & Mysql socket error

by Pascal Opitz

published 8 July 2007

I keep coming across this whenever I set up MySQL and PHP on a new mac: mysql_connect() fails throwing an error …

 mysql_connect() [function.mysql-connect]: Can't connect to local 
MySQL server through socket '/var/mysql/mysql.sock'

And everytime I forget to remember what the solution was:
This can be fixed by changing settings in /etc/php.ini:


sudo cp /etc/php.ini.default /etc/php.ini
sudo vim /etc/php.ini

Then set the mysql default socket to the temp folder:


mysql.default_socket = /tmp/mysql.sock

Quite annoying, but then again: Hey, I’ve got a new Mac!!!

Comment

  1. Alternatively, you can also refer directly to the socket file in the call to mysql_connect (or in the configuration file of a CMS), replacing the host name ‘localhost’ with ’:/tmp/mysql.sock’ (note the leading colon). That’s my preferred method.
    Michel Fortin    8 July, 4:22am    #
  2. I ran into this problem. I set the server to 127.0.0.1 and it then it worked perfect.
    ernest leitch    8 July, 4:35am    #
  3. or just use the excellent MAMP, which doesn’t have any of these problems ;)
    — tom    8 July, 3:15pm    #

Have your say

If your comment doesn't show up straight away, please don't be offended - we're not censoring, we are just trying to keep out all the viagra and poker ads. Thanks to an explosion of spam all comments are held for manual green-lighting.

name Remember
email
http://
Message <?>

Quick links

Other people's articles that we think you might be interested in: