前提・実現したいこと
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '172.18.0.3' (115) のエラーを解決したい
Docker環境でRuby on RailsのDBを作成する為に
docker-compose run web rails db:create ```と入力すると上記のエラーが発生しました。 ## 発生している問題・エラーメッセージ
Creating notworry_web_run ... done
Can't connect to MySQL server on '172.18.0.3' (115)
Couldn't create 'app_development' database. Please check your configuration.
rails aborted!
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '172.18.0.3' (115)
/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.7.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in require' /usr/local/bundle/gems/bootsnap-1.7.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in
block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.7.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in register' /usr/local/bundle/gems/bootsnap-1.7.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in
require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.7.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in require' /app/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)>' /app/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)
## 試したこと 1.docker-compose.yamlファイルのpasswordを設定
version: '3'
services:
web:
build: ./api
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- '3000:3000'
depends_on:
- db
volumes:
- ./api:/app
- bundle:/usr/local/bundle
tty: true
stdin_open: true
db:
image: mysql:5.7
volumes:
- mysql_data:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD: 01110111
ports:
- '3306:3306'
front:
build: ./front
volumes:
- ./front:/app
- node_modules:/app/node_modules
ports:
- '8080:8080'
tty: true
stdin_open: true
command: npm run serve
volumes:
mysql_data:
bundle:
node_modules:
2.config/database.ymlのpassword、hostを変更
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: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
- password:
- password: 01110111
- host: localhost
- host: 172.18.0.3
development:
<<: *default
database: 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: 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: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
## 補足情報(FW/ツールのバージョンなど) OS Catalina バージョン10.15.7 MySQL 5.7 参考にしたサイト: [https://qiita.com/Kyou13/items/be9cdc10c54d39cded15](https://qiita.com/Kyou13/items/be9cdc10c54d39cded15) ## 5時間ほど調べてみましたが自力で解決できませんでした。 よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/05 09:34 編集
2021/02/05 10:52
2021/02/05 12:03
2021/02/05 12:26