MySQLをbundleしようとすると失敗する

##gem 'mysql2'をbundle出来ない問題

mysql嫌いになりそう。何回も同じエラーで苦しんだので備忘録としてまとめます。

```
$ bundle
```

```
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Using rake 13.0.1
Using concurrent-ruby 1.1.7
Using i18n 1.8.5
Using minitest 5.14.1
Using thread_safe 0.3.6
Using tzinfo 1.2.7
Using zeitwerk 2.4.0
Using activesupport 6.0.2.1
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.10
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.6.0
Using rails-html-sanitizer 1.3.0
Using actionview 6.0.2.1
Using rack 2.2.3
Using rack-test 1.1.0
Using actionpack 6.0.2.1
Using nio4r 2.5.2
Using websocket-extensions 0.1.5
Using websocket-driver 0.7.3
Using actioncable 6.0.2.1
Using globalid 0.4.2
Using activejob 6.0.2.1
Using activemodel 6.0.2.1
Using activerecord 6.0.2.1
Using mimemagic 0.3.5
Using marcel 0.3.3
Using activestorage 6.0.2.1
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailbox 6.0.2.1
Using actionmailer 6.0.2.1
Using actiontext 6.0.2.1
Using arbre 1.2.1
Using formtastic 3.1.5
Using formtastic_i18n 0.6.0
Using has_scope 0.7.2
Using method_source 1.0.0
Using thor 1.0.1
Using railties 6.0.2.1
Using responders 3.0.1
Using inherited_resources 1.11.0
Using jquery-rails 4.4.0
Using kaminari-core 1.2.1
Using kaminari-actionview 1.2.1
Using kaminari-activerecord 1.2.1
Using kaminari 1.2.1
Using polyamorous 2.3.2
Using ransack 2.3.2
Using ffi 1.13.1
Using sassc 2.4.0
Using sprockets 4.0.2
Using sprockets-rails 3.2.1
Using tilt 2.0.10
Using sassc-rails 2.1.2
Using activeadmin 2.7.0
Using public_suffix 4.0.5
Using addressable 2.7.0
Using execjs 2.7.0
Using autoprefixer-rails 9.8.6.2
Using bcrypt 3.1.15
Using bindex 0.8.1
Using popper_js 1.16.0
Using bootstrap 4.5.2
Using bundler 2.1.4
Using byebug 11.1.3
Using regexp_parser 1.7.1
Using xpath 3.2.0
Using capybara 3.33.0
Using mini_magick 4.10.1
Using ruby-vips 2.0.17
Using image_processing 1.11.0
Using carrierwave 2.1.0
Using childprocess 3.0.0
Using coffee-script-source 1.12.2
Using coffee-script 2.4.1
Using coffee-rails 5.0.0
Using orm_adapter 0.5.0
Using warden 1.2.8
Using devise 4.7.2
Using devise-i18n 1.9.1
Using diff-lcs 1.4.4
Using dotenv 2.7.6
Using dotenv-rails 2.7.6
Using enum_help 0.0.17
Using factory_bot 6.1.0
Using factory_bot_rails 6.1.0
Using multipart-post 2.1.1
Using faraday 1.0.1
Using font-awesome-rails 4.7.0.5
Using jquery-ui-rails 6.0.1
Using momentjs-rails 2.20.1
Using fullcalendar-rails 3.9.0.0
Using hashie 4.1.0
Using jbuilder 2.10.0
Using turbolinks-source 5.2.0
Using turbolinks 5.2.1
Using jquery-turbolinks 2.1.0
Using json 1.8.6
Using jwt 1.5.6
Using rb-fsevent 0.10.4
Using rb-inotify 0.10.1
Using listen 3.2.1
Using multi_json 1.15.0
Using multi_xml 0.6.0
Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
Errno::EACCES: Permission denied @ rb_sysopen -
/Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/CHANGELOG.md
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
```

またお前か、、いい加減にしろ。
##解決までの道のり

とりあえずエラー文を見ると以下を実行しろとのこと。sudo付けないと権限問題でエラーが出るのでつける。

```
$ sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'
```

```
--source 'https://rubygems.org/'
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    current directory: /Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/Users/matsumotoshun/.rbenv/versions/2.6.3/bin/ruby -I /Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/2.6.0 -r ./siteconf20200825-6979-21jocc.rb extconf.rb
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.21_1/lib
-----
creating Makefile

current directory: /Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/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/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/gem_make.out
```

まあ出来ないんですけど、、、ある記事に
ビルド時にOpenSSL公開鍵のPATH指定が必要」と「bundle configでPATHオプションをLDFLAGS、CPPFLAGSで別々に指定」が必要だとかなんとか、、そうかそうか。分からん。

 その記事:https://qiita.com/4EAE_Learner/items/110f7e025b1d842d4bee

まあとりあえずopensslというものが必要らしい。brewでインストール。

```
$ brew install openssl
```

次はこいつで公開鍵のpathを確認する。

```
$ brew info openssl
```

```
省略

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"　　　　　　←　ここ使用
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"　　　←　ここ使用

省略
```
なんか出てきた。これがpathみたい？なのでこいつを使うと、、以下のコマンドでパスを指定する。

```
$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib"
```
これで出来たはず、、

```
$ bundle 
```

```
Errno::EACCES: Permission denied @ rb_sysopen -
/Users/matsumotoshun/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/CHANGELOG.md
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
```

出来ねーじゃねーか。パス通ってないの？そしたらこんな記事があった。
https://stackoverflow.com/questions/51264240/rake-dbmigrate-error-with-mysql2-gem-library-not-loaded-libssl-1-0-0-dylib/51269245

この記事によるとやってることは同じみたいだけどgem installのコマンド + mysqlのバージョン指定 + さっきのpath指定でmysqlをインストールできるみたい。

```
$ gem install mysql2 -v 0.5.3 -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib
```

記事にはmysql2 -v 0.4.10って書いてあるけど、自分のbundle時のエラーでは0.5.3って書いてあったのでそれに合わせる。
どうせ出来ないんだろ？ダメもとでbundleする

```
$ bundle
```

```
Bundle complete! 39 Gemfile dependencies, 132 gems now installed.
Gems in the group production were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
```

いやできるんかい！汗