subaru-hello / Zeroken

一軒目に飲むお酒と飲む順番を提供するアプリ
5 stars 0 forks source link

Unknown MySQL server host 'db' (-2) (ActiveRecord::ConnectionNotEstablished) #138

Closed subaru-hello closed 1 year ago

subaru-hello commented 1 year ago

RailsとMysqlのソケット同士がうまく接続されない

本来Docker imageを使用したときに両者はTCP接続をされている。 そのため、ソケットを見るような処理は走らない。 だが、docker-compose.ymlの記述に誤りがあった場合はソケットを見るような仕組みになっている。

subaru-hello commented 1 year ago

docker-compose.yml


version: '3'
services:
  web:
    build: .
    volumes:
      - .:/Zeroken
       # 1. exclude volumes
      - /app/vendor
      - /app/tmp
      - /app/log
      - /app/.git
    ports:
      - "3000:3000"
    environment:
      - MYSQL_HOST=db
      - MYSQL_PASSWORD=password
    links:
      - db
    depends_on:
      - db
    stdin_open: true
    tty: true

  db:
    image: mysql/mysql-server:5.7
    restart: always
    platform: linux/amd64
    environment:
      MYSQL_ROOT_PASSWORD:
      MYSQL_DATABASE: zeroken_development
      MYSQL_USER: root
      MYSQL_PASSWORD: password
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
        #command: mysqld --innodb_use_native_aio=0
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - '43306:3306'
    tty: true

volumes:
  mysql-data:

config/database.yml

# MySQL.  Versions 4.1 and 5.0 are recommended.
# 
# 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:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html

default: &default
  adapter: mysql2
  pool: 5 
  timeout: 5000
  encoding: utf8
  host: <%= ENV['MYSQL_HOST'] || 'localhost' %>
  username: <%= ENV['MYSQL_USERNAME'] || 'root' %>
  password: <%= ENV['MYSQL_PASSWORD'] || '' %>
  port: 3006

development:
  <<: *default
  database: zeroken_development

test:
  <<: *default
  database: zeroken_test

production:
  <<: *default
  database