Tuesday, February 13, 2007

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: 127.0.0.1 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,
--Hardy

5 comments:

André Miani said...

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.

Hardy said...

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

ndgiang84 said...

same problem here. in /config/database.yml just change the host: localhost to host: 127.0.0.1

then my problem solved. hope this helps.

Anonymous said...

Thank you for taking the time to post this solution! Saved me a bunch of time.

Ace Suares said...

Life Saver! Thx!