Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Mauris Alonso
Mauris Alonso
6,152 Points

mysql2 gem is not loading - MAC

Hello everyone,

I recently installed and updated Ruby on Rails on my Mac. Everything runs perfect until I type "localhost:3000" on my browser. For some reason is telling my mysql2 gem is not loaded, this is the error:

Gem::LoadError

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).

I am new to Rails and after browsing around for two hours looking for a solution, all I found was hundreds of people having the same problem, but no precise answer.

I will appreciate any help, and I thank any answer in advance.

Thanks!

Hi there,

Can you explain what steps you have take with you app. Are you following a course on Treehouse?

You've installed the Mac Ruby on Rails and started the web server, right? Do you get messages in the console to say how far the process gets?

In your Gemfile; do you have mysql2 installed as a line like gem 'mysql2'.

Steve.

14 Answers

Kevin Korte
Kevin Korte
28,149 Points

First, I assume you have gem 'mysql2' in your Gemfile in your rails root directory. Make sure you do. And you've ran bundle install.

If you still have this problem, I'd delete your Gemfile.lock (it'll be auto generated on the next bundle install). You can also try running bundle update mysql2

Try those. Which version of rails are you running?

Mauris Alonso
Mauris Alonso
6,152 Points

Yes, Gem mysql2 is installed. Bundle install & update were done. I deleted the gemfile.lock and I also did bundle updated mysql2.

Still the same. I have Rails 4.2.4, Ruby version 2.2.3p173.

Just in case this have something to do with it, my mysql server version is 5.6.26 Homebrew.

I am definitely missing something out, although I have followed and reviewed my installation steps like 10 times already. I can create rails files and everything, bundle works fine and all gems are set in Gemfile correctly, but I can not find a way around this problem when running the rails server.

I FIXED THIS!!!!!!!!!!!!!!!

I had the same problem. I searched all over the internet and havent found a solution. But i'm test and when I change gem 'mysql2' for gem 'mysql' on Gemfile...

YES! SOLVED

I dont know if this is right solution, but works. I was give up and try this! =) Happy!

Mauris Alonso
Mauris Alonso
6,152 Points

I still can't figure this out. Please forgive me is this is too much, but I like to post here the entire error page, so it can be reviewed a little better by the experts. Once again thanks a lot for any incoming help.


Gem::LoadError

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).

Extracted source (around line #177): 175 176 177 178 179 180

        require path_to_adapter
      rescue Gem::LoadError => e
        raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
      rescue LoadError => e
        raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
      end

Rails.root: /Users/malonso/Sites/simple_cms

Application Trace | Framework Trace | Full Trace activerecord (4.2.4) lib/active_record/connection_adapters/connection_specification.rb:177:in rescue in spec' activerecord (4.2.4) lib/active_record/connection_adapters/connection_specification.rb:174:inspec' activerecord (4.2.4) lib/active_record/connection_handling.rb:50:in establish_connection' activerecord (4.2.4) lib/active_record/railtie.rb:120:inblock (2 levels) in <class:Railtie>' activesupport (4.2.4) lib/active_support/lazy_load_hooks.rb:38:in instance_eval' activesupport (4.2.4) lib/active_support/lazy_load_hooks.rb:38:inexecute_hook' activesupport (4.2.4) lib/active_support/lazy_load_hooks.rb:45:in block in run_load_hooks' activesupport (4.2.4) lib/active_support/lazy_load_hooks.rb:44:ineach' activesupport (4.2.4) lib/active_support/lazy_load_hooks.rb:44:in run_load_hooks' activerecord (4.2.4) lib/active_record/base.rb:315:in<module:ActiveRecord>' activerecord (4.2.4) lib/active_record/base.rb:26:in <top (required)>' activerecord (4.2.4) lib/active_record/migration.rb:383:inconnection' activerecord (4.2.4) lib/active_record/migration.rb:370:in call' actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call' activesupport (4.2.4) lib/active_support/callbacks.rb:88:in __run_callbacks__' activesupport (4.2.4) lib/active_support/callbacks.rb:778:in_run_call_callbacks' activesupport (4.2.4) lib/active_support/callbacks.rb:81:in run_callbacks' actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:incall' actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in call' actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:incall' actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' web-console (2.2.1) lib/web_console/middleware.rb:39:incall' actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.2.4) lib/rails/rack/logger.rb:38:incall_app' railties (4.2.4) lib/rails/rack/logger.rb:20:in block in call' activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:inblock in tagged' activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:intagged' railties (4.2.4) lib/rails/rack/logger.rb:20:in call' actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:incall' rack (1.6.4) lib/rack/methodoverride.rb:22:in call' rack (1.6.4) lib/rack/runtime.rb:18:incall' activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in call' rack (1.6.4) lib/rack/lock.rb:17:incall' actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in call' rack (1.6.4) lib/rack/sendfile.rb:113:incall' railties (4.2.4) lib/rails/engine.rb:518:in call' railties (4.2.4) lib/rails/application.rb:165:incall' rack (1.6.4) lib/rack/lock.rb:17:in call' rack (1.6.4) lib/rack/content_length.rb:15:incall' rack (1.6.4) lib/rack/handler/webrick.rb:88:in service' /Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:inservice' /Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in run' /Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:inblock in start_thread'

I'm doing some Googling to see if I can find anything out there that might help you - I know how frustrating Rails can be when stuff just doesn't work!

One thing I found here talks about the Gemfile.lock file having too explicit a declaration for mysql2.

Have you got a github repo for your project? I don't think the error is capable of replication, but I can give it a go!

Mauris Alonso
Mauris Alonso
6,152 Points

I am still going crazy over the internet trying to find a solution. I know it can't be that difficult since everything else works and looks good. I will create a github repo and post it here, although it is nothing just a new rails project, but as you said maybe you can spot something in the generated files.

Thanks big time for your help, just the fact that you are replying give me fuel to not give up!!

I'm OnlySteveH over at Github - look me up!

I'm sure the error is local, though, so it probably won't replicate. Worth a try, though!

Are these errors occurring when you launch the server with rails s or at some other time?

Have you migrated the database?

Mauris Alonso
Mauris Alonso
6,152 Points

Busy day, but here it is my github repo. I hope something can be found that at least tell us what the problem is.

Thanks big time once again.

https://github.com/malonso2k11/first_project_rails

Can you describe when the error is occurring:

Are these errors occurring when you launch the server with rails s or at some other time?

Have you migrated the database?

Kevin Korte
Kevin Korte
28,149 Points

Is your gem mysql2 in some sort of group like :group => production when your database.yml is using set to use the mysql2 adapter in development, but the gem is not available in dev mode?

Mauris Alonso
Mauris Alonso
6,152 Points

Now that you mentioned the database.yml I have tried few things like comment it out and other things I find online but now I have my doubts. Here is my database.yml codes:

Default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: testpass
  host: localhost

development:
  <<: *default
  database: first_project_development

test:
  <<: *default
  database: first_project_test

production:
  <<: *default
  database: first_project_production
  username: first_project
  password: <%= ENV['FIRST_PROJECT_DATABASE_PASSWORD'] %>

My database.yml file looks a little different with more specific pointers to the database type & path:

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

Also, as Kevin pointed out, there can be different groups within your Gemfile - here's a trimmed down version of one of my apps:

gem 'sqlite3'

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring', '1.3.4'
  gem 'rspec-rails', '~> 2.0'
end

group :test do
    gem 'capybara', '~> 2.1.0'
    gem 'shoulda-matchers', '~>2.8.0'
end

My sqlite3 is defined generically across the app - but that can be tailored within the :development, :production and :test environments. Let's have a look at your Gemfile as there might be something in there.

Steve.

Are these errors occurring when you launch the server with rails s or at some other time?

Have you migrated the database? Do you have a schema.rb file - it isn't in your repo.

rake db:create or rake db:migrate

Mauris Alonso
Mauris Alonso
6,152 Points

I just create the rails project, bundle install and then rails server. After that went I go to the browser and shoot 'localhost:3000' is when this error comes up. I found quite a lot of people having the same problem but not a viable solution so far.

OK now I ran the 'rake db:create' and this is what came back:

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).
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:8:in `establish_connection'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:15:in `create'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:93:in `create'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/malonso/.rbenv/versions/2.2.3/bin/rake:33:in `<main>'
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"testpass", "host"=>"localhost", "database"=>"first_project_development"}
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).
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:8:in `establish_connection'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/mysql_database_tasks.rb:15:in `create'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:93:in `create'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/malonso/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/malonso/.rbenv/versions/2.2.3/bin/rake:33:in `<main>'
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"testpass", "host"=>"localhost", "database"=>"first_project_test"}
rake aborted!
Gem::LoadError: 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).

Gem::LoadError: can't activate mysql2 (~> 0.3.13), already activated mysql2-0.4.0. Make sure all dependencies are added to Gemfile.

Tasks: TOP => db:create
(See full trace by running task with --trace)

The Gem::LoadError pops again !

If this is your first attempt at getting Rails going, I would be very tempted to uninstall/delete everything and start from scratch. Something didn't go well!

Maybe don't use Homebred but install it all in parts. Dunno - but I'm out of ideas and we're getting nowhere. Maybe a fresh start will help.

Steve.

Mauris Alonso
Mauris Alonso
6,152 Points

I agree with you. I going to go all clear out all and install all fresh. I will post back once its done.

Thanks for the help and support ^^

Best of luck!

Mauris Alonso
Mauris Alonso
6,152 Points

While uninstalling on Mac, I was doing the installation on my Fedora workstation computer. It runs flawlessly. I hope Mac pc wont go complicated on me again.

If you call your Mac a pc again, it'll probably refuse to boot ever again. :-P

Oh, and i changed adapter name (config/database.yml) to mysql too.

Mauris Alonso
Mauris Alonso
6,152 Points

Nope, I tried your advice, but still nothing. I am taking my time and doing a throughout analysis in my Mac, because something must be wrong. My Linux laptop run it like a charm, but I no longer know what to do with Mac :(

Man, my first comment didn't load. I changed gem 'mysql2' to gem 'mysql' in Gemfile and after I changed adapter name (config/database.yml) to mysql too.

I don't know why but now working perfectly. =)

Michael Hall
PLUS
Michael Hall
Courses Plus Student 30,909 Points

I had this problem and solved it by adding a version in the gemfile. gem 'mysql2', '~> 0.3.18'