Posted on July 7, 2007 at 8:32 am

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

So you installed Ubuntu, got all excited about developing your Rails application on it, and then…

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

No matter what you do, database connection doesn’t work. You reinstall Rails (of course you installed it via “sudo apt-get rails”, right??), reinstall MySql, recreate the database schema, change root’s password, install Kubuntu instead of Ubuntu… But it doesn’t work.

The reason for this error is quite simple, really: somewhere along Ruby’s Mysql driver, mysql socket is expected to exist at /tmp/mysql.sock. But that’s not where it is in Ubuntu. If you take some time searching, you’ll notice that the .sock file is actually on /var/run/mysqld - and it’s called mysqld.sock instead.

In fact, if you Google it, there is a closed bug entry on Rails’ tracking system regarding that problem, and the suggested solution there is to change your database.yml to add a link to the correct socket. Something like:

production:
  adapter: mysql
  socket: /var/run/mysqld/mysqld.sock

Which is obviously not a good idea, since you’ll end up creating new projects, moving to a different OS or whatever - and everything will break again.

So I tried a small patchwork to fool mysql’s driver, and then it works nicely:

sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

That way you will actually HAVE a /tmp/mysql.sock file, as expected by mysql driver, and everything will connect just fine. Just like it’s meant to be. Amen!

Tags:, , , ,

14 Responses to “No such file or directory - /tmp/mysql.sock”

  1. Axel on July 11th, 2007 at 3:17 am says:

    Dude ! Thx a lot for this post - had some trouble with rails and ubuntu befor, because I did the ‘gems’ - way. But anyway - this here saved some time for me I guess !

    cheers

  2. James on July 14th, 2007 at 7:29 pm says:

    Thanks for this - I had been scratching my head for a while trying to work out what it was complaining about

  3. Opeq on August 15th, 2007 at 3:53 am says:

    You made my day! thanks man!

  4. Glen on October 2nd, 2007 at 6:06 am says:

    Fixed my problem immediately–Thanks!

  5. Tony Whitmore on January 4th, 2008 at 11:29 am says:

    The downside is that the tmpfs in /tmp is cleared on a reboot of an Ubuntu system. You could add your “ln” line to a system boot script to create it on boot though.

  6. Kyle K on January 18th, 2008 at 6:11 pm says:

    Awesome tip. Talk about a KISS solution. I’m not even using Rails so I don’t have a database.yml!

  7. Ryan Mulligan on February 19th, 2008 at 2:16 pm says:

    The real answer to this problem is to install libmysql-ruby which lets ruby understand mysql better. Then you can use TCP like this

    login: &login
    adapter: mysql
    host: localhost
    username: #{user}
    password: #{password

    development:
    database: #{application}_dev

  8. Chris on May 19th, 2008 at 6:23 am says:

    Bloody handy tip that solved a problem I wasn’t looking forward to battling at 2:25am, thanks!

  9. dawolf on May 20th, 2008 at 11:57 am says:

    Thanks Ryan for the “apt-get install libmysql-ruby” tip!

  10. Simon Elliott on June 7th, 2008 at 7:43 am says:

    This is a great fix, I really appreciate you posting this it saved me whole bunch of pain. Thankyou

  11. Germán Carrillo on July 17th, 2008 at 2:23 pm says:

    Thanks, I did find a lot and nothing works but your solution did!

  12. Yury on August 13th, 2008 at 8:45 am says:

    No above solutions helped me. But after
    sudo gem install mysql every thing is fine

  13. Tejaswi sharma on August 28th, 2008 at 4:34 am says:

    I love you man. You really saved my life today thanks a lot.

  14. torrnova on October 23rd, 2008 at 8:46 pm says:

    thanx a lot for the smart solutions for both herval & ryan …

Leave a Reply