bundle exec rails s が出来なくなった件の対処メモ

Timemachineのバックアップデータから復元したPCで、どうもmysqlまわりの挙動がおかしいので、新しく入れ直そうかと
`brew update`
して
`brew install mysql`
でmysqlを入れ直したあと
`bundle exec rails s` をすると、エラーが出て起動しなくなってしまいました。
## 環境
OS: mac OSX El Capitan 10.11.2
Ruby: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.4.0]
Mysql: 5.7.10 (Homebrew)

動くまでに色々エラーで躓いたので、メモしておきます。

# Library not loaded: /usr/local/lib/libmysqlclient.18.dylib (LoadError)

```
$ bundle exec rails s

/Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require': dlopen(/Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
  from /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `<top (required)>'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `require'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `each'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `block in require'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `each'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `require'
  from /Users/miyuki/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.7.7/lib/bundler.rb:133:in `require'
  from /Users/miyuki/repo/config/application.rb:16:in `<top (required)>'
  from /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/railties-3.2.22/lib/rails/commands.rb:53:in `require'
  from /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/railties-3.2.22/lib/rails/commands.rb:53:in `block in <top (required)>'
  from /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/railties-3.2.22/lib/rails/commands.rb:50:in `tap'
  from /Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/railties-3.2.22/lib/rails/commands.rb:50:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'
```

http://qiita.com/puzzel/items/8768324d6c60417f4d6c
を参考に

```
$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
```

をやってみたのですが

```
ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted
```
となってしまったので、いったん別の方のコメント
>vendor/bundleを削除してからbundle installし直す

のほうを試してみたところ、そもそもbundle installが失敗した。。

エラーログ

```
An error occurred while installing eventmachine (1.0.3), and Bundler cannot continue.
Make sure that `gem install eventmachine -v '1.0.3'` succeeds before bundling.
```
とのことで、どうやら下記のgemで問題があるようです。

# gem eventmachineのインストールが失敗する

```

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/miyuki/.rbenv/versions/2.0.0-p247/bin/ruby extconf.rb –with-cppflags=-I/include
checking for rb_trap_immediate in ruby.h,rubysig.h... no
checking for rb_thread_blocking_region()... yes
checking for inotify_init() in sys/inotify.h... no
checking for __NR_inotify_init in sys/syscall.h... no
checking for writev() in sys/uio.h... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enable_interrupt()... no
checking for rb_time_new()... yes
checking for sys/event.h... yes
checking for sys/queue.h... yes
creating Makefile

make "DESTDIR="
compiling binder.cpp
In file included from binder.cpp:20:
./project.h:107:10: fatal error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>
         ^
1 error generated.
make: *** [binder.o] Error 1
```

### 対策
http://stackoverflow.com/questions/30818391/gem-eventmachine-fatal-error-openssl-ssl-h-file-not-found#_=_
を参考に、

```
$ bundle config build.eventmachine --with-cppflags=-I$(brew --prefix openssl)/include
```

で解決。
`bundle install` もできるようになりました。

で、再び ` bundle exec rails s` してみると…
また別のエラー

# gem mysql2 でエラー

```
/Users/miyuki/repo/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/client.rb:12:in `<class:Client>': uninitialized constant Mysql2::Client::SECURE_CONNECTION (NameError)
```

### 対策
https://github.com/brianmario/mysql2/issues/603#issuecomment-158705807
を参考に
mysql2のversionを `0.3.17` にしたら
ようやく
`bundle exec rails s` できるようになりました！