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

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

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

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

MySQL

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

Ruby on Rails

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

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

Q&A

解決済

1回答

1641閲覧

【EC2】unicorn起動時にエラーが発生してしまう

yastinbieber

総合スコア49

unicorn

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

MySQL

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

Ruby on Rails

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

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

0グッド

0クリップ

投稿2021/01/17 06:57

編集2021/01/19 03:25

前提・実現したいこと

Cloud9にて作成したRailsアプリをEC2経由でAWSにデプロイしようと試みております。

途中unicornをインストールしたのですが、
起動時にエラーが吐かれてしまい自身で調べても解決しなかったためご質問させていただきたいです。

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

console

1[ec2-user@ip-172-31-36-245 git_toreka]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D 2master failed to start, check stderr log for details

エラー場所を確認

E, [2021-01-12T12:51:35.080644 #14762] ERROR -- : Access denied for user 'toreka'@'localhost' (using password: NO) (Mysql2::Error::ConnectionError) . . . E, [2021-01-13T12:26:53.188705 #27496] ERROR -- : Your password has expired. To log in you must change it using a client that supports expired passwords. (Mysql2::Error) . . . E, [2021-01-14T04:52:37.948095 #1474] ERROR -- : Your password has expired. To log in you must change it using a client that supports expired passwords. (Mysql2::Error) . . . E, [2021-01-14T05:58:16.048889 #2037] ERROR -- : Your password has expired. To log in you must change it using a client that supports expired passwords. (Mysql2::Error) . . .

このような形でMysqlにおけるエラーが複数記載されている形です。

試したこと

1つ目にあるAccess denied for user 'toreka'@'localhost' (using password: NO)の箇所を直すことができれば他エラーも解消するのではないかと考えたためその点に絞って考えてみました。

「パスワードがないから開かない」とあるようなのでパスワードの設定を確認して見ました。

[ec2-user@ip-172-31-36-245 git_toreka]$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

mysqlには実際に入ることができます。
パスワードの設定はできています。

database.ymlに記載しているパスワードの記載が正しいかも確認してみました。

##database.yml . . . 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] %>
##credentials.yml db: database: dbname username: root password: xxxxxxxx socket: /var/lib/mysql/mysql.sock

パスワードも入力しておりました。

config/unicorn.rbの中身も念のため貼っておきます。

##config/unicorn.rb [ec2-user@ip-172-31-36-245 git_toreka]$ cat config/unicorn.rb app_path = File.expand_path('../../', __FILE__) #アプリケーションサーバの性能を決定する worker_processes 1 #アプリケーションの設置されているディレクトリを指定 working_directory app_path #Unicornの起動に必要なファイルの設置場所を指定 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" #Railsアプリケーションの応答を待つ上限時間を設定 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

上記確認をしたり修正等も加えてみたりしましたがエラー内容が改善されません。
恐れ入りますが解消方法お分かりの方ご教示いただけますと幸いです。

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

mysql Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL)
Rails 5.2.4.4
ruby 2.5.1
unicorn v5.4.1
nginx version: nginx/1.18.0

追記(0119更新)

tarminal

1ec2-user:~/environment $ cd ~/.ssh 2ec2-user:~/.ssh $ ssh -i torekabodymake_keypair.pem ec2-user@54.249.173.184 3Last login: Mon Jan 18 11:09:48 2021 from 18.183.138.44 4 5 __| __|_ ) 6 _| ( / Amazon Linux 2 AMI 7 ___|\___|___| 8 9https://aws.amazon.com/amazon-linux-2/ 10[ec2-user@ip-172-31-36-245 ~]$ cd /var/www/git_toreka 11[ec2-user@ip-172-31-36-245 git_toreka]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D 12master failed to start, check stderr log for details
[ec2-user@ip-172-31-36-245 git_toreka]$ less log/unicorn.stderr.log I, [2021-01-19T02:44:32.809591 #29445] INFO -- : Refreshing Gem list NoMethodError: Cannot load database configuration: undefined method `[]' for nil:NilClass (erb):52:in `<main>' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/2.5.0/erb.rb:876:in `eval' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/2.5.0/erb.rb:876:in `result' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.4.4/lib/rails/application/configuration.rb:172:in `database_con figuration' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.4/lib/active_record/railtie.rb:133:in `block (2 levels) i n <class:Railtie>' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:71:in `instance_ eval' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:62:in `with_exec ution_control' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:67:in `execute_h ook' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:51:in `each' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.4.4/lib/active_support/lazy_load_hooks.rb:51:in `run_load_ hooks' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.
[ec2-user@ip-172-31-36-245 git_toreka]$ rails db:migrate RAILS_ENV=production rails aborted! NoMethodError: Cannot load database configuration: undefined method `[]' for nil:NilClass (erb):52:in `<main>' /var/www/git_toreka/app/models/application_record.rb:1:in `<top (required)>' /var/www/git_toreka/app/models/user.rb:1:in `<top (required)>' /var/www/git_toreka/config/routes.rb:4:in `block in <top (required)>' /var/www/git_toreka/config/routes.rb:1:in `<top (required)>' /var/www/git_toreka/config/environment.rb:5:in `<top (required)>' /var/www/git_toreka/bin/rails:9:in `<top (required)>' /var/www/git_toreka/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: NoMethodError: undefined method `[]' for nil:NilClass (erb):52:in `<main>' /var/www/git_toreka/app/models/application_record.rb:1:in `<top (required)>' /var/www/git_toreka/app/models/user.rb:1:in `<top (required)>' /var/www/git_toreka/config/routes.rb:4:in `block in <top (required)>' /var/www/git_toreka/config/routes.rb:1:in `<top (required)>' /var/www/git_toreka/config/environment.rb:5:in `<top (required)>' /var/www/git_toreka/bin/rails:9:in `<top (required)>' /var/www/git_toreka/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate => db:load_config => environment (See full trace by running task with --trace) [ec2-user@ip-172-31-36-245 git_toreka]$

なおdatabaseはcreateされています

mysql> SHOW DATABASES LIKE 'git_toreka'; +-----------------------+ | Database (git_toreka) | +-----------------------+ | git_toreka | +-----------------------+ 1 row in set (0.01 sec) mysql>

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

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

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

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

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

guest

回答1

0

ベストアンサー

Access denied for user 'toreka'@'localhost' (using password: NO) (Mysql2::Error::ConnectionError)

とエラーメッセージに出ていますが、使用しているユーザがdatabase.ymlの記載と一致しません。
そのエラーを元にするなら、どこかしら設定が誤っているか、誤った情報を読み取って接続しようとしていることになります。
とりあえずはdatabase.ymlの記載を端折らずに全部貼ってください。

また、stderr.logの抜粋箇所が5日ほど前のものなのですが、それは本当に今回unicornを起動しようとしたときのエラーですか?

投稿2021/01/17 17:00

yu_1985

総合スコア7469

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

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

yastinbieber

2021/01/18 02:10

いつもありがとうございます。 database.yml の記載を貼らせていただきました。 ご確認いただけますと幸いです。 またstderr.logに関してですが再度`less log/unicorn.stderr.log`で確認したところやはり5日ほど前の記述が見受けられます。 そちらも添付させていただきました。 お忙しいところ恐縮ですがご確認のほどよろしくお願いいたします。
yu_1985

2021/01/18 02:42

もう一度unicornを起動しようとしたらstderr.logになにか追記されますか?
yastinbieber

2021/01/18 06:23 編集

unicornを起動しようとすると依然 master failed to start, check stderr log for details がでます。 再度 less log/unicorn.stderr.log を実行するとかなり長い文が出てきます。 (勘違いしておりましたがログなので過去に解消(解決)したものも記載されているんですかね) その中からerrorという文字を探してみたところ E, [2021-01-12T12:51:35.080644 #14762] ERROR -- : Access denied for user 'toreka'@'localhost' (using password: NO) (Mysql2::Error::Connectio nError) E, [2021-01-14T05:58:16.048889 #2037] ERROR -- : Your password has expired. To log in you must change it using a client that supports expired passwords. (Mysql2::Error) I, [2021-01-18T05:08:38.505618 #21832] INFO -- : Refreshing Gem list RuntimeError: YAML syntax error occurred while parsing /var/www/git_toreka/config/database.yml. Please note that YAML must be consistently i ndented using spaces. Tabs are not allowed. Error: (<unknown>): did not find expected key while parsing a block mapping at line 12 column 1 /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.4.4/lib/rails/application/configuration.rb:190:in `rescue in da tabase_configuration' このように書かれたエラー文が発見できます。
yu_1985

2021/01/18 08:56

ご自分でお気付きの通り、ログはあくまでも記録なので過去のものも含めて記述されています。 なので、タイムスタンプを見て直近のコマンド実行以後に出たものだけを見てください。
yu_1985

2021/01/18 08:57

> RuntimeError: YAML syntax error occurred while parsing /var/www/git_toreka/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. database.ymlの記載でYAMLの書式が誤ってるそうなので、まずはそれを直してください。 内容を見るに、tabでインデントしてると思われますが、YAMLではtabを使ったインデントはできません。
yastinbieber

2021/01/18 12:03

詳しくありがとうございます。 yamlについて理解していなかったので参考になりました。 実際にtab箇所をspaceにしたところ上記のエラーは出なくなりました。 しかしながら次のエラーが吐かれてしまっており、 ``` I, [2021-01-18T11:50:54.555648 #23909] INFO -- : Refreshing Gem list NoMethodError: Cannot load database configuration: undefined method `[]' for nil:NilClass ``` 自身でも再度エラー文を検索にかけてみているのですが確信につながらずでして、 再度ご意見をいただけますと幸いです。
yu_1985

2021/01/18 14:20 編集

INFOなのでそれ単体はエラーではありません。ログは一部だけを見ずもう少し広範囲を見てください。 コマンド実行時には何もメッセージは出ませんか?
yastinbieber

2021/01/19 03:18

度々のご回答ありがとうございます。 該当しそうな部分を追記させていただきました。 不要な部分もあるかもしれませんがご確認いただけますと幸いです。 ご迷惑おかけします。
yu_1985

2021/01/19 03:29

> NoMethodError: Cannot load database configuration: このあたりのメッセージは調べてみましたか? いくつか可能性はありそうですが、データベースの接続設定が読み込めてないと言ってくれているのでそのあたりを調べるべきでしょう。 恐らくこの辺が解決策じゃないかと思いますが…。 https://qiita.com/yokoto/items/e7c431fc66c5b1815d58#%E8%A7%A3%E6%B1%BA%E7%AD%963-railsapplicationcredentialshogefuga%E3%82%92%E5%91%BC%E3%81%B0%E3%81%AA%E3%81%84
yastinbieber

2021/01/19 11:34

ありがとうございます。 結論エラー解消され [ec2-user@ip-172-31-36-245 git_toreka]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D のコマンドを打ってもmaster failed to start, check stderr log for details の表記は出なくなりました。 その後password関連のエラーが吐かれてしまいましたが自力で解消することができました。 今回も細かくありがとうございました。 またご質問させていただくこともあるかと思いますが引き続きよろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問