Tuesday, March 15, 2011

Ruby on Rails cannot connect to database from runner script

In my application I have a runner script running on schedule (crontab) that needs to connect to database and get some information. I get the following error when I try to run a query ( using Model.find(...) ) :

.../vendor/rails/railties/lib/commands/runner.rb:45: .../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:471:in `real_connect': Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (Mysql::Error)
From stackoverflow
  • Generally this sort of thing happens because of either paths (so you aren't seeing the right database.yml or something) or permissions (you aren't doing it as the right user).

    A pattern that generally works for me is to put a crontab entry like this:

    cd path/to/rails/app-root; script/runner MyController.thing_to_do
    

    in the crontab of the (pseudo)-user that the app runs as.

    Goro : Turns out my runner scripts were not running production environment by default. Forcing them to use the production database takes care of the problem.

0 comments:

Post a Comment