MAMP でインストールした mysql に ruby からアクセスしたい。
しかるに gem install mysql2
とするとエラーになる。
% gem install mysql2
This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/maangie/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20150603-18362-t9oxdg.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
-----
*** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/maangie/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
--with-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
extconf failed, exit code 1
Gem files will remain installed in /Users/maangie/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/maangie/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0-static/mysql2-0.3.18/gem_make.out
要は mysql.h が無いのがいかん、と。
MAMP Forum • View topic - mysql rails and gem install に「the devel version of MAMP」を使えって書いてあるけど、その下に「I'm having a similar problem. I don't see the developer version of MAMP on the download page. Is it still there? (同様の問題を抱えています。the developer version of MAMP が見つかりません。まだありますか?)」とあるように、既にない。
それで、Homebrew でインストールすればいけるんじゃね?と…。
% brew install mysql
% gem install mysql2 -- --with-mysql-include=/usr/local/Cellar/mysql/5.6.25/include/mysql
なんか無理くりですけど…。
ちゃんと動きました。
require 'active_record'
# DB接続設定
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: 'localhost',
username: 'username',
password: 'password',
database: 'mydb',
socket: '/Applications/MAMP/tmp/mysql/mysql.sock'
)
# テーブルにアクセスするためのクラスを宣言
class User < ActiveRecord::Base
end
# レコード取得
p User.all
brew uninstall mysql
したら動きませんでした。あたり前田のクラッカー。