mysql2をインストールする際のエラー解決！

AWSにデプロイするに伴い、DBをSQLite3からmysql2へ変更することにしました。
まずgemfileをSQLite3からmysql2へ書き換えて`bundle install`してみると下記のエラーが起こってしまいました！


```
current directory: /Users/fujiwaratakumi/Desktop/WorkShare/vendor/bundle/ruby/2.7.0/gems/mysql2-0.5.3/ext/mysql2
/Users/fujiwaratakumi/.rbenv/versions/2.7.5/bin/ruby -I /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/site_ruby/2.7.0 -r ./siteconf20211223-8259-uwu9op.rb extconf.rb
--with-ldflags\=-L/usr/local/opt/openssl/lib
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
```

いくつかの理由有って、Makefileが作成できないようです。
#そもそもMakefileとは？
「Makefile」は、コンパイル、依存関係の管理、インストールなどのルールを記述しておくためのファイルで、makeコマンドが読み込んで処理を行います。Makefileには、ファイルの生成手順や、プログラムを構成しているファイル同士の関係を記述します
参照先　[「Makefile」ファイル](https://linuc.org/study/knowledge/542/)

#考えられる原因

```
/Users/username/.rbenv/versions/2.7.5/lib/ruby/2.7.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
	from /Users/username/.rbenv/versions/2.7.5/lib/ruby/2.7.0/mkmf.rb:1050:in `collect'
	from /Users/username/.rbenv/versions/2.7.5/lib/ruby/2.7.0/mkmf.rb:1050:in `find_library'
```

というエラー文があったので、そもそもMySQLがローカル環境に入っていない可能性があったので、

```
$ brew install mysql
$ gem install mysql2
```
を実行しました。そうすると

```
$gem install mysql2
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
	ERROR: Failed to build gem native extension.

    current directory: /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
/Users/fujiwaratakumi/.rbenv/versions/2.7.5/bin/ruby -I /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/site_ruby/2.7.0 -r ./siteconf20211223-10562-6ft7i7.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.27_1/lib
-----
creating Makefile

current directory: /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\= clean

current directory: /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\=
compiling client.c
client.c:787:14: warning: incompatible function pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE (*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-function-pointer-types]
  rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
             ^~~~~~~~~~~~~
/Users/fujiwaratakumi/.rbenv/versions/2.7.5/include/ruby-2.7.0/ruby/ruby.h:1990:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
client.c:795:16: warning: incompatible function pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE (*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-function-pointer-types]
    rb_rescue2(do_query, (VALUE)&async_args, disconnect_and_raise, self, rb_eException, (VALUE)0);
               ^~~~~~~~
/Users/fujiwaratakumi/.rbenv/versions/2.7.5/include/ruby-2.7.0/ruby/ruby.h:1990:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
2 warnings generated.
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/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/fujiwaratakumi/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-20/2.7.0/mysql2-0.5.3/gem_make.out
```
エラー文が変化しました！ここまでくれば
[【Rails】MySQL2がbundle installできない時の対応方法]
(https://qiita.com/fukuda_fu/items/463a39406ce713396403)
を参考にエラーを解決できると思い

```
brew info openssl
```
を実行！すると

```
openssl@3: stable 3.0.1 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
==> Dependencies
Required: ca-certificates ✔
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@3/certs

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

openssl@3 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

==> Analytics
install: 100,362 (30 days), 277,536 (90 days), 281,820 (365 days)
install-on-request: 75,962 (30 days), 221,268 (90 days), 224,722 (365 days)
build-error: 4,499 (30 days)
```
なんとopensslが入っていないと言われてしまいました。そこで、verを確認。

```
openssl version
LibreSSL 2.8.3
```
入っていると思うのですが、再度インストール！

```
$ brew install openssl
$ brew info openssl
```
再度中身を確認します！そうすると

```
$ brew info openssl
openssl@3: stable 3.0.1 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@3/3.0.1 (6,420 files, 28.2MB)
  Poured from bottle on 2021-12-23 at 15:52:31
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
==> Dependencies
Required: ca-certificates ✔
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@3/certs

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

openssl@3 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

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

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

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

==> Analytics
install: 100,362 (30 days), 277,536 (90 days), 281,820 (365 days)
install-on-request: 75,962 (30 days), 221,268 (90 days), 224,722 (365 days)
build-error: 4,499 (30 days)
```
中身が変化しました。Pathが通っていることが確認できたので、
`bundle install`を実行。無事導入することができました！！
SQlite３を使っていたので、MySQLとopensslのインストールが入っていなかったことが原因のようです！
無事インストールができてよかったです。この記事が誰かの参考になれば幸いです！


#参考にさせて頂いたサイト
>[Makefileファイル](https://linuc.org/study/knowledge/542/)
>[Homebrew brew linkしてもリンクしたopensslを使ってくれない](https://qiita.com/halo57/items/e7511f3befbcb9fedd6a)
>[bundle install でmysql2インストール時にエラーになる](https://qiita.com/tomsyoya/items/28f938747bcd6889cc9e)
>[【Rails】MySQL2がbundle installできない時の対応方法](https://qiita.com/fukuda_fu/items/463a39406ce713396403)
>