質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

1815閲覧

AWS(EC2)の本番環境でunicornを起動するとエラーが発生する

yuu0000

総合スコア4

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2021/07/15 10:24

前提・実現したいこと

現在アプリケーションをAWSのEC2で立ち上げるために実装を進めているのですが、本番環境でunicornを起動したところエラーが起きたためご相談させて下さい。

打ち込んだコマンド

[ec2-user@ip-xxxxxxxxx locat] bundle exec unicorn_rails -c config/unicorn.rb -E production -D

[ec2-user@ip-xxxxxxxxx locat] RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D

試しに、下のコマンドも打ってみましたが同様のエラーが起こりました。

###実装のイメージ

実装のイメージとしては、ユニコーンの起動により本番環境でRailsのアプリケーションを動かすことができることになります。

発生している問題・エラーメッセージ

コマンドを打った時のエラーコード

[ec2-user@ip-xxxxxxxxx locat]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D master failed to start, check stderr log for details

エラーログ

I, [2021-07-14T10:34:08.077769 #15926] INFO -- : Refreshing Gem list bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/3.0.1/bin/unicorn_rails) /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ ext/kernel_require.rb:23:in `require': libmysqlclient.so.18: cannot open shared object file: No such file or di rectory - /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.so (Load Error) from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_pat h_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_pat h_cache/loaded_features_index.rb:92:in `register' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_pat h_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_pat h_cache/core_ext/kernel_require.rb:31:in `require' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `block in require' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:299:in `load_dependency' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `require' from /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/lib/mysql2.rb:36:in `<main>'

エラーコードの詳細を開いたところ、下記が原因になっていることが分かりました。

Refreshing Gem list bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/3.0.1/bin/unicorn_rails) /home/ec2-user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ ext/kernel_require.rb:23:in `require': libmysqlclient.so.18: cannot open shared object file: No such file or di rectory

該当のソースコード

[config/database.yml]

# MySQL. Versions 5.5.8 and up are supported. # # Install the MySQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: socket: /tmp/mysql.sock development: <<: *default database: locat_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: locat_test production: <<: *default database: <%= Rails.application.credentials.db[:database] %> username: <%= Rails.application.credentials.db[:username] %> password: <%= Rails.application.credentials.db[:password] %> socket: <%= Rails.application.credentials.db[:socket] %> # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password or a full connection URL as an environment # variable when you boot the app. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # If the connection URL is provided in the special DATABASE_URL environment # variable, Rails will automatically merge its configuration values on top of # the values provided in this file. Alternatively, you can specify a connection # URL environment variable explicitly: # # production: # url: <%= ENV['MY_APP_DATABASE_URL'] %> # # Read https://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full overview on how database connection configuration can be specified. #

[credentials.ymlの設定]

db: database: アプリ名 username: root password: 設定したPW socket: /var/lib/mysql/mysql.sock
  • こちらのcredentials.ymlの環境変数の設定を上部のdatabase.ymlに設定しております。
  • 開発環境がRails6に当たるため、
[ec2-user@ip-172-31-23-189 <アプリ名>]$ rails db:create RAILS_ENV=production Created database '<データベース名>'

とは異なる方法で実装しております。

[Gemfile]

group :production, :staging do gem 'unicorn' end

[config/unicorn.rb]

app_path = File.expand_path('../../', __FILE__) worker_processes 1 working_directory app_path pid "#{app_path}/tmp/pids/unicorn.pid" listen 3000 stderr_path "#{app_path}/log/unicorn.stderr.log" stdout_path "#{app_path}/log/unicorn.stdout.log" timeout 60 preload_app true GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true check_client_connection false run_once = true before_fork do |server, worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! if run_once run_once = false # prevent from firing again end old_pid = "#{server.config[:pid]}.oldbin" if File.exist?(old_pid) && server.pid != old_pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH => e logger.error e end end end after_fork do |_server, _worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection end

試したこと

  • libmysqlclient.so.18: cannot open shared object file: No such file or directoryとあるので、一度EC2上にあるmysql2をアンインストールした後に、gem install mysql2で再度導入してみました。

  • EC2上で再度bundleを行い、$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D を実行したがエラー内容は変わらなかった。

  • mysqlが起動していないことを考えましたが、sudo systemctl status mysqldで確認してみたところしっかり起動していました。

  • unicornの状態を確認してみましたが、停止の状態です。

ps aux | grep unicorn

[ec2-user@ip-xxxxxxxx locat]$ ps aux | grep unicorn ec2-user 25272 0.0 0.0 119436 960 pts/0 S+ 10:15 0:00 grep --color=auto unicorn
  • bundle exec gem list | grep unicornでバージョンを確認したところunicorn (6.0.0)でした。

補足情報(FW/ツールのバージョンなど)

開発環境

  • rubymine
  • ruby(3.0.1)
  • Ruby on rails (6.1.3.1)
  • unicorn (6.0.0)

Github

参考記事

Qiita
Qitta
teratail
参考ブログ
teratail

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yu_1985

2021/07/15 13:49

mysql-develはインストールしてみましたか?
yuu0000

2021/07/16 04:05

ありがとうございます! 無事解決できました。
guest

回答1

0

自己解決

mysql-develが正しくインストールされていないことが原因でした

投稿2021/07/16 04:07

yuu0000

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問