前提・実現したいこと
現在アプリケーションを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)
参考記事
回答1件
あなたの回答
tips
プレビュー