気ままに気ままのエンジニアブログ

定期的に得た知見を気ままに発信中

MySQL install時のエラー解決方法(bundle install)

こんにちは。

プログラミングを始めた当初は、エラーを見る度に正直ため息付いてましたが、ここ最近なんかエラーが可愛く見えてきました。

f:id:hachimaki37:20200419122158j:plain
エラーって猫なのかな?
どうもハチマキです。

はじめに

ここではローカル開発環境構築に伴い、躓くポイントをまとめております。

今回は、MySQLインストール時に起こり得るエラーの解決方法についてです。
 ※MySQLインストール時は、様々なエラーが発生することが考えられます。
  エラー文をしっかり読み、原因を特定し、解決アクションを実施してみましょう。
 ※下記の手順は、あくまで一つの原因に対するソリューションです。

下記の手順を行えば一つ前に進むことができますので、深呼吸して、一つずつ解決していきましょう!

ちなみに私は、MySQLのエラーを解決するのに数日費やしました。。辛かったなぁ、、

本日の概要 : MySQLインストール時のエラー解決方法

  • 各種バージョン
  • エラー例
  • 問題点
  • 解決方法
  • 備考
  • 参考資料

各種バージョン

事象例

bundle installしたら指定バージョンのMySQLをインストールして!と指示された。
言われた通りのコマンド打ったのに同じエラーが発生する。。

エラー例


Fetching mysql2 0.4.2
Installing mysql2 0.4.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.2/ext/mysql2
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20200107-59499-1yptfkw.rb extconf.rb --with-ldflags=-L/usr/local/opt/openssl/lib
ーーwith-cppflags=-I/usr/local/opt/openssl/include
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
ーー
Using mysql_config at /usr/local/bin/mysql_config
ーー
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
ーー
Setting rpath to /usr/local/Cellar/mysql/8.0.18_1/lib
ーー
creating Makefile

To see why this extension failed to compile, please check the mkmf.log which can be found here:

    /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/extensions/universal-darwin-17/2.3.0/mysql2-0.4.2/mkmf.log

current directory: /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.2/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.2/ext/mysql2
make "DESTDIR="
compiling client.c
In file included from client.c:1:
In file included from ./mysql2_ext.h:41:
./result.h:24:3: error: unknown type name 'my_bool'
my_bool *is_null;
    ^
./result.h:25:3: error: unknown type name 'my_bool'
my_bool *error;
    ^
client.c:434:3: error: use of undeclared identifier 'my_bool'
my_bool res = mysql_read_query_result(client);
    ^
client.c:436:19: error: use of undeclared identifier 'res'
return (void *)(res == 0 ? Qtrue : Qfalse);
        ^
client.c:755:3: error: use of undeclared identifier 'my_bool'
my_bool boolval;
    ^
client.c:786:7: error: use of undeclared identifier 'boolval'
boolval = (value == Qfalse ? 0 : 1);
        ^
client.c:787:17: error: use of undeclared identifier 'boolval'
retval = &boolval;
          ^
client.c:790:10: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
case MYSQL_SECURE_AUTH:
    ^~~~~~~~~~~~~~~~~
    MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.18_1/include/mysql/mysql.h:187:3: note: 'MYSQL_DEFAULT_AUTH' declared here
MYSQL_DEFAULT_AUTH,
    ^
client.c:791:7: error: use of undeclared identifier 'boolval'
boolval = (value == Qfalse ? 0 : 1);
    ^
client.c:792:17: error: use of undeclared identifier 'boolval'
retval = &boolval;
        ^
client.c:823:38: error: use of undeclared identifier 'boolval'
wrapper->reconnect_enabled = boolval;
            ^
client.c:1178:38: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
            ^~~~~~~~~~~~~~~~~
            MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.18_1/include/mysql/mysql.h:187:3: note: 'MYSQL_DEFAULT_AUTH' declared here
MYSQL_DEFAULT_AUTH,
    ^
12 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.2 for inspection.
Results logged to /Users/ユーザー名/アプリケーション名/vendor/bundle/ruby/2.3.0/extensions/universal-darwin-17/2.3.0/mysql2-0.4.2/gem_make.out

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

In Gemfile:
    mysql2

このエラーでは、Make sure that `gem install mysql2 -v '0.4.2'とmysqlのバージョンを指定して、インストールして!と訴えております。
しかし、あるコマンドを実行していないとうまくインストールされません。

問題点

  1. MySQLをHomebrewでインストールしていない
  2. バージョン指定せずにMySQLをインストールした

おそらく上記のどちかに問題が発生しており、うまくMySQLのインストールが出来ていないのでしょう。

解決方法

2つのコマンドを実行します。
1. $ brew install mysql@5.6
こちらのコマンドをまず実行しましょう。ダウンロードが開始されるはずです!


==> Downloading https://homebrew.bintray.com/bottles/mysql@5.6-5.6.46_2.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/b3/b3b0ca8146a2d477c8c37b5b9f7b775ea316fbc424ae30b04f8abe95e8c1c1f1?__gda__=exp=1578394547~hmac=064d068f31870d15c2222b3c5e604e79bad
###################### 100.0%
==> Pouring mysql@5.6-5.6.46_2.high_sierra.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To connect:
    mysql -uroot

mysql@5.6 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.6 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql@5.6/bin:$PATH"' >> ~/.bash_profile

For compilers to find mysql@5.6 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.6/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.6/include"


To have launchd start mysql@5.6 now and restart at login:
  brew services start mysql@5.6
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.6/bin/mysql.server start
==> Summary
🍺 /usr/local/Cellar/mysql@5.6/5.6.46_2: 343 files, 154.7MB

2. $ sudo gem install mysql2 -v '0.4.2'
続いて、こちらのバージョン指定してコマンドを実行しましょう。すると、インストールされていくはずです!

Password:
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.2
Parsing documentation for mysql2-0.4.2
Installing ri documentation for mysql2-0.4.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed
これで、MySQLのインストールが完了しました!
お忘れなく最後に、$ bundle installを実行していきましょう。

環境構築完了まであと少しです!

備考

$ gem uninstall mysql2
不要にインストールしてしまったものは、上記コマンドでアンインストールしてしまいましょう。