Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql::Erro

MySQL isn’t running, and I can’t figure out why. I’m not a back-end developer, so I don’t know very much about MySQL. The project I’m working on is using Ruby with Rails and a MySQL database. I’m the designer and front-end dev trying to view the files locally using webrick (rails local server). I’ve done it successfully once before, but I don’t know why it isn’t working now. This is the error message I get in the terminal when I try to run “rails server” in my project’s directory:

“Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2) (Mysql::Error).”

Since this is a new clone of a project, I went ahead and did a quick bundle install to make sure I had everything I needed. Even after that, I still got the same error message.

Below shows the web I’ve woven so far in trying to figure out the above error message.

From System/usr/local/bin/, I typed:


$sudo ./mysqld_safe &
[1] 6983
$./mysqladmin -u root shutdown
./mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
$ telnet localhost:3306
localhost:3306: nodename nor servname provided, or not known
$ telnet 127.0.0.1:3306
127.0.0.1:3306: nodename nor servname provided, or not known
$ telnet
telnet> quit

That was mostly the Rails developer trying to figure out why it wasn’t working. I’m hoping it’s useful to you as well. His conclusion was that I needed to create a mysql root password to see if that would fix it because I didn’t create one when I first installed mysql. I tried that by doing this:


$mysqladmin -u root password mypassword
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

After that, we thought it might be easier to uninstall mysql altogether and reinstall it. Since I initially installed it using Homebrew, I also uninstalled and reinstalled it the same way, which worked just fine.


$ brew uninstall mysql
$ brew install mysql

Then we tried this again, which had the same error:


$ telnet 127.0.0.1:3306
127.0.0.1:3306: nodename nor servname provided, or not known

$ netstat, however, returned a huge list of things.

The we tried to start the mysql server:


$ mysql.server start
Starting MySQL
.. ERROR! The server quit without updating PID file (/usr/local/var/mysql/Nickis-MacBook-Air.local.pid).

So we tried to remove that file:


$ rm /usr/local/var/mysql/Nickis-MacBook-Air.local.pid
rm: /usr/local/var/mysql/Nickis-MacBook-Air.local.pid: No such file or directory
$ sudo rm /usr/local/var/mysql/Nickis-MacBook-Air.local.pid
rm: /usr/local/var/mysql/Nickis-MacBook-Air.local.pid: No such file or directory

That is the error message I’m left with now, but the one at the top I believe is still relevant.

I really hope someone can help. I’m completely stuck.

Ok, so I figured this out with the help of the back-end dev a day or two after I posted this. This is what we did to fix it:

Uninstalled mysql with homebrew:


$brew uninstall mysql

Moved to user/local/Cellar directory:


$cd /
$cd usr/local/Cellar

Tried to find all files and clean things up:


$which mysql
$locate mysql
$brew cleanup
$brew remove mysql
$launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
$ rm ~/Library/LaunchAgents/com.mysql.mysqld.plist

I think the $brew cleanup helped a lot.

Then reinstalled mysql with homebrew:


$brew install mysql

This is what I think really made it work correctly. I needed to set up the database to run as my user account and start mysql manually.


$unset TMPDIR
$mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$mysql.server start
Starting MySQL
.. SUCCESS!
$mysql -uroot
mysql> quit
Bye
$mkdir -p ~/Library/LaunchAgents
$cp /usr/local/Cellar/mysql/5.5.20/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
$launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

And it works! Hope this helps someone out there.

Thanks for updating with the solution.