LoginSignup
1
1

More than 1 year has passed since last update.

【本番環境】【備忘録】rails db:create RAILS_ENV=production でrails aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: NO)とエラーが出る問題の対処法

Last updated at Posted at 2021-05-04

AWSのサービスである、EC2とRDSを用いてローカルで作成したRailsのアプリをデプロイしようと作業を進めています。

https://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1
上記記事に従って
rails db:create RAILS_ENV=production
rails db:migrate RAILS_ENV=production
を行ったのですがタイトルのエラーが発生・・・

エラー文

rails aborted!
Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: NO)
/var/www/rails/アプリ名/bin/rails:9:in `<top (required)>'
/var/www/rails/アプリ名/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

試したこと

①$ mysql -u root -p の実行

MySQL
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

特に問題なく入れた。

②config/database.ymlを開き、パスワードを記載
いつも使っているのがVSコードなので、VSコードにてdatabase.ymlを開く。
本番環境用のパスワードの欄が空欄だったため、記載する。
スクリーンショット 2021-05-05 1.05.06.png
しかし全く同じエラー。どうして・・・・・?

結論

ローカル環境ではなく、「本番環境(EC2)」にてconfig/database.ymlを開き、パスワードを記載すると解決します!
(ローカル環境にてdatabase.ymlを編集していたので、本番環境への影響がなく、全く意味のないことをしていたようです。。)

手順(注意:すべて本番環境の中で実行する)

asami|アプリ名
$ vi config/database.yml
database.yml
production:
  <<: *default
  database: アプリ名_production
  username: root
  password: XXXXX # ←ここに記述 

無事に通りました!
本当に単純すぎる間違いでかなり時間をロスしました・・・・・

1
1
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
1
1