Rails 和 MySQL - ActiveRecord::ConnectionNotEstablished 在生产环境中 [英] Rails and MySQL - ActiveRecord::ConnectionNotEstablished in production environment

查看:35
本文介绍了Rails 和 MySQL - ActiveRecord::ConnectionNotEstablished 在生产环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量简短:
对数据库使用 Rails 3.2.1mysql2;在 Ubuntu 11.10 上运行
开发环境中一切正常
当我在 production 中启动我的服务器 (WEBrick) 时,它可以正常启动,但是在加载任何页面后,它会引发以下错误:

I'll try to be short:
Using Rails 3.2.1 and mysql2 for databases; running on Ubuntu 11.10
In development environment everything works just fine
When I start my server (WEBrick) in production, it boots up fine, but after loading any page, it raises the following error:

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in
retrieve_connection'
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in
`retrieve_connection'
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in
`connection'
...
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in
`service'
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block
in start_thread'

在尝试找到任何可能的错误来源,并咨询了十亿个不同的网站和类似的报告后,我仍然无法找到问题所在.我的 database.yml(现在在 localhost 上运行,并且我在生产环境中的设置与开发环境相同,可以正常工作)很好,我的 Gemfile 没问题(gem'mysql2' 包括在内,不用担心),生产数据库已创建并且 rake db:migrate 迁移它没有问题......我没有想到或在互联网上没有找到任何其他可能的错误来源?

After trying to find any possible source of error, and consulting like a billion different websites and similar reports, I am still not able to find what's wrong. My database.yml (running on localhost right now, and have my settings in production environment the same as for development, which works) is fine, my Gemfile is alright (gem 'mysql2' included, don't worry), the production database is created and rake db:migrate had no problems migrating it... Any other possible source of error I did not think of or did not find on the internet?

我强调,这只发生在生产中

感谢您的帮助

编辑 发布我的 Gemfile:

EDIT Posting my Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.1'

gem 'mysql2'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'rufus-scheduler'
gem 'gmaps4rails'

希望这会有所帮助 - 虽然我并没有发现它有什么问题.

Hope this helps - although I don't really see anything wrong with it.

编辑两个
奇怪的是,我在生产环境中启动 rails 控制台时没有问题 - MySQL 查询工作,模型实例可以毫无问题地保存.当我尝试从浏览器访问数据库时,我只会收到上述错误.

EDIT TWO
Oddly enough, I have no problems when I start the rails console in the production environment - MySQL queries work, model instances can be saved without problems. I only get the aforementioned error when I try to access the database from the browser.

推荐答案

对我来说,这归结为一个错误配置的 database.yml 文件.要确认格式正确,您可以从 .yml 文件的存储目录执行以下操作:

For me it came down to a misconfigured database.yml file. To confirm the correct formatting you can do the following from the directory where the .yml file is stored:

irb 
require 'yaml'
a = YAML::load(File.open("database.yml"))

如果你有错误,你会看到类似这样的回应......

if you have an error you'll see something like this in response...

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> a = YAML::load(File.open("database.yml"))
Psych::SyntaxError: (<unknown>): could not find expected ':' while scanning a simple key at line 17 column 3
        from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in `parse'
        from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in `parse_stream'
        from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:in `parse'
        from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:in `load'
        from (irb):2
        from C:/Ruby193/bin/irb:12:in `<main>'

就我而言,我在输入数据库密码时忘记在:"后留一个空格.

In my case, I forgot to leave a space after the ":" when entering the database password.

这篇关于Rails 和 MySQL - ActiveRecord::ConnectionNotEstablished 在生产环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆