前提・実現したいこと
docker-compose run web rails db:create
を実行したら
Creating coffee_passport_web_run ... done Access denied for user 'root'@'172.23.0.4' (using password: NO) Couldn't create 'coffee_passport_development' database. Please check your configuration.
と、エラーが出ました
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: 生のパスワード ports: - '3306:3306' command: --default-authentication-plugin=mysql_native_password volumes: - ./tmp/db:/var/lib/mysql web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/coffee_passport ports: - "3000:3000" depends_on: - db
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: <%= ENV['MYSQL_PASSWORD'] %> socket: /tmp/mysql.sock host: db development: <<: *default database: coffee_passport_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: coffee_passport_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: coffee_passport_production username: root password: <%= ENV['MYSQL_PASSWORD'] %> socket: /var/lib/mysql/mysql.sock
試したこと
MYSQL_ROOT_PASSWORD: <%= ENV['DATABASE_PASSWORD'] %>
を上記の通りに追記したり、
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
をdatabase.ymlに追加したりしました。
DARABASE_PASSWORDをdbコンテナの環境変数にも設定しようと
docker-compose run web -e DATABASE_PASSWORD=hogehoge
を実行したら
/usr/bin/entrypoint.sh: line 8: exec: -e: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
とエラーが出ました。
db:
image: mysql:5.7
に変更したら
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
とエラーが出ました。
docker-compose.ymlの
MYSQL_ROOT_PASSWORD: 生のパスワード
を打ち込んで
docker-compose run web rails db:create
を実行しても
Access denied for user 'root'@'172.26.0.4' (using password: NO)
とエラーが出ました
docker-compose down
rm -rf tmp/db
docker-compose up -d --build
と打ち込んでも
エラー内容が
soichirohara@SoichironoMBP coffee_passport % docker-compose run web rails db:create Creating coffee_passport_web_run ... done Access denied for user 'root'@'172.21.0.4' (using password: NO) Couldn't create 'coffee_passport_development' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.21.0.4' (using password: NO) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:126:in `create' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `each' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:184:in `create_current' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require' /coffee_passport/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /coffee_passport/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で打ったときにそのパスワードを打ったらちゃんとログイン出来なたので、
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。