あざらし備忘録。

音ゲー好きなウェッブエンジニアがいろいろ思った事やった事を書いていくブログです

macのMAMPのMySQLを使ってRubyのmysql2をインストールする

詰まったのでメモ。

日本語の記事ほとんどなかったので参考になれば〜。

はじめに

環境は以下の様な感じです。

  • MAMP 2.1.3
  • rbenv
  • ruby2.0.0-p195

そこまで環境依存する問題ではないように感じましたが、念のため。

gem install mysql2やったら怒られた

いつもどおりgemを入れようと思ってふんふーんって感じで(実際ははじめにbundlerで落ちましたがややこしくなるため省略w)

$ gem install mysql2

ってやったら

Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

/Users/g-nakanishi/.rbenv/versions/2.0.0-p195/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
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
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
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/g-nakanishi/.rbenv/versions/2.0.0-p195/bin/ruby
--with-mysql-config
--without-mysql-config
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/
--with-mysqlclientlib
--without-mysqlclientlib
--with-mlib
--without-mlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mlib
--without-mlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-zlib
--without-zlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-socketlib
--without-socketlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-nsllib
--without-nsllib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mygcclib
--without-mygcclib
--with-mysqlclientlib
--without-mysqlclientlib

Gem files will remain installed in /Users/g-nakanishi/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mysql2-0.3.13 for inspection.
Results logged to /Users/g-nakanishi/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mysql2-0.3.13/ext/mysql2/gem_make.out

。。。落ちた。。。 ググり業開始。

--with-mysql-configの指定方法

こんなのが見つかってきました。mysql2のgithubより、

If you have installed MySQL to a non-standard location, add
gem install mysql2 --with-mysql-config=/some/random/path/bin/mysql_config

なのでまずはMAMPのmysql_configのパスを入手

$ sudo find / -name mysql_config

/Applications/MAMP/Library/bin/mysql_configとわかったので以下のように実行

$ gem install mysql2 --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

ログを取り忘れたのですが、mysql.hがねぇよと。findかけても確かにない。。。
というか色々調べてみた結果MAMPにはもともとmysqlのinclude関連が入ってない模様。。。
ググり業再び。

荒業で入れる

調べていくうちにここここを見つけ、どうやら本家のmysqlを落としてきてmakeしてまるまるcpしている...!?まじですか。

まぁやりましょう。

MAMP2.1.3で同梱されているmysqlは5.5.29のようなので、こことかからmysqlをダウンロードしてきて保存したディレクトリにて次のように実行

$ tar -xzvf mysql-5.5.29.tar.gz
$ cd mysql-5.5.29
$ brew install cmake # cmakeが入っていれば不要
$ cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
$ make -j2
$ cp libmysql/*.dylib /Applications/MAMP/Library/lib/
$ mkdir -p /Applications/MAMP/Library/include/mysql
$ cp include/* /Applications/MAMP/Library/include/mysql
$ env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

各コマンドに関しての説明は先の参考サイトに書いてあるのでご参照ください。

これでruby全体のgemにはmysql2がインストール出来ました!!!!

bundlerを使ってmysql2を入れる

このままだと、bundle installすると同じように落ちてしまいます。 このあたりの記事を参考に、次のようにしました。

$ bundle config build.mysql2 --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
$ bundle install --path vendor/bundle

mysql2のビルドコンフィグを設定してあげて、bundle install、みたいなことができるのですね。便利。というか無いとツラいか。

ともかく、これで無事にmacのMAMPのMySQLを使ってRubyのmysql2をインストールすることが出来ました!

参考サイトの方々、良記事本当にありがとうございました!
Thanks for using your explanation as reference!

終わりに

こうやって物語形式で書くか、「こうしたら入るよ」ってコマンドだけびゃって書くか、どっちがいいのか未だに悩んでる。
自分で読んでる時もどっちでもいいしな〜どっちでもいいのかなw
とりあえずMAMPはなんでmysql.hを始めいろいろはいってないんだよと小一時間問い詰めたい、っていうか大丈夫なのこれ??

参考