Can't connect to MySQL on Mac -- missing mysql.sock file

tchaymore picture tchaymore · Apr 26, 2011 · Viewed 51.6k times · Source

I installed MySQL on my Mac OS 10.6 about a week ago, and, after some playing around, got it to work just fine. It integrated with python MySQLdb and I also got Sequel Pro to connect to the database. However, php wouldn't access the server. Even after I added a php.ini file to /etc/ and directed it toward the same sock that Sequel Pro was using: /tmp/mysql.sock.

But now I can't access the local server at all. As far as I can tell, there is no mysql.sock file anywhere on my computer, not in /tmp/ or anywhere else.

I can start the mysql server from Terminal, but it logs me out automatically after a minute:

110425 17:36:18 mysqld_safe Logging to '/usr/local/mysql/data/dn0a208bf7.sunet.err'.
110425 17:36:18 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
110425 17:37:58 mysqld_safe mysqld from pid file /usr/local/mysql/data/dn0a208bf7.sunet.pid ended

If I try to call "mysql" from the command line (which worked perfectly earlier today):

ERROR 2002 (HY000): Can\'t connect to local MySQL server through socket '/tmp/mysql.sock' (2)

The PHP error is of course similar:

PHP Warning:  mysql_real_escape_string(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock)

Also, there is no "my.cnf" file in my mysql installation directory: /usr/local/mysql. There are my.cnf files for the mysql installations that come along with XAMPP. Those also have the default socket listed as '/tmp/mysql.sock', but I had to change them manually.

Any ideas what's going on? Why would modifying the php.ini file have produced a change for Sequel Pro as well?

Answer

Riba picture Riba · Apr 26, 2011

First of all I suggest you to use homebrew to install any third part libraries or tools on your mac. Have a look to : Link

Otherwise for your problem you can search where is the mysql socket on your mac and then symlink it to /tmp.

In your terminal try something like :

locate mysql | grep sock

You will get something like :

/the/path/to/mysql.sock

Then do :

ln -s /the/path/to/mysql.sock /tmp/mysql.sock

This should works.

Also you can edit your php.ini file to set the correct path for the mysql socket.

Hope this help.