MySQL socket error on your new Mac

I hate get getting this error.

Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

You get a new mac, install mac ports, install your Rails environment and you get this. I’ve solved this problem before, but I always forget what I did. So this time, I’m documenting it.

The first easy fix is this, in your database.yml you can add the line

host: 127.0.0.1

under adapter. This will use TCP/IP sockets instead of local bindings. It works, but purists will say this is slower. The real problem is there is no /tmp/mysql.sock file. You need to find where your socket exists. On Lion, I used

mysqladmin variables | grep socket

Which told me the location of my socket. In my case this was

/opt/local/var/run/mysql5/mysqld.sock

The simple solution is to create a symlink so that when Rails looks for /tmp/mysql.sock it points to the above path.

ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

Now rails work the way you want it to.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *