Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 293
Jamie Buchanan Avatar asked Mar 31 '11 11:03

Jamie Buchanan


People also ask

Can't connect to local MySQL server through socket '/ tmp my?

It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ . There are a couple of solutions for this error. Then try to connect again.

Can't connect to local MySQL server through socket '/?

Another possible solution to the >can't connect to local mysql server through socket> error message is to try and connect to the MySQL using the 127.0. 0.1 ip address instead of localhost. When you use localhost to connect to MySQL, the operating system uses the socket connector.


2 Answers

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 
like image 171
Tobias Cohen Avatar answered Oct 14 '22 16:10

Tobias Cohen


Found it!

Change host: localhost in config/database.yml to host: 127.0.0.1 to make rails connect over TCP/IP instead of local socket.

development:   adapter: mysql2   host: 127.0.0.1   username: root   password: xxxx   database: xxxx 
like image 27
Jamie Buchanan Avatar answered Oct 14 '22 17:10

Jamie Buchanan