简体   繁体   中英

Rails mysql2 error: “rake aborted! Please install the mysql2 adapter…”

I am running this on a Windows 7 (64 bit) machine. I installed RoR using the one-click installer. I've updated my database.yml file to use mysql2:

development:
  adapter: mysql2
  encoding: utf8
  database: blog_development
  pool: 5
  username: root
  password: 
  socket: /tmp/mysql.sock
test:
  adapter: mysql2
  encoding: utf8
  database: blog_test
  pool: 5
  username: root
  password: 
  socket: /tmp/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  database: blog_production
  pool: 5
  username: root
  password: 
  socket: /tmp/mysql.sock

I added this line to my Gemfile (per the tutorial video):

gem 'mysql2', :group => :production

Then:

gem install mysql2

which succeeds. Then:

bundle install

Which also succeeds, but mysql2 is not listed.

Then:

rake db:create

which gives this error:

"rake aborted! Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (my sql2 is not part of the bundle. Add it to Gemfile.) Tasks: TOP => db:create (See full trace by running task with --trace)"

bundle show mysql2

Gives this error: "Could not find gem 'mysql2' in the current bundle."

What am I missing to get mysql2 going?

Go to your app

  1. Open Gemfile
  2. Add this line

    gem 'mysql2'

rake db:create之前运行bundle install (在gem install mysql2

Similar issue was resolved for me after

  • creation of libmysql.lib file per https://github.com/brianmario/mysql2/issues/486 and using it to install/compile native gems (lib resided in directory used for "--with-mysql-lib="$mysql top_path/lib"" gem installation)
  • putting libmysql.dll to ruby_top bin folder
  • installing both mysql and mysql2 gems (was getting exactly the same error with just mysql2 gem installed, though database.yml had "adapter = mysql2" everywhere).

After all that mysql2 appeared in list of gems after command "bundle install", I was able to run "rake db:create" successfully, started redmine x64 windows on x64 ruby 2.0 with x64 mysql on webrick, going on with configuring on some production server.

===============

An update

I have to precise that besides installing both mysql and mysql2 I created file Gemfile.local in redmine application top directory which probably made the trick with bundler. So I would recommend to replace the last step with:

  • installing mysql2 gem
  • creating the file Gemfile.local in application top directory where you list local gems to be included to your bundle.

I saw another answer that recommends to add mysql2 gem to Gemfile, but for me mysql2 was already included in Gemfile but has appeared in bundler output after adding to Gemfile.local only.

I'm leaving both solutions if I'm wrong and the trick was done by mysql gem installed together with mysql2, unfortunatelly I cannot remove/reinstall all from scratch now to test that for sure, I will update when I'm able to do so, hope all this will save some time to someone.

My Gemfile.local file contents is:

---8<---

gem "mysql2", "~> 0.3.11"

gem "eventmachine"

gem "thin"

---8<---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM