Focus on IT Recommend

Home > ruby - mysql2 gem not loaded (for manual code, not database adapter) in Rails 3.2.16 (Ubuntu)

ruby - mysql2 gem not loaded (for manual code, not database adapter) in Rails 3.2.16 (Ubuntu)



I'm running Rails app the application on PostgreSQL but needs to execute some arbitrary MySQL query so I included gem mysql2 into Gemfile and bundle install and all that.

The code works great (connected to MySQL database and execute query) on localhost (OS X). But when I deploy to the server (Ubuntu) with RVM, Rails doesn't load the gem and gives me this error:

NameError (uninitialized constant Mysql2)

which basically means mysql2 is not loaded. And doing puts Gem.loaded_specs.keys in the code confirm this. Strangely when I do rails console the mysql2 gem is loaded just fine.

My suspection is that mysql2 is one of the gems used by Rails for the database adapters, so if the current database adapter is not set to mysql2 then Rails conveniently ignore that. But I don't know enough of Rails codebase to verify my suspection.

More spec details:

Recommend:ruby - Rails: Specified 'mysql2' for database adapter but the gem is not loaded

ose commands given in the solution. I am still getting the same error for some reason. I run gem install mysql2 -v 0.3.18 followed by bundle install on my terminal. And than I start the server by typing rails server Upon opening the localho

  • Rails 3.2.16
  • RVM 1.25.27
  • Ubuntu 13.04
  • Ruby 2.1.2

The mysql2 gem is corrected added to the global section of Gemfile, not just limiting to development tag.

Part of my Gemfile:

source ''
gem 'rails', '3.2.16'
gem 'pg'
gem 'mysql2'
ruby-on-rails ruby rubygems rvm
  this question
edited Jun 9 '14 at 16:28 asked Jun 9 '14 at 15:30 huy 1,478 5 19 37      Only to confirm, your gemfile looks like group :production do gem 'mysql' end and do you specified in your config/database.yml? production: adapter: mysql2 –  jandresrodriguez Jun 9 '14 at 15:36      I think you misunderstood my question. I use postgresql as adapter, not mysql. I just want to include the gem (just like any other gem) to use its functionality (i.e connect to MySQL database) –  huy Jun 9 '14 at 15:38      Please update your question with your gemfile. Have you run bundle install on the server as well? –  BroiSatse Jun 9 '14 at 16:01      Why do you need to use MySQL? In any case, my guess is that Mysql2 is not the constant you're after. –  Alex Lynham Jun 9 '14 at 16:28      @BroiSatse I updated my Gemfile. –  huy Jun 9 '14 at 16:30  |  show more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Recommend:ruby on rails - Gem::LoadError Specified 'mysql2' for database adapter, but the gem is not loaded

0' gem 'mysql2' database.yml is development: adapter: mysql2 encoding: utf8 database: #new_app_development pool: 5 username: root password: password host: localhost gem list gives mysql2 (0.4.0) but still I'm getting Gem::LoadError Specifie


------splitte line----------------------------