LoginSignup
0
0

More than 3 years have passed since last update.

【AWS・EC2】bundle exec rake db:migrate RAILS_ENV=productionをした際のエラーCould not find gem 'mysql2' in any of the gem sources listed in your Gemfile.の解決法

Last updated at Posted at 2020-04-20

環境

AWS内:rails6
ローカル環境:rails6

したいこと

awsのEC2に接続している状態でbundle exec rake db:migrate RAILS_ENV=productionをしたい。

出ているエラー

EC2の中に入っている状態でbundle exec rake db:migrate RAILS_ENV=productionをすると、以下のエラーが表示される。

Could not find gem 'mysql2' in any of the gem sources listed in your Gemfile.

エラーが出るに至った経緯

・本番環境をAWSのEC2にデプロイしようとしていた際のエラー。
・下記の記事をみながらポートフォリオを本番環境にデプロイしようとしていた。
https://qiita.com/Yuki_Nagaoka/items/dbb185feb6d4f051c2f2#2mysql%E3%81%AE%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0%E3%81%AE%E8%A8%AD%E5%AE%9A
https://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1#mysql%E3%81%AE%E8%A8%AD%E5%AE%9A
・テスト環境、開発環境、本番環境を全てPosgresqlで実装しようと思ったが、EC2を使ってデプロイする方法が紹介されている記事はMysqlが多く、途中でMysqlに変更した経緯がある。

原因・考察

EC2の中のアプリのgemfileにmysql2が入っていない?
権限がない?
EC2上とローカルの環境が異なる?

試したこと

vi Gemfile でGemfileの中を確認。
→mysqlは入っている。(画像参照)

image.png

EC2のアプリ(ポートフォリオ)内でbrew install mysqlする。

以下の様に表示される。

[shogo@ip-10-0-0-58 Portfolio]$ brew install mysql
bash: brew: コマンドが見つかりません

ターミナルにて以下を試す。

$  bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
$ bundle install  https://qiita.com/sun_peace/items/f06cc95e63a71b7296ec

→下記のエラーが表示される。

To see why this extension failed to compile, please check the mkmf.log which can be found here:

 /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/mysql2-0.5.3/gem_make.out

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
[shogo@ip-10-0-0-58 Portfolio]$

→ログを見ろ的なエラーだが、ファイルのいちがわからず。

ps ax | grep mysqlでmysqlが稼働しているかを見ると、以下の様に表示される。

[shogo@ip-10-0-0-58 Portfolio]$ ps ax | grep mysql 
 5032 pts/0  S+   0:00 grep --color=auto mysql
29854 ?    Sl   0:04 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
[shogo@ip-10-0-0-58 Portfolio]$

→一応稼働はしている?

EC2でmysql2をbundle installできない を試す。

ln /home/linuxbrew/.linuxbrew/bin/gcc-7 /home/linuxbrew/.linuxbrew/bin/gccをしたところ、次の様に表示されます。

[shogo@ip-10-0-0-58 Portfolio]$ ln /home/linuxbrew/.linuxbrew/bin/gcc-7 /home/linuxbrew/.linuxbrew/bin/gcc
ln: failed to access `/home/linuxbrew/.linuxbrew/bin/gcc-7': No such file or directory
[shogo@ip-10-0-0-58 Portfolio]$

ターミナルで以下を入力する。

&  bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

以下の様に表示されます。

To see why this extension failed to compile, please check the mkmf.log which can be found here:

 /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/shogo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/mysql2-0.5.3/gem_make.out

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
[shogo@ip-10-0-0-58 Portfolio]$

解決法

sudo yum install mysql-devel

エラー文の上の方に書いてあった。

このエラー、いくつか解決法があるので注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0