Chris
Chris

Reputation: 5073

Why am I getting this error when mysql2 is required in production only?

My gemfile has this line:

gem 'mysql2', group: :production

but I get this error when I run the command from the command line: rails r "puts Rails.env"

/Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/activerecord-4.2.6/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /Users/cgunnels/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/cgunnels/.rvm/gems/ruby-2.2.2@global/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
...
rest of stack trace

On a side note: I haven't been able to figure out why mysql2 won't install. I've tried a couple fixes per a stackoverflow search but no dice.

UPDATE: I didn't mention, but I want to be in development environment.

Upvotes: 0

Views: 42

Answers (2)

spickermann
spickermann

Reputation: 107077

When you run rails r without telling which environment to then Rails will use the development environment as a fallback.

You have three options:

  1. Start Rails in the production environment

    bundle exec rails runner -e production "puts Rails.env"
    

    or

  2. Install the mysql2 gem in all environment by removing the , group: :production from the gem 'mysql2' line in your Gemfile.

    or

  3. Update your config/database.yml to use sqlite3 instead of mysql2 in the development environment.

Upvotes: 2

Marlin Pierce
Marlin Pierce

Reputation: 10089

You are probably getting the error exactly because when you are not in production there is no database driver.

Upvotes: 0

Related Questions