简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM