mysql - 延迟作业 - rake 作业 :clear causes "Can' t connect to local MySQL server through socket '/tmp/mysql.sock' (2)

标签 mysql ruby-on-rails ruby-on-rails-4 delayed-job

我的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

(移除 socket 线没有帮助)

根据 my.cnf 的路径:

/var/run/mysqld/mysqld.sock

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

 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连接到你的生产数据库,你需要相应地设置环境,即生产:

RAILS_ENV=production rake jobs:clear

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

关于mysql - 延迟作业 - rake 作业 :clear causes "Can' t connect to local MySQL server through socket '/tmp/mysql.sock' (2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014408/

相关文章:

MySql 错误 2002

mysql - 比较 2 个表并找到不重复的条目

mysql - 是否可以根据输入参数之一来决定 SQLWhere 子句的条件?

ruby-on-rails - rails 4 : Error when installing tiny_tds gem?

ruby-on-rails - ubuntu 11.04 上的 RVM/Rails 安装错误

postgresql - 本地机器上的 Postgres : rake db:migrate produces an error

ruby-on-rails - Rails 4 相当于 Rails 3 'Model.all'

php - 表单中的值未正确显示在数据库中

ruby-on-rails - 仅从现有模型/ Controller 生成测试

ruby-on-rails - 在我的 rspec 中为 #<RSpec::ExampleGroups 获取未定义的方法 `get'