No such file or directory - /tmp/mysql.sock

Here comes a maybe not so high level bit, but it nevertheless cost me a lost of time the other day to figure out what was going on.

Long story short - I am recently experimenting with ruby on rails and and kept getting the following error message each single time I wanted to scaffold any of my database tables:

No such file or directory - /tmp/mysql.sock

I am running Windows XP together with cygwin. Ruby is installed via cygwin and MySQL is installed via the MySQL windows installer.

It was pretty obvious ruby could not connect to my database, but I could not figure out why. The file mysql.sock did not exist on my system and I could not figure out how to get ruby to access the database via tcp/ip. Some people suggested adding port: 3306 to database.yml , but that did not change anything.

Then finally the breakthrough. I changed host: localhost in database.yml to host: and everything worked like a charm. It turns out that when specifying localhost ruby tries to connect via a socket file whereas specifying a IP address switches to tcp/ip. Interesting.

So long,

Old comments

2007-02-17André Miani
I'm using linux and got the same error but in my case I solved cleaning the username and password field name in config/database.yml, logged as root ... if its bad I dont know but its works.
2007-02-17Hardy Ferentschik
I found this on in the mysql documentation:

Note that if you specify localhost as a hostname, mysqladmin defaults to using a Unix socket file connection rather than TCP/IP. From MySQL 4.1 onward, you can explicitly specify the connection protocol to use by using the --protocol={TCP|SOCKET|PIPE|MEMORY} option.

See Running Multiple Servers on Unix
same problem here. in /config/database.yml just change the host: localhost to host:

then my problem solved. hope this helps.
Thank you for taking the time to post this solution! Saved me a bunch of time.
2010-12-07Ace Suares
Life Saver! Thx!
2012-08-17Robert Malvern
I could have spend days trying to work this out!