77

I get this error

`establish_connection': Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load -- active_record/connection_adapters/mysql2_adapter) (RuntimeError)
  from /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/activerecord-

Here is dump of whole error and my config and gemfile.

djot
  • 2,952
  • 4
  • 19
  • 28
Swistak
  • 871
  • 1
  • 6
  • 8
  • I faced the same problem and after several analysis, i am able to successfully install the mysql2 on windows with rails3. The process i followed is given in the following blog post. http://www.kyybaventures.com/blog/installing-mysql2-gem-ruby-rails-windows Hope, it will be useful to solev the above problem. Mani – maniempire Aug 04 '11 at 16:30

11 Answers11

101

I was getting the same error while using rails 3.0.7 and mysql2 0.3.2. The solution, which I found here, is to use an older version of mysql2. Thus edit your gemfile to

gem 'mysql2', '< 0.3'

and run

bundle install 
TrinitronX
  • 4,959
  • 3
  • 39
  • 66
stream7
  • 1,728
  • 1
  • 14
  • 21
  • This one fixed it for me on Ubuntu 10.04!! If using RVM, you can also use: `rvm gem install mysql2 --version=0.2.7`, and add the above line to your Gemfile. – TrinitronX May 19 '11 at 22:00
  • I had this exact same issue today, going up to 3.1 and back down to 3.0.7. This was the fix. – David Ortinau May 23 '11 at 01:51
  • 2
    except those versions of mysql2 have blocking issues in large sites – Kevin Jun 03 '11 at 22:34
  • Same here - apparently mysql2 > 3.0 does not load the AR adapter pronto – Julik Jun 13 '11 at 13:57
  • That fixed the problem. On Ubuntu 11.04 Natty – Nicolas Raoul Jun 21 '11 at 06:39
  • 3
    For more enlightenment, see the following mysql2 issue on github, which says mysql 0.3.x will only work with rails 3.1 and onward: https://github.com/brianmario/mysql2/issues/155 – Tim Harper Aug 24 '11 at 18:54
  • This should be the selected answer. Excellent! – Yuval Karmi Dec 01 '11 at 23:18
  • Still useful for those of us still maintaining Rails 2.3 apps but who want to use mysql2 for its better UTF-8 handling. I don't think Rails < 3 works with mysql2 0.3+ – hoff2 Jul 27 '12 at 15:51
  • Install `homebrew` if you are using mac osx and then do `brew install` mysql. After that, `bundle install` and it should work. :) – Ms01 Jan 26 '13 at 00:33
36

Also need to change adapter from mysql to mysql2 in database.yml as said here Install mysql2 gem on Snow Leopard for Rails 3 with rvm

From:

development: adapter: mysql

To:

development: adapter: mysql2

Community
  • 1
  • 1
vrybas
  • 1,719
  • 17
  • 12
14

Did you include the mysql2 gem in your gemfile instead of the old mysql gem, and ran bundle install afterwards?

amaseuk
  • 2,147
  • 4
  • 24
  • 43
6

If you're using rvm, and possibly added mysql2 outside of rvm, try these steps: Confirm that your Gemfile says:

gem 'mysql2'

or for Rails2.x:

gem 'mysql2', '~> 0.2.11'

then:

$ cd RAILS_ROOT
$ gem uninstall mysql2

Select gem to uninstall:
 1. mysql2-0.2.11
 2. mysql2-0.3.6
 3. All versions
> 3 # select "All versions"
$ rvm gemset install mysql2
$ bundle install

Now rails should start properly.

mmell
  • 2,448
  • 1
  • 24
  • 16
3

This also fixed the issue I had with:

Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load -- active_record/connection_adapters/mysql2_adapter)

You actually need to run: gem install mysql2 and add the gem to your config.

maxkaplan
  • 31
  • 2
  • 1
    I was having this problem as I was installing the mysql2 gem manually. Then I used it as you said in the gemfile and ran bundle install. It loaded up, but I I got the could not connect to mysql.sock error. So I THEN ran env ARCHFLAGS="-arch i386" gem install mysql2 --version 0.2.6 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config (i am on a mac pro) and this seemed to solve my problems. Ran rails s and hey presto..Hope this helps someone else out :) – fullstacklife Jan 16 '11 at 21:37
  • @dryprogrammers - Wich I could give you 100+, please put your comment as answer, it is VERY relevant for Ubuntu 12-10 as well. On fully updated Ubuntu 12.10 Precise gitlab would not install. Bump. After many hours (6-8) of searching and trying many things, I tried your mac hack as a last resort. LO AND BEHOLD much as I (cough) don`t really care at all for Macs ... this did it for me now I can go to bed ... happy! :) – Stephane Gosselin Mar 13 '13 at 04:16
2

just run "gem install mysql" also add the same in gemfile and run bundle. worked

supriya
  • 108
  • 6
2

If anybody still facing problem of installing mysql2 gem with rails 3 on Windows, then refer to the detailed steps of installation on -

http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

Ritesh Kumar
  • 2,183
  • 2
  • 20
  • 32
1

I'm a beginner at ruby, rails, and linux. So if this solution did not work, I hold no responsibility :)

Step 1:

sudo gem uninstall mysql2

Step 2:

sudo gem install mysql -v 0.2.7

Start webrick, if the same problem still appears reinstall rails.

This solution works for me.

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
Tim
  • 11
  • 1
  • 1
    Isn't it generally bad practice to install a gem using sudo? http://stackoverflow.com/questions/2119064/sudo-gem-install-or-gem-install-and-gem-locations – Tass Aug 18 '11 at 13:52
1

Same issue for me too. Upgraded to Rails 3.1.rc4 then downgraded to 3.0.3. Worked for me.

connie
  • 11
  • 1
0

as of 0.3.0, and ActiveRecord 3.1 - the ActiveRecord adapter has been pulled out of this gem and into ActiveRecord itself. If you need to use mysql2 with Rails versions < 3.1 make sure and specify gem "mysql2", "~> 0.2.7" in your Gemfile

the missing file (no such file to load) can be found in the pre 0.3.0 versions of mysql2.

quoted from the documentation of mysql2

0

I had the same error after upgrading from Ubuntu 11.10 to 12.04. This is how I fixed the problem:

gem uninstall mysql2
bundle

I think the key here is the 'native extensions' -- I suppose when I installed last, I was using a different version of mysql.

Installing mysql2 (0.3.11) with native extensions
Josh W Lewis
  • 1,319
  • 11
  • 19