Rails Socket Config (Can't connect to local MySQL server through socket)

31 Jul 2006

(Note: this article might be better named “the most complicated database.yml file possible”)

Have you ever accidentally added your database.yml file to subversion or moved a database.yml file to a computer where it doesn’t belong? How about moving your dev server from your laptop to your desktop to your school’s web host to some new install of Suse you’re playing with?

Most of the time this is a big headache. You get errors like the one in the title where the socket was not found. Different flavors of *nix will put the default mysql socket in different places on the machine and it can be a pain to try to keep up with things.

Well, here’s a a database.yml file that can relieve you of ever having to remember which machine you’re on. Many thanks to LazyAtom go gave me the idea for this (who, in turn, got the idea from James Duncan Davidson

Behold - The Code:

    login: &login
      adapter:  mysql
      username: user
      password: 
      socket:  <%=
        ['/opt/local/var/run/mysql5/mysqld.sock', # darwinports
         '/opt/local/var/run/mysqld/mysqld.sock', # darwinports, again
         '/var/run/mysqld/mysqld.sock',           # ubuntu/debian
         '/tmp/mysql.sock'].select { |f| File.exist? f }.first %>

    development:
      <<: *login
      database: rails_development

    test:
      <<: *login
      database: rails_test

    production:
      <<: *login
      database: rails

Ta-Da. I just installed this on my dev box and things are working great already.

For those of you who run a staging server to test your app in production, you might want to check out the following setup (I’m seriously considering a staging server setup for my apps. Also, this is good if you’re absolutely dead-set on using the same database.yml file for multiple hosts and multiple configurations (just as long as you know the hostname).

    <% host = `uname -n`.strip %>
    user: &user
      <% if host.eql? 'staging' %>
      username: staging_user
      password: staging_pass
      <% else %>
      username: user
      password: pass
      <% end %>

    login: &login
      <<: *user
      adapter:  mysql
      socket:  <%=
        ['/opt/local/var/run/mysql5/mysqld.sock', # darwinports
         '/opt/local/var/run/mysqld/mysqld.sock', # darwinports, again
         '/var/run/mysqld/mysqld.sock',           # ubuntu/debian
         '/tmp/mysql.sock']
         .select { |f| File.exist? f }.first %>

    development:
      <<: *login
      database: rails_development

    test:
      <<: *login
      database: rails_test

    production:
      <<: *login
      database: rails

If you want to partition the login info for several different users on a large collaborative project you may want to have a special file (ignored by SVN) that holds just the login info for a given user:

    login: &login
      adapter:  mysql
      username: user
      password: 
      socket:  <%=
        ['/opt/local/var/run/mysql5/mysqld.sock', # darwinports
         '/opt/local/var/run/mysqld/mysqld.sock', # darwinports, again
         '/var/run/mysqld/mysqld.sock',           # ubuntu/debian
         '/tmp/mysql.sock'].select { |f| File.exist? f }.first %>


    # replace the above login details with information from ./config/dblogin.yml
    <%= file = File.join(RAILS_ROOT, "config", "dblogin.yml")
        IO.read(file) if File.exist?(file) %>

    development:
      <<: *login
      database: rails_development

    test:
      <<: *login
      database: rails_test

    production:
      <<: *login
      database: rails
  • Sergio Jardim said: You saved my life man!! I've googling this solution for 2 nights. Thanks a lot dude!
  • Danger said: testing my own comments. Man, switching from Typo to Mephisto was worthwhile but HARD on Dreamhost.
  • gm kevin said: orshabal chavez infernatil verminolf hell demon ferunbras apocalypse ciclop demon hero hidra Dipthrah Ghazbaran Morgaroth Spirit of Fire Fury Warlock Black Knight Behemoth Frost Dragon Dragon Lord Undead Dragon Pirate Ghost Pirate Corsair Juggernaut The Obliverator
  • daiiing said: Thank you very much!!!
  • dara said: nice solution
  • hagope said: @Danger => did you have this 'can't ... mysql.sock' problem on Dreamhost? I am having this problem with Capistrano, it cannot execute the rake db:migrate steps...
  • Tony Mann said: Finally, a robust solution! Thanks so much.

Please if you found this post helpful or have questions.