RailsにMySQLを導入したらかなりハマった件について

Railsアプリケーションを作るにあたってMySQLを導入して作成しようと思ったら、謎のエラーでかなりハマってしまったので忘備録にメモしておこうと思います。調べるのに結構時間かかったのでこれは誰かの役に立つだろう…。

スポンサードリンク

RailsにMySQLが入らない

今回のエラーは、Gemfileに gem ‘mysql2’ を記述して bundle install しようとした時に起きた出来事です。次のようなエラーが出ました、、、。

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds before bundling.

ちなみにこのときOS MojaveにアップデートしていたのでMySQLもxcode-select(Command Line Tool)も最新のものをインストールしていました。

$ mysql --version
mysql Ver 8.0.12 for osx10.14 on x86_64 (Homebrew)

まずはエラーの通りにgemをインストールしてみます。

$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
-省略-
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/8.0.12/lib
-----
creating Makefile

current directory: /Users/tatsuya/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/tatsuya/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/tatsuya/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/tatsuya/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/mysql2-0.5.2/gem_make.out

うーん、頭が痛くなります。もうなんかよくわからないですね。 clang: error: linker command failed with exit code 1 (use -v to see invocation) とあるのでなにやらリンカコマンドに問題があるのは確認できます。何かがビルド出来てない?

とにかくネットを調べまくって原因を追及しました。

スポンサードリンク

救世主となった記事達

1時間くらい試行錯誤していると、運良くエラーを解決してくれそうな記事に出会いました。それがこちら

この記事によると ld: library not found for -lssl から、opensslのパスがビルドの時に必要だとか何とか。ビルドのときにLDFLAGSとかCPPFLAGSとかにパスを追加する記述が必要だそうです。プログラミング初心者には結構きつい。というか、こんなの良く解決できますね…。

とりあえず以下のコードを打ち込む。

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

よし、これで大丈夫!

$ bundle install
Errno::EACCES: Permission denied @ rb_sysopen -
/Users/tatsuya/rails-space/application/rails_app5/vender/bundle/ruby/2.5.0/gems/mysql2-0.5.2/CHANGELOG.md
An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds before bundling.

ん?まだ無理っぽい。Permission deniedと出ているので権限周りが良くなさそうですね。次にこちらの記事を参考にしたらsudoだけでいけるっぽいので試してみることにします。

$ sudo bundle install
Installing mysql2 0.5.2 with native extensions
Bundle complete! 13 Gemfile dependencies, 63 gems now installed.
Bundled gems are installed into `./vender/bundle`

いけました!!!!!!

まとめ

RailsにMySQLを入れたら大変な苦労をしたのでこの記事が誰かの役に立てば幸いです。なんでこんなに面倒なんだ!笑

p.s.先人達の知恵に感謝します。

プログラミングではこのような面倒なエラー解決に時間を掛けることがよくあります…。

効率よくプログラミングを学習したいのであれば「テックブースト」をオススメします。オンラインでどこにいても聞き放題です。無料カウンセリングも行なっているので一度話を聞きに行ってみるというのもアリだと思います。

テックブーストについてより詳しく>>

スポンサードリンク

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です