database configuration does not specify adapter

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


I'm getting this error when I'm trying to connect to a mysql database. The problem is that the application works for weeks, and then randomly I get this message. When I get this error message the application is not able to reconnect to the database until I restart it.

I'm using a configuration file to connect to the database, and the adapter is specified...the database configuration is not generated at runtime.

Do you have any idea on what is going on?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

when I tried to run a command line script (let's say 'my_script' here), the same error happened. The reasons were:

  1. There is only production environment there.
  2. I missed to set RAILS_ENV for the command line.

So, the following is the solution in my case:

> $ RAILS_ENV=production my_script

Solution 2 - Ruby on-Rails

I just had this problem, and it was caused by a typo in my configration.yml.

I originally had this:

production:
  adapter:mysql

When I meant to have this:

production:
  adapter: mysql

That one little space between adapter: and mysql makes the difference.

Solution 3 - Ruby on-Rails

Another possible cause:

In Rails 3.2.x, establish_connection has a default argument set from the environment:

From connection_specification.rb:

def self.establish_connection(spec = ENV["DATABASE_URL"])
  resolver = ConnectionSpecification::Resolver.new spec, configurations
  spec = resolver.spec

The way ConnectionSpecification::Resolver works depends on ENV['DATABASE_URL'] giving a nil if not set. (Normally, it would be something like 'postgres://...').

So, if you happen to have misconfigured DATABASE_URL such that ENV['DATABASE_URL'] == '', that will give you database configuration does not specify adapter.

Solution 4 - Ruby on-Rails

I had this error when I mistakenly started rails server with

> sudo rails s -e "Production" -p 80

and I should have started rails with

> sudo rails s -e "production" -p 80

Solution 5 - Ruby on-Rails

I found another thing that can cause this problem: "mixing in" another YAML node using & and *.

I was originally doing something like the following to facilitate local, per-develop, Git-ignored config files:

http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams

But, after some debugging, I came to find out that establish_connection was for some reason being called with only the mixed-in key-value pairs and not the main ones. I.e. adapter, host, and database were not being passed in. I have no idea why, and this used to work for me.

Anyhow, instead of mixing in another YAML node, I now put the entire development and test hashes in the local config file, and establish_connection is once again being called correctly.

Solution 6 - Ruby on-Rails

For me, this command resolved the issue.

rake db:migrate RAILS_ENV=production

Solution 7 - Ruby on-Rails

I've found a couple of clues that this might be related to older library (ActiveRecord) or gem versions. For example, problems with fixtures even though rest of app seems okay (after an upgrade) or this trac ticket, which "stops gems from requiring an adapter from an old Active Record gem". Both of these are old, though, but it might be worth making sure your gems are up to date (if possible).

Are you using the native rails MySQL adapter by any chance? This is now deprecated under rails, but it's conceivable it's still limping along.

I've taken a very quick look at connection_specification.rb, too, which is where this error is coming from, and my best guess is that a reconnect is failing... but why (since it was obviously okay when you first started the app)? Are you doing something wild like calling ActiveRecord::Base.establish_connection in your application controller (or elsewhere)?

Or perhaps something like: script runner is called from cron in the dead of night when the connection has dropped. Unfortunately, the runner is invoked with an incorrect RAILS_ENV. Thus the wrong stanza is read from database.yml, and that stanza contains an invalid adapter:?

Solution 8 - Ruby on-Rails

I got the same error, by typing in the following command:

db:migrate RAILS_ENV=product

Should've been:

db:migrate RAILS_ENV=production

Solution 9 - Ruby on-Rails

If you get this error while deploying with Capistrano. Make sure you are setting the correct RAILS_ENV via

set :rails_env, 'production'

For example I was not explicitly setting the Rails environment in for the Capistrano staging deployment configuration. And thus Capistrano used 'staging' as the RAILS_ENV, resulting in the above error. Setting it to production like above in the staging.rb file solved the issue.

Solution 10 - Ruby on-Rails

Do remember that RAILS_ENV=staging will look for a staging specification in your database.yml just as setting RAILS_ENV=production will look for a production specification in database.yml file.

Provide database configs, as shown below, for every rails environment you target using, for example

bundle exec cap staging deploy

production:
  adapter: mysql2
  encoding: utf8
  database: rails
  username: rails
  password: pass
  host: 127.0.0.1
  port: 3306
  pool: 5
  timeout: 5000

staging:
  adapter: mysql2
  encoding: utf8
  database: rails
  username: rails
  password: pass
  host: 127.0.0.1
  port: 3306
  pool: 5
  timeout: 5000

Solution 11 - Ruby on-Rails

Remember to use the C-Based ruby gem for mysql. The ruby-based is unstable for production.

Try installing the gem

gem install mysql

Remember to copy libmySQL.dll into the ruby bin directory.

Solution 12 - Ruby on-Rails

I had this error with another problem; I had specified 'development' twice, and 'test' not at all.

Solution 13 - Ruby on-Rails

There are alot of bad tutorials out there on the internet that show yaml files like so:

development:
encoding: utf
database: dbname
...etc

YAML files are case sensitive and require TWO SPACES for the inner contents of each given db-type attribute. Like so:

development:
  encoding: utf
  database: dbname
  ...etc

UPDATE: I got this error again today. My VPS server had installed Rails 3.2.8 when my app was running Rails 3.2.6.

Definitely check your Gemfile and your database.yml file (of course). The problem here is clearly stated---Rails is not communicating with your database specifically due to an adapter (aka gem)

Solution 14 - Ruby on-Rails

We had this issue with one of our older apps. Someone had created a boutique named environment, that even if RAIL_ENV was set to production, it was looking for a database configuration called legacy_<RAIL_ENV>, so I had to make a database environment called legacy_production for this app to work.

If you are maintain someone else's app, I would look for a copy of this app's database.yml that is working, perhaps it has some oddly named configuration. You can search your codebase for establish_connection to see if it is defining some strange variant.

Solution 15 - Ruby on-Rails

I met this problem due to the 'multiple database support issue'. In my app/model folder, there is a file defined a redundant database connection:

class CacheCleanerActiveRecord < ActiveRecord::Base
  establish_connection "cache_cleaner_#{Rails.env}"
  self.abstract_class = true
end

but this database is not found in my database.yml ( because it's not used at all ).

so the solution is quit simple: remove this file and everything is fine !

Solution 16 - Ruby on-Rails

You may have an error like:

RAILS_ENV= test

A space after the equals sign is not allowed, change it to:

RAILS_ENV=test

Solution 17 - Ruby on-Rails

This happens to me, finally I found that RAILS_ENV is case sensitive, in my enviroment I set RAILS_ENV=DEVELOPMENT , which is wrong, the value of RAILS_ENV must be lowercase.

$ RAILS_ENV=DEVELOPMENT rails server webrick
=> Booting WEBrick
=> Rails 4.2.5 application starting in DEVELOPMENT on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test environment)
  * production - set it to true

Exiting
/home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_c
onnection': 'DEVELOPMENT' database is not configured. Available: ["default", "development", "test", "production"] (ActiveRecord::AdapterNotSpecified)
        from /home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:211:in `res
olve_connection'



$ RAILS_ENV=development rails server webrick
RubyDep: WARNING: Your Ruby is outdated/buggy. (To disable warnings, set RUBY_DEP_GEM_SILENCE_WARNINGS=1)
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: install 2.3.1.
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-07-20 16:41:09] INFO  WEBrick 1.3.1
[2016-07-20 16:41:09] INFO  ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-07-20 16:41:09] INFO  WEBrick::HTTPServer#start: pid=19881 port=3000

Solution 18 - Ruby on-Rails

rails -e "production" is okay

only rails -e production returns error

database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

Solution 19 - Ruby on-Rails

call rake assets:precompile:all

Solution 20 - Ruby on-Rails

This is probably not the most likely issue to cause this error, but here it is just in case.

My problem was that I was building the database settings in a Hash using symbols as keys and then serializing it with #to_yaml to database.yaml. ActiveRecord expects the environment names to be Strings, not Symbols, so it wasn't picking up the database settings when reading the generated file. I fixed it by using string keys in the hash.

Solution 21 - Ruby on-Rails

For Rails4, commenting the line fetch(:default_env).merge!(rails_env: 'production') in production.rb and adding set :rails_env, :production fixed it.

Solution 22 - Ruby on-Rails

You need to specify the environment when running the server or command as your database.yml file may have only production adapter while simply runnig rake db:migrate for example will take environment variable as development.

Solution 23 - Ruby on-Rails

Just for the sake of completeness, I just got this error because I natively created a parametrised Rails runner script that takes an email address, and named the command line option -e -- which of course is the one the Rails runner uses for the environment. So it was trying to find an environment configuration that matched the email address!

Luckily, just before the ActiveRecord error mentioned in the title, it gave me an error message that helped me twig what the problem actually was:

You did not specify how you would like Rails to report deprecation notices for your test@example.com environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/test@example.com.rb

Solution 24 - Ruby on-Rails

Check the spelling of adapter I had adaptor and got this error.

Solution 25 - Ruby on-Rails

I had this error message when upgrading from Rails 4 to 5. I was calling

establish_connection "myconnection"

where "myconnection" is a valid key in my database.yml. However, passing this parameter as a string is apparently no longer supported. Using a symbol instead got rid of the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertoView Question on Stackoverflow
Solution 1 - Ruby on-RailsFumisky WellsView Answer on Stackoverflow
Solution 2 - Ruby on-Railsdjacobs7View Answer on Stackoverflow
Solution 3 - Ruby on-RailsBenjamin OakesView Answer on Stackoverflow
Solution 4 - Ruby on-RailsandrewView Answer on Stackoverflow
Solution 5 - Ruby on-Railsrlkw1024View Answer on Stackoverflow
Solution 6 - Ruby on-RailsSenGView Answer on Stackoverflow
Solution 7 - Ruby on-RailsMartin CarpenterView Answer on Stackoverflow
Solution 8 - Ruby on-RailsEternal21View Answer on Stackoverflow
Solution 9 - Ruby on-RailsthekindofmeView Answer on Stackoverflow
Solution 10 - Ruby on-RailsJones AgyemangView Answer on Stackoverflow
Solution 11 - Ruby on-RailsRicardo AcrasView Answer on Stackoverflow
Solution 12 - Ruby on-RailsLorenView Answer on Stackoverflow
Solution 13 - Ruby on-Railsboulder_rubyView Answer on Stackoverflow
Solution 14 - Ruby on-RailsPeter DietzView Answer on Stackoverflow
Solution 15 - Ruby on-RailsSiweiView Answer on Stackoverflow
Solution 16 - Ruby on-RailsDavid tsangView Answer on Stackoverflow
Solution 17 - Ruby on-RailsfangxingView Answer on Stackoverflow
Solution 18 - Ruby on-RailsMuflixView Answer on Stackoverflow
Solution 19 - Ruby on-RailsgrosserView Answer on Stackoverflow
Solution 20 - Ruby on-RailsJenner La FaveView Answer on Stackoverflow
Solution 21 - Ruby on-RailsPratik KhadloyaView Answer on Stackoverflow
Solution 22 - Ruby on-RailsAsim MushtaqView Answer on Stackoverflow
Solution 23 - Ruby on-RailsEmma BurrowsView Answer on Stackoverflow
Solution 24 - Ruby on-RailsdepassionView Answer on Stackoverflow
Solution 25 - Ruby on-RailsJong Bor LeeView Answer on Stackoverflow