前提・実現したいこと
rails で mysql を使用したいのですが、
database.ymlの接続情報defaultにて、
password: <%= ENV["環境変数名"] %>と記述しても、
環境変数名の値が取得できていないようで、
[Error] Access denied for user 'root'@'localhost' (using password: NO)が表示されてしまいます。
発生している問題・エラーメッセージ
Access denied for user 'root'@'localhost' (using password: NO) Extracted source (around line #90): 88 conn_attrs = parse_connect_attrs(opts[:connect_attrs]) 89 90 connect user, pass, host, port, database, socket, flags, conn_attrs 91 end 92 93 def parse_ssl_mode(mode)
該当のソースコード
database.yml
1# MySQL. Versions 5.5.8 and up are supported. 2# 3# Install the MySQL driver 4# gem install mysql2 5# 6# Ensure the MySQL gem is defined in your Gemfile 7# gem 'mysql2' 8# 9# And be sure to use new-style password hashing: 10# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html 11# 12default: &default 13 adapter: mysql2 14 encoding: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: <%= ENV["TEST_PASS"] %> 18 host: localhost 19 20development: 21 <<: *default 22 database: test_app_development 23 24# Warning: The database defined as "test" will be erased and 25# re-generated from your development database when you run "rake". 26# Do not set this db to the same as development or production. 27test: 28 <<: *default 29 database: test_app_test 30 31# As with config/credentials.yml, you never want to store sensitive information, 32# like your database password, in your source code. If your source code is 33# ever seen by anyone, they now have access to your database. 34# 35# Instead, provide the password as a unix environment variable when you boot 36# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 37# for a full rundown on how to provide these environment variables in a 38# production deployment. 39# 40# On Heroku and other platform providers, you may have a full connection URL 41# available as an environment variable. For example: 42# 43# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 44# 45# You can use this database configuration with: 46# 47# production: 48# url: <%= ENV['DATABASE_URL'] %> 49# 50production: 51 <<: *default 52 database: test_app_production 53 username: test_app 54 password: <%= ENV['TEST_APP_DATABASE_PASSWORD'] %> 55
試したこと
database.ymlのpassword: に直接パスワードを入力した場合は、
エラーが発生せずにデータベースにアクセスできることも確認しております。
GitBash上でENVコマンドを実行して指定の環境変数が正しく設定されているか確認しましたが、
指定の環境変数は設定されており、指定した値もMySQLサーバーのパスワードと一致している事を確認済みです。
補足情報(FW/ツールのバージョンなど)
ruby 2.6.5p114 (2019-10-01 revision 67812) [x64-mingw32]
Rails 6.0.2.1
MySQL Server 8.0.18
回答1件
あなたの回答
tips
プレビュー