延迟作业 - rake jobs:clear 导致“无法通过套接字连接到本地 MySQL 服务器 '/tmp/mysql.sock' (2)

Delayed Job - rake jobs:clear causes "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

我的 database.yml 文件:

production:
  adapter: mysql2
  database: app_production
  host: 127.0.0.1
  username: root
  password: 12345678
  port: 3306
  pool: 15
  timeout: 5000
  socket: /var/run/mysqld/mysqld.sock

(拔掉插座线也无济于事)

根据my.cnf的路径:

/var/run/mysqld/mysqld.sock

Mysql 是 运行ning 因为我的应用程序已启动,但是当我 运行:

 rake jobs:clear

从控制台,我得到:

rake aborted!
Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:567:in `retrieve_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:477:in `delete_all'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:8:in `delete_all'
/home/deploy/dispatch/shared/bundle/ruby/2.2.0/gems/delayed_job-4.0.6/lib/delayed/tasks.rb:4:in `block (2 levels) in <top (required)>'
Tasks: TOP => jobs:clear

延迟作业将采用与您的应用程序 运行 所在的 rails 环境关联的数据库配置。

如果您只是 运行 rake jobs:clear,它会在开发模式下默认 运行 并尝试连接到您的开发数据库。如果你想 delayed_job 连接到你的生产数据库,你需要相应地设置环境,即 production:

RAILS_ENV=production rake jobs:clear

顺便说一下,在 database.yml 中保留 socket 行,否则它将默认为 /tmp/mysql.sock