Installing mysql on leopard: "Can't connect to local MySQL server through socket"

Neil picture Neil · Jul 16, 2009 · Viewed 13.6k times · Source

I migrated to a new machine and used migration assistant to copy across my files (which seemed to copy across the DBs) but I had to use macports to install Mysql (whereas last time I compiled from source via Dan Benjamin's guide). For some reason, mysql is intermittently throwing the following error;

Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)

It does this no matter what I try, which has included setting the socket in /opt/local/etc/mysql5/my.cnf. Previously I've managed to temporarily fix this by restarting the machine, but right now it just doesn't want to know, despite grep mysql telling me I seem to have a pid;

  0    46     1   0   0:00.01 ??         0:00.01 /opt/local/bin/daemondo --label=mysql5 --start-cmd /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper restart ; --pid=none
  0    70     1   0   0:00.01 ??         0:00.01 /bin/sh /opt/local/lib/mysql5/bin/mysqld_safe --datadir=/opt/local/var/db/mysql5 --pid-file=/opt/local/var/db/mysql5/localhost.pid
 74   100    70   0   0:09.22 ??         1:02.68 /opt/local/libexec/mysqld --basedir=/opt/local --datadir=/opt/local/var/db/mysql5 --user=mysql --pid-file=/opt/local/var/db/mysql5/localhost.pid --socket=/tmp/mysql.sock
501 66217 65266   0   0:00.00 ttys001    0:00.00 grep mysql

How do I fix this? Are there any steps I can take next? I've been trying for a few weeks now and I've read round all relevant blog posts, so I'm completely out of ideas.

Answer

Rob Wilkerson picture Rob Wilkerson · Sep 13, 2009

This is way after the fact, but here's what I did:

touch /opt/local/var/run/mysql5/mysqld.sock
vi /opt/local/etc/mysql5/my.cnf

Add the following lines to my.cnf:

[mysqld_safe]
socket = /opt/local/var/run/mysql5/mysqld.sock

[client]
socket = /opt/local/var/run/mysql5/mysqld.sock

This should allow you to connect. This worked for me on both Leopard and Snow Leopard.