Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
docker-compose exec web rails db:createでエラーにはまる
調べてみましたが、なかなか抜け出せなく困っています。
docker-compose upをしたときに、
おなじみの「Yay! You’re on Rails!」が
表示されるようにするにはどうしたらいいでしょうか?
「rails s」をすると、「Yay! You’re on Rails!」が表示できます。
「docker-compose up」をすると、
こちらのエラーページになってしまいます。
こちら https://teratail.com/questions/283808
を参考にdatabase.ymlで
host:localhost → host:db にすると、
以下のエラーになります。
Mysql2::Error::ConnectionError: Access denied for user 'root'@'192.168.0.3' (using password: NO)
尚、下記記事を参考に環境構築を行っています。
エラー抜粋
ーーーーーーーーーーーーー
$ docker-compose exec web rails db:create
ーーーーーーーーーーーーー
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
発生している問題・エラー全文
ーーーーーーーーーーーーー
$ docker-compose exec web rails db:create
ーーーーーーーーーーーーー
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
Couldn't create 'mysql2' database. Please check your configuration.
rails aborted!
ActiveRecord::ConnectionNotEstablished: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
/app_name/bin/rails:5:in `<top (required)>'
/app_name/bin/spring:10:in `block in <top (required)>'
/app_name/bin/spring:7:in `tap'
/app_name/bin/spring:7:in `<top (required)>'
Caused by:
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
/app_name/bin/rails:5:in `<top (required)>'
/app_name/bin/spring:10:in `block in <top (required)>'
/app_name/bin/spring:7:in `tap'
/app_name/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
ERROR: 1
# 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: 5
username: root
password:
host: localhost
development:
<<: *default
database: test_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: test_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 or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: test_app_production
username: test_app
password: <%= ENV['TEST_APP_DATABASE_PASSWORD'] %>