Content with Style

Web Technique

PHP & Mysql socket error

by Pascal Opitz on July 7 2007, 19:58

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!!!


  • 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.

    by Michel Fortin on July 7 2007, 22:22 #

  • I ran into this problem. I set the server to and it then it worked perfect.

    by ernest leitch on July 7 2007, 22:35 #

  • or just use the excellent MAMP, which doesn’t have any of these problems ;)

    by tom on July 8 2007, 09:15 #

  • Victor, as Michel said above, you need to find out where the socket lives, and use the path instead of localhost (or whatever you\'ve added for your db host) in your application. If you\'re on a shared host, though, my hunch is that you simply don\'t have access to a db.

    by Matthias Willerich on March 10 2009, 14:22 #

  • hi, i\'m very interested about this, because i have the same problem, but i don\'t have access to the server configuration. How can i refer to the socket file?

    by VĂ­ctor on March 5 2009, 19:18 #