RuntimeError with mysql2 and rails3 (bundler)

63,347

Solution 1

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

Solution 2

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 

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
63,347

Related videos on Youtube

Swistak
Author by

Swistak

Wannabe *

Updated on January 05, 2020

Comments

  • Swistak
    Swistak over 4 years

    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.

  • Senthil Kumar
    Senthil Kumar over 13 years
    This might help someone. stackoverflow.com/questions/3608287/…
  • fullstacklife
    fullstacklife over 13 years
    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 :)
  • Tomasz Iniewicz
    Tomasz Iniewicz about 13 years
    this clued me in. I ran "sudo gem install mysql2" and it worked. Thanks!
  • TrinitronX
    TrinitronX almost 13 years
    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.
  • David Ortinau
    David Ortinau almost 13 years
    I had this exact same issue today, going up to 3.1 and back down to 3.0.7. This was the fix.
  • Kevin
    Kevin almost 13 years
    except those versions of mysql2 have blocking issues in large sites
  • Barry
    Barry almost 13 years
    Same here - apparently mysql2 > 3.0 does not load the AR adapter pronto
  • Gabe Martin-Dempesy
    Gabe Martin-Dempesy almost 13 years
    Manual gem management outside of a Gemfile is not recommended. It is prone to dependency issues for other developers or deployment.
  • Nicolas Raoul
    Nicolas Raoul almost 13 years
    That fixed the problem. On Ubuntu 11.04 Natty
  • Tass
    Tass over 12 years
    Isn't it generally bad practice to install a gem using sudo? stackoverflow.com/questions/2119064/…
  • Tim Harper
    Tim Harper over 12 years
    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: github.com/brianmario/mysql2/issues/155
  • Yuval Karmi
    Yuval Karmi over 12 years
    This should be the selected answer. Excellent!
  • hoff2
    hoff2 almost 12 years
    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+
  • Ms01
    Ms01 over 11 years
    Install homebrew if you are using mac osx and then do brew install mysql. After that, bundle install and it should work. :)
  • stefgosselin
    stefgosselin about 11 years
    @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! :)