Hoopla!

now with extra whiz-bang!

Hoopla!

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

July 31, 2006 · 2 comments

(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

→ 2 comments Tags:

uninitialized constant Admin::GeneralController

April 10, 2006 · 0 comments

I just updated my typo to revision 1004 and, while almost all of my site is accessible, going to /admin barfs out a 500 error.

From my log:
1
2
3
4
5
6
7
8
9
10
11
12
13
NameError (uninitialized constant Admin::GeneralController):
    /vendor/rails/activerecord/lib///activesupport/lib/active_support/dependencies.rb:100:in `const_missing'
    generated/routing/recognition.rb:9:in `recognize_path'
    /vendor/rails/actionpack/lib/action_controller/routing.rb:477:in `recognize!'
    /vendor/rails/railties/lib/dispatcher.rb:38:in `dispatch'
    /vendor/rails/railties/lib/fcgi_handler.rb:150:in `process_request'
    /vendor/rails/railties/lib/fcgi_handler.rb:54:in `process!'
    /vendor/rails/railties/lib/fcgi_handler.rb:53:in `each_cgi'
    /usr/lib/ruby/1.8/fcgi.rb:597:in `each'
    /usr/lib/ruby/1.8/fcgi.rb:597:in `each_cgi'
    /vendor/rails/railties/lib/fcgi_handler.rb:53:in `process!'
    /vendor/rails/railties/lib/fcgi_handler.rb:23:in `process!'
    dispatch.fcgi:34

Going straight to /admin/content/new can get me to where I can post – so this might just be a typo somewhere.

→ 0 comments Tags: