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

Jamie Buchanan picture Jamie Buchanan · Mar 31, 2011 · Viewed 100.5k times · Source

I have a standard Rails3 environment, RVM 1.2.9, Rails 3.0.5, Ruby 1.9.2p180, MySQL2 Gem 0.2.7, mysql-5.5.10-osx10.6-x86_64

Error I get when running rake db:migrate to create database is:

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

config/database.yml has

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx

sure it's something simple I'm missing.

Answer

Tobias Cohen picture Tobias Cohen · Jun 20, 2011

First, to find your socket file:

mysqladmin variables | grep socket

For me, this gives:

| socket                                            | /tmp/mysql.sock                                                                                                        |

Then, add a line to your config/database.yml:

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /tmp/mysql.sock