世の中のもやもやを「スッキリ」と!!

コンピュータ関係をつぶやきます。世の中の役に立つことを書いていきたいです。

4-4データベースのセットアップでエラーになった場合の対処法

Chapter4-2 データベースのセットアップにて

テキスト通りに進めて
bin/rake db:createをすると


[vagrant@vagrant-centos65 vagrant]$ bin/rake db:create
/home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:190:in `rescue in spec': 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)
from /home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:187:in `spec'
from /home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/railtie.rb:129:in `block (2 levels) in '
from /home/vagrant/.gem/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /home/vagrant/.gem/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /home/vagrant/.gem/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
from /home/vagrant/.gem/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
from /home/vagrant/.gem/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
from /home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/base.rb:326:in `'
from /home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/base.rb:23:in `'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:338:in `active_record_configured?'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:259:in `disconnect_database'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:97:in `preload'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:143:in `serve'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /home/vagrant/.gem/ruby/2.1.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `'
from /opt/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /opt/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `
'
がっつりエラーが出ます。
その場合

stackoverflow.com

を参考に、Gemfileの中の1行を変更します。

具体的には
gem 'mysql2'

gem 'mysql2', '~> 0.3.18'
変更したGemfileは以下の通りです。


source 'https://rubygems.org'
ruby '2.1.0'

gem 'rails', '4.1.0'
gem 'mysql2', '~> 0.3.18'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development

gem 'therubyracer', platforms: :ruby
gem 'bcrypt', '~> 3.1.7'
gem 'nokogiri', '~> 1.6.1'
gem 'rails-i18n', '~> 4.0.1'
group :test do
gem 'rspec-rails', '~> 3.0.0'
gem 'spring-commands-rspec', '~> 1.0.1'
gem 'capybara', '~> 2.2.1'
gem 'factory_girl_rails', '~> 4.4.1'
end