0

I use two OS X machines to develop my Ruby on Rails application. My desktop works fine, and my laptop was working fine until a few days ago when I got this error when the local server was running on pageload:

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

I get the same error when I try to run mysql from terminal.

I checked a few other threads, but none of their solutions helped my problem:

Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on OSX

Can't connect to local MySQL server through socket

The last link the guy seems to be having the exact same problem as I do, but the answer on that page didn't help me since I can't run mysql. I keep seeing this problem online where people fixed it because their .sock file was in the wrong location so I ran

$ ln -s /var/lib/mysql/mysql.sock /tmp

to try to create an alias but it looks like mysql.sock doesn't exist in either place. When I tried running:

$ mysqladmin variables | grep socket

I got this error:

Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

Lastly, I turned on invisible files and went looking around my computer, and couldn't find a mysql.sock file or mysql folder in any of the above directories. Once again, mysql WAS working fine on this computer a week ago, and has now inexplicably stopped working for some reason.

How do I restore or recreate my .sock file for this computer?

Community
  • 1
  • 1
cranderveldt
  • 109
  • 9
  • have you tried running mysqld? – coderek Jul 06 '12 at 01:19
  • Yeah, here's the output: https://gist.github.com/3068400 – cranderveldt Jul 07 '12 at 22:42
  • can you post the output of `ps aux | grep mysql` and `which mysql` here? It may be you are running 2 mysql servers in different places. – coderek Jul 08 '12 at 02:28
  • You might be onto something with this. https://gist.github.com/3069120 I recently had to change my computer's name on my office's network and that looks like it changed up some file paths in mysql too. Note "Old-Computer-Name" and "New-Computer-Name" in the gist. – cranderveldt Jul 08 '12 at 03:02

2 Answers2

0

I guess you have installed mysql using homebrew before. Note that you have your mysqld already running at

/usr/local/Cellar/mysql/5.5.24/bin/mysqld

However you current mysql command link to

/usr/local/bin/mysql

instead, it should be

/usr/local/Cellar/mysql/5.5.24/bin/mysql

You can try add alias to your bashrc pointing to the correct command.

Try kill all other mysqld_safe process before running mysql in a new window. It should read the correct mysql sock file now.

coderek
  • 1,860
  • 14
  • 20
  • That sounds awesome, what commands do I run to accomplish this? Sorry I am a mysql newbie. – cranderveldt Jul 08 '12 at 04:16
  • open `~/.bashrc` add `alias mysql="/usr/local/Cellar/mysql/5.5.24/bin/mysql"` at last line. and open new terminal to run `mysql` – coderek Jul 08 '12 at 04:19
  • Weird I don't have `~/.bashrc` at all. I have `~/.bash_history` and `~/.bash_profile` Any reason why I wouldn't have this file, or why it wouldn't be in my home directory? – cranderveldt Jul 08 '12 at 04:23
  • No good, I tried it in `/etc/bashrc` and I tried creating `~/.bashrc` with only that line in it but when I open a new Terminal window it still gives me `ERROR 2002 (HY000)`, etc. – cranderveldt Jul 08 '12 at 04:34
  • try restart your machine and make sure mysqld is running. – coderek Jul 08 '12 at 05:35
0

The reason could be the specified socket path in your Gemfile might be wrong.

First, to find your socket file:

mysqladmin variables | grep socket

For me, this gives:

| socket  /var/run/mysqld/mysqld.sock 

Then, add this line /var/run/mysqld/mysqld.sock to your config/database.yml in the socket field. See the below examples

Example: socket: /var/run/mysqld/mysqld.sock

Development Mode

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock #  this line

Production Mode

production:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock  #  this line

Test Mode

test:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /var/run/mysqld/mysqld.sock  #  this line
Prabhakar
  • 6,458
  • 2
  • 40
  • 51