homebrewで入れたmysql@5.6とmysql2 gemを動かす

MojaveにUpgradeしたりmysqlをダウングレードしたりしてたら動かなくなったので修正メモ :pencil: 

## 環境
```
macOS Mojave 10.14
mysql 5.6.42
```

## 結論

```sh
❯ bundle exec gem uninstall mysql2
❯ bundle config --local build.mysql2 --with-mysql-config=/usr/local/opt/mysql@5.6/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
❯ bundle install
```

※ 2019/09/11 追記
フラグが２つあるとうまく動かない場合もあるらしい。
[bundle install 時、mysql2でエラー \- Qiita](https://qiita.com/tktcorporation/items/0ef8c930fc18ce72c301)
その場合は片方のflagだけで良さそう。

```sh
❯ bundle config --local build.mysql2 --with-mysql-config=/usr/local/opt/mysql@5.6/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib
❯ bundle install
```

## 試行錯誤の道のり

```sh
❯ bundle exec rake db:create
rake aborted!
LoadError: dlopen(/Users/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
  Referenced from: /Users/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle
/Users/paranishian/Work/my_app/config/application.rb:4:in `require'
/Users/paranishian/Work/my_app/config/application.rb:4:in `<top (required)>'
/Users/paranishian/Work/my_app/Rakefile:4:in `require'
/Users/paranishian/Work/my_app/Rakefile:4:in `<top (required)>'
/Users/paranishian/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/paranishian/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)
```

む、動かんくなった :fearful: 

`bundle doctor`で調査する。

```sh
❯ bundle doctor
The git source `git://github.com/galetahub/ckeditor.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
The git source `git://github.com/carrierwaveuploader/carrierwave.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
The git source `git://github.com/seuros/capistrano-sidekiq.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
Warning: the running version of Bundler (1.16.1) is older than the version that created the lockfile (1.16.2). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
The Gemfile's dependencies are satisfied
The following gems are missing OS dependencies:
 * mysql2: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
```

ふむふむ。
入れ直してみるか。

```sh
❯ bundle exec gem uninstall mysql2
Successfully uninstalled mysql2-0.4.10
❯ be gem uninstall mysql2
❯ bundle install
The git source `git://github.com/galetahub/ckeditor.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.

...

An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10'` succeeds before bundling.

In Gemfile:
  mysql2
```

`mysql2`が入らない :thinking: 

mysql-configに`mysql@5.6`のものを指定してみて再度チャレンジ。

```sh
❯ bundle config build.mysql2 --with-mysql-config=/usr/local/opt/mysql@5.6/bin/mysql_config
❯ bundle install

...

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/opt/mysql@5.6/lib
-----
creating Makefile

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

current directory: /Users/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
result.c:326:40: warning: incompatible pointer types assigning to 'my_bool *' (aka 'char *') from 'bool *' [-Wincompatible-pointer-types]
    wrapper->result_buffers[i].is_null = &wrapper->is_null[i];
                                       ^ ~~~~~~~~~~~~~~~~~~~~
result.c:328:40: warning: incompatible pointer types assigning to 'my_bool *' (aka 'char *') from 'bool *' [-Wincompatible-pointer-types]
    wrapper->result_buffers[i].error   = &wrapper->error[i];
                                       ^ ~~~~~~~~~~~~~~~~~~
2 warnings generated.
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/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10 for inspection.
Results logged to /Users/paranishian/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/mysql2-0.4.10/gem_make.out

An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10'` succeeds before bundling.

In Gemfile:
  mysql2
```

お、ちょっと進捗した :eyes: 
`-lssl`がないらしいのでさらにオプションで追加して再度チャレンジ。

```sh
❯ bundle config --local build.mysql2 --with-mysql-config=/usr/local/opt/mysql@5.6/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
❯ bundle install
...
Bundle complete! 137 Gemfile dependencies, 329 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
```

通った！ :tada: 

## 参考
- [Homebrew で MySQL を 5.7.22 から 8.0.11 にバージョンアップしたら Rails が動かなくなったので対処する - Qiita](https://qiita.com/renny1398/items/5df494a2b2f5d74b24f4)
- [RailsプロジェクトでMySQLがbundle installできなかった - Qiita](https://qiita.com/akito19/items/e1dc54f907987e688cc0)