Posted at

Railsでmysql2をインストールするときにハマったところ

More than 1 year has passed since last update.


やりたいこと

railsにおいて


  1. sqliteからmysqlに使用するDBMSを切り替える。


config/database.ymlを変更する


database.yml

#変更前

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

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

# 変更後

default: &default
adapter: mysql2
pool: 5
timeout: 5000
username: 'MYSQL_USERS'
password: 'MYSQL_PASSWORD'
socket: /var/lib/mysql/mysql.sock

development:
<<: *default
database: development


上記のymlファイルは開発環境のDBのみを記載。

設定が完了したら、DBを作成

$ bundle exec rake db:create

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

mysqlのgemを入れていなかったのでエラーします。ここまでは大体「rails mysql」とかでググると出てきます。

なのでmysqlをGemfileに記載します。

$ emacs Gemfile

# 以下を追記
gem mysql2

$ bundele install

# 略

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/ext/mysql2
/usr/local/rbenv/versions/2.3.1/bin/ruby -r ./siteconf20160924-12971-1a4niqw.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
-----
mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

エラーした。

どうやら、mysql-develを入れていなったらしい。


$sudo yum install -y mysql-devel
$bundle install
Installing mysql2 0.4.4 with native extensions

#...成功

どうやら上手くいったらしい。

DBを作成する

$ bundle exec rake db:create

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).
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_handling.rb:50:in `establish_connection'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/mysql_database_tasks.rb:8:in `establish_connection'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/mysql_database_tasks.rb:15:in `create'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:93:in `create'

/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:274:in `each'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:248:in `block in execute'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `each'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `execute'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/usr/local/rbenv/versions/2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:180:in `invoke_with_call_chain'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/task.rb:173:in `invoke'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:152:in `invoke_task'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `each'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `block in top_level'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:117:in `run_with_threads'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:102:in `top_level'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:80:in `block in run'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:77:in `run'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
/usr/local/rbenv/versions/2.3.1/bin/rake:23:in `load'
/usr/local/rbenv/versions/2.3.1/bin/rake:23:in `<top (required)>'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli/exec.rb:74:in `load'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli/exec.rb:74:in `kernel_load'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli/exec.rb:27:in `run'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli.rb:332:in `exec'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli.rb:20:in `dispatch'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/cli.rb:11:in `start'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/exe/bundle:34:in `block in <top (required)>'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
/usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/exe/bundle:26:in `<top (required)>'
/usr/local/rbenv/versions/2.3.1/bin/bundle:23:in `load'
/usr/local/rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
Couldn't create database for {"adapter"=>"mysql2", "pool"=>5, "timeout"=>5000, "username"=>"MYSQL_USER", "password"=>"MYSQL_PASSWORD", "socket"=>"/var/lib/mysql/mysql.sock", "database"=>"development"}

なんかエラーが起きた。

mysql2のgemは入れたのに、エラーを起こしてしまう。。。

とりあえず解決策探してmysql2のバージョンを下げればいいとわかったので、デモ環境つくるくらいならコレでいいかと思った。

$ emacs Gemfile

gem 'mysql2', '~>0.3.21' # versionを過去にしてい

$ bundle update
Installing mysql2 0.3.21 (was 0.4.4) with native extensions

$ bundle exec rake db:create
$ mysql

> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| development |
| test |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

できた。

とりあえずrailsでmysqlの利用環境を整えるところまで完了。