お世話になっております。
下記の件、知見がある方がいらっしゃいましたらご教示お願いします。
起きている問題
AWS
環境でnginx
、unicorn
、mysql
をdocker
コンテナで構築しdocker-compose run app rake db:create
実行時にMysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")>
が起きました。
$ docker-compose run app rake db:create
Starting coffee_app_db_1 ... done
#<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"coffee_app_development", "pool"=>5, "username"=>"karirin", "password"=>"karirin3948", "host"=>"localhost"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'coffee_app_development'
#<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"coffee_app_test", "pool"=>5, "username"=>"karirin", "password"=>"karirin3948", "host"=>"localhost"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'coffee_app_test'
確認したこと
■mysqld.sock確認
$ touch /var/run/mysqld/mysqld.sock
[mysqld]$ ls
mysqld.sock
mysqld.sock
がないというエラーだったので生成しましたが、解決しませんでした。
■mysqld起動確認
$ ps ax | grep mysqld
5180 pts/0 S+ 0:00 grep --color=auto mysqld
30674 ? Ssl 1:03 /usr/sbin/mysqld
30892 ? Ssl 1:13 mysqld
mysqldは起動していることを確認しました。
■sudo mysql.server start
$ sudo mysql.server start
[sudo] ryouya のパスワード:
sudo: mysql.server: コマンドが見つかりません
sudo mysql.server start
を実行したところ、コマンドが見つからないとでました。
$ mysql --version
mysql Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)
mysql --version
では正常にバージョンが表示されました。
関連ファイル
Dockerfile(Rails)
FROM ruby:2.5.1
RUN apt-get update -qq && \
apt-get install -y apt-utils \
build-essential \
libpq-dev \
nodejs \
default-mysql-client
RUN mkdir /coffee_app
WORKDIR /coffee_app
ADD Gemfile /coffee_app/Gemfile
ADD Gemfile.lock /coffee_app/Gemfile.lock
RUN bundle install -j4
ADD . /coffee_app
EXPOSE 3000
Dockerfile(mysql)
FROM mysql:8.0.17
RUN apt-get update && \
apt-get install -y apt-utils \
locales && \
rm -rf /var/lib/apt/lists/* && \
echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \
locale-gen ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
ADD ./docker/mysql/charset.cnf /etc/mysql/conf.d/charset.cnf
Dockerfile(nginx)
FROM nginx:1.12.2
RUN apt-get update && \
apt-get install -y apt-utils \
locales && \
echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \
locale-gen ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
ADD ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
ADD ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf
docker-compose.yml
version: '2'
services:
app:
build:
context: .
dockerfile: ./docker/rails/Dockerfile
command: bundle exec unicorn -p 3000 -c /app/config/unicorn.rb
# command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- '3000:3000'
volumes:
- /var/tmp
- .:/coffee_app
depends_on:
- db
extends:
file: ./docker/mysql/password.yml
service: password
db:
build:
context: .
dockerfile: ./docker/mysql/Dockerfile
ports:
- '3306:3306'
volumes:
- db_data:/var/lib/mysql
extends:
file: ./docker/mysql/password.yml
service: password
nginx:
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
ports:
- '80:80'
volumes:
- coffee_app
volumes:
db_data:
database.yml
development:
adapter: mysql2
encoding: utf8
database: coffee_app_development
pool: 5
username: karirin
password: karirin3948
host: localhost
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: coffee_app_test
pool: 5
username: karirin
password: karirin3948
host: localhost
production:
adapter: mysql2
database: coffee_app_production
host: <%= ENV['DB_HOSTNAME'] %>
encoding: utf8
username: root
reconnect: false
pool: 5
password: <%= ENV['DB_PASSWORD'] %>
環境
ruby 2.5.1
rails 5.1.6
docker version 19.03.6
docker-compose version 1.24.0