LoginSignup
4
7

More than 3 years have passed since last update.

【本番環境】rails db:create でAccess denied for user 'rails'@'localhost' (using password: YES)というエラーが出る問題の対処法

Last updated at Posted at 2020-06-26

環境

terminal

OS
Amazon Linux 2 AMI

-> % mysql -v
mysql  Ver 14.14 Distrib 5.7.29, for osx10.15 (x86_64) using  
EditLine wrapper

-> % rails -v
Rails 5.2.4.3

-> % ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin19]

状況

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

ローカルで作成したDBを本番環境に反映させるために
rails:db createrails:db migrate
を行ったのですがエラーが出てしまいました・・・

エラー文

terminal
[ec2-user@ip-**** アプリのディレクトリ ]$ rails db:create RAILS_ENV=production

Access denied for user 'rails'@'localhost' (using password: YES)
Couldn't create 'アプリ名_production' database. Please check your configuration.
rails aborted!
Mysql2::Error: Access denied for user 'rails'@'localhost' (using password: YES)
/var/アプリ名/rails/アプリ名/bin/rails:9:in `<top (required)>'
/var/アプリ名/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はデフォルトのrootユーザーではなく、新たにrailsという名前のユーザーを作成し、
このユーザーに権限を与えて使用しています。

mysql

mysql>  SELECT User, Host FROM mysql.user;
+-----------+-----------+
| User      | Host      |
+-----------+-----------+
| root      | %         |
| mysql.sys | localhost |
| rails     | localhost |
| rdsadmin  | localhost |
+-----------+-----------+
4 rows in set (0.01 sec)
config/database.yml

default: &default
  adapter: mysql2
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: rails
  password: 
  host: localhost

development:
  <<: *default
  database: アプリ名_development
  username: rails
  password:  


# 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: アプリ名_test
  username: rails
  password: 

production:
  <<: *default
  database: アプリ名_production
  username: rails
  password: 

エラーの原因

注目すべきは
Access denied for user 'rails'@'localhost' (using password: YES)
でした。

これはrailsユーザーでlocalhostに接続しようとしているけど、接続先が見つかりません
というような意味合いのようです。

開発環境であればhttp://localhost:3000で接続するので問題ありませんが
本番環境だとRDSに接続する必要があるため、RDSのエンドポイントを記載しないとエラーになるようです。

解決策

config/database.ymlファイルのhostを下記の様に書き換えます。

config/database.yml

default: &default
  adapter: mysql2
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: rails
  password: 
  host: RDSのエンドポイント

development:
  <<: *default
  database: アプリ名_development
  username: rails
  password:  


# 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: アプリ名_test
  username: rails
  password: 

production:
  <<: *default
  database: アプリ名_production
  username: rails
  host: RDSのエンドポイント
  password: 

これで無事にrails:db createが成功しました。

補足

今回はrailsというユーザーを新しく作成しましたが、
rootユーザーで試しても、問題なくデータベースを作成することができました。

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