无法打开rails console:生成数据库未配置,establish_connection引发ActiveRecord :: AdapterNotSpecified

Nat*_*han 15 mysql console activerecord ruby-on-rails

我的应用程序在生产中非常完美 - 至少是网站的一部分.当我通过SSH连接到我的VPS并执行"rails c RAILS_ENV = production"时,问题才会出现.控制台在开发模式下工作正常.

我之前遇到过这个问题(或者至少有一个看起来像这个问题)并通过向database.yml添加"reconnect:true"来解决这个问题 - 但这次没有修复它.这是错误输出的开始:

/home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:62:in `establish_connection': RAILS_ENV=production database is not configured (ActiveRecord::AdapterNotSpecified)
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:59:in `block (2 levels) in <class:Railtie>'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/railtie.rb:57:in `block in <class:Railtie>'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:25:in `instance_exec'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:25:in `run'
from /home/tour/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/initializable.rb:50:in `block in run_initializers'
Run Code Online (Sandbox Code Playgroud)

对于我的生产数据库设置:

production:
  adapter: mysql
  encoding: utf8
  database: tour_production
  pool: 5
  username: [user]
  password: [password]
  socket: /var/run/mysqld/mysqld.sock
  reconnect: true
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么它通过乘客工作,但没有运气控制台.无论如何,我使用的是Rails 3,Ruby 1.9.2,Passenger和Nginx.

我一直在通过Runner做一些小事,这很烦人.我想解决这个问题.所以,谢谢你的帮助.

Sha*_*ell 51

使用控制台时,将环境指定为第一个参数而不是环境变量.所以:

ruby script/console production
Run Code Online (Sandbox Code Playgroud)

将加载生产环境.

您的错误消息是因为它正在寻找环境RAILS_ENV=production而不仅仅是production.


YWC*_*llo 42

最新的Rails 3方式很简单:

bundle exec rails console -e production
Run Code Online (Sandbox Code Playgroud)

  • 实际上,它更像是:捆绑exec rails控制台生产 (6认同)

Zub*_*bin 17

@Shadwell是正确的,但是如果你使用了一致的语法,你可以RAILS_ENV在开头设置如下:

RAILS_ENV=production rails console
Run Code Online (Sandbox Code Playgroud)