简体   繁体   中英

Rails and MySQL - ActiveRecord::ConnectionNotEstablished in production environment

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'

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?

Thanks for your help


Posting my Gemfile: 发布我的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.

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.

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.

you have to create a mysql database first. and then go to your application directory and edit database.yml file (~/config/database.yml) with your database information.

like:

development:
adapter: msql2
database: your database path
username: mysql username
password: mysql password
pool: 5
timeout: 5000

You've probably already moved on with this but in case someone else gets to this page, here are a couple of things to try.

Are you sure there are no typos when you try to launch the server in production mode?

bundle exec rails s --environment=production

If there is a typo in "production" rails will not complain when it tries to start the server but it will try to look for that typo in the database.yml file and since it does not exist you will get the error you mentioned

Another thing to try: Check whether you database.yml file is formatted correctly. YAML is really picky with tabs and spaces so double check by doing the following in your console

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

If you get a hash back then your file is fine, otherwise fix the problem that was returned

i had the same issue. Spree latest version is still not compatible with rails 3.2.0/3.2.1 so use the rails 3.1.3 and the latest spree version

In my case, rails version was 3.2.0 and spree default version was 0.40.0. i changed spree version to 1.0.0.rc3 and that shown me rails compatibility issue, so i changed rails version to 3.1.3 and all went fine.

You can use the latest spree stable version i guess its 0.70.3.

I encountered a similar problem in a Rails engine gem, and eventually tracked it down to a scope declaration (eg: scope :owing, where...) on one of the classes. Commenting that out resolved the ActiveRecord::ConnectionNotEstablished but resulted in a failing spec (obviously).

The engine is using the Combustion gem to include a minified rails app for specs, and the problem occurred regardless of db type. Hope that helps in some way.

Edit: Here's a rails issue for it .

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