LoginSignup
0
0

More than 3 years have passed since last update.

エラー解決に関するロードマップ1

Posted at

localhost:3000にアクセスし、 ブラウザサーバーを起動した際に以下のエラーが発生しました。

ActiveRecord::ConnectionNotEstablished
No connection pool with 'primary' found.

このエラーの解決の筋道を備忘録として残します。

前提

本エラーはajax_appというリポジトリのアプリケーションを作成するにあたり、
chat_appというリポジトリを複製、編集を行いajax_appを作成しております。

内容

1.エラー文の理解

ActiveRecord::ConnectionNotEstablished
No connection pool with 'primary' found.
→これは何を言っているのかを理解する
 Googleで検索、以下の投稿から
 データベースとの繋がりが形成されていない
 ことが想定されます

2.エラーの箇所の想定(ここはある程度経験が必要?)

データベースと接続していない原因は
migrateファイル
datebase.yaml
のどちらかと想定される。

ちなみに、
migrateファイル はデータベース内のテーブルを取り扱う設計図。
datebase.yaml はデータベースそのものを扱う設計図。
という認識で良いかと思います。

migrateファイルの基本的なエラーはrails db:migrateされていない事による

ActiveRecord::PendingMigrationError

が一般的。今回は当該エラーではなかったので、
datebase.yaml
がエラーの原因と想定。

3.ターミナルのエラー文の理解

datebase.ymlに原因があることがわかったが、datebeseの内容は正しい。(テキスト通り)

datebase.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: ajax_app_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: ajax_app_test

# 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 as a unix environment variable when you boot
# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: ajax_app_production
  username: ajax_app
  password: <%= ENV['FIRST_APP_DATABASE_PASSWORD'] %>

そこで、データベースの状況を確認するため rails db:create:status を実行。

以下のエラーが表示された。

Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB.
rails aborted!

Google翻訳を使用し、日本語に訳します。

Railsは、database.ymlから複数のデータベースを使用しているかどうかを推測できず、非プライマリデータベースのタスクを生成できません。この機能を使用する場合は、ERBを簡略化してください。

なお、
エラーが起こった際に検索、翻訳をを行うキーワード
・couldn’t
・error
・false など
エラーにまつわる文言を含む文を検索することがよいでしょう

4.エラーの箇所の想定

以上よりデータベース(sequel pro)を確認すると、
ajax_app_development データベースが存在しないことが判明。
データベースが存在しないため、アクセスに関するエラーが出ていたことがわかった。

ajax_app_development データベースが存在しないことが判明。

原因

今回、first_appという一度作成済みのファイルを編集して、ajax_appと名前を変えて使用しておりました。
本来、datebase.yml内の first_app と明記されている箇所を ajax_app と書き換えて、rails db:create
を実行する必要があります。
おそらく、該当箇所をfirst_appに書き換える前に rails db:create を実行した後に書き換えを行ったため、ajax_appのデータベースは作成されず、接続しているdbはfirst_app_developmentのままであったことが原因だったと考えられます。
つまり、ajax_appのdatebase.ymlとfirst_appのdatebase.ymlがどちらともfirst_app_developmentデータベースに接続していたため、エラーが生じました。

修正

以上の原因から
first_appデータベースを削除 rails db drop
ajax_appデータベースの生成 rails db create

以上で新たに ajax_app のデータベースを作成し、localhost:3000にアクセス

→アクセスできました。

総括

エラー解決までの流れ

・エラー文の解読

・問題箇所の想定

・エラー文の解読

問題箇所の想定
 以上の繰り返し

エラー文は以下のキーワードを含む文に注目

・couldn’t
・error
・false
google翻訳にかけて意味を理解して良い。

以上を心がけて行うと、理解が深まると思います。

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