mysql2のgemをインストールしようとして失敗した時の対処法

##はじめに
開発環境
OS   MacOS
Gem  2.7.3
Bundler : 2.0.2

##エラー
rail newでプロジェクト作成後、bundle installを行うと次のようなエラーに遭遇しました。

```

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/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 12.3.3
Using concurrent-ruby 1.1.5
Using i18n 1.6.0
Using minitest 5.12.2
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using zeitwerk 2.1.10
Using activesupport 6.0.0
Using builder 3.2.3
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.4
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.3.0
Using rails-html-sanitizer 1.2.0
Using actionview 6.0.0
Using rack 2.0.7
Using rack-test 1.1.0
Using actionpack 6.0.0
Using nio4r 2.5.2
Using websocket-extensions 0.1.4
Using websocket-driver 0.7.1
Using actioncable 6.0.0
Using globalid 0.4.2
Using activejob 6.0.0
Using activemodel 6.0.0
Using activerecord 6.0.0
Using mimemagic 0.3.3
Using marcel 0.3.3
Using activestorage 6.0.0
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailbox 6.0.0
Using actionmailer 6.0.0
Using actiontext 6.0.0
Using public_suffix 4.0.1
Using addressable 2.7.0
Using bindex 0.8.1
Using msgpack 1.3.1
Using bootsnap 1.4.5
Using bundler 2.0.2
Using byebug 11.0.1
Using regexp_parser 1.6.0
Using xpath 3.2.0
Using capybara 3.29.0
Using childprocess 2.0.0
Using ffi 1.11.1
Using jbuilder 2.9.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.0
Using ruby_dep 1.5.0
Using listen 3.1.5
Using method_source 0.9.2
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Errno::EACCES: Permission denied @ rb_sysopen -
/Users/moriyuuichirou/.rbenv/versions/2.5.0/lib/ruby/gems/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.

In Gemfile:
  mysql2
```

```

Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.
```
と書かれているので、``内のコードを試してみました。

```

$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/moriyuuichirou/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/CHANGELOG.md
```

なんか許可がないとかなんとか書かれていますね。
##試したこと
###opensslのインストール
自分のPCにopensslのgemがインストールされているか確認しましょう。インストールされていなければインストールしましょう。
そして、

```

$ brew info openssl
openssl: stable 1.0.2t (bottled) [keg-only]
SSL/TLS cryptography library
https://openssl.org/
/usr/local/Cellar/openssl/1.0.2t (1,795 files, 12.0MB)
  Poured from bottle on 2019-09-30 at 15:02:48
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl.rb
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

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

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

For pkg-config to find openssl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

==> Analytics
install: 229,823 (30 days), 1,280,416 (90 days), 6,250,474 (365 days)
install_on_request: 56,131 (30 days), 186,838 (90 days), 860,840 (365 days)
build_error: 0 (30 days)
moriyuuichirounoMacBook-Pro:sample moriyuuichirou$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl/include"
You are replacing the current local value of build.mysql2, which is currently nil
```
と入力します。ここで注目するのは、

```

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
```
です。よって、

```

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl/include"
```
と入力します。そして、最後に

```

$ sudo chown -R moriyuuichirou:staff ~/.rbenv

```

と入力して、gem install!
確認してみましょう。

 ```

$ gem list
*** LOCAL GEMS ***
省略
mysql2 (0.5.2)
```

##最後に
以下の文献以外にも色々と調べながらやったので、個人的にちょっと知識がごちゃ混ぜになった感じがします。はじめの

```
gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
```
のエラー文をコピーしてGoogleに貼り付けた方がいい資料が見つかる気がします。

##参考URL
[macOS Mojave環境でmysql2(0.5.2)のgemインストールが失敗した](https://qiita.com/nagative/items/b15424a6d04881d4df86)
[gem mysql2をインストールしようとするとエラーになる](https://teratail.com/questions/167449)