9
10

More than 3 years have passed since last update.

docker + mysql が接続できない /Can't connect to local MySQL server through socket 'var/run/mysqld/mysqld.sock' (2)

Last updated at Posted at 2020-11-02

概要

ポートフォリオ制作真っ最中です。
mysqlとのアクセスの際に躓いてしまったので、備忘録の意味も含めて書きます。
初心者で同じところで躓いてしまった方に届けば嬉しいです。
※誤りなどあれば、コメントくださると嬉しいです。

今までのフロー

Can't connect to local MySQL server through socket 'var/run/mysqld/mysqld.sock' (2)

解決法

①docker-compose.ymlでユーザーとパスワードを設定
②同様のユーザーとパスワードをdatabase.ymlで設定

原因到達までの流れ

$docker exec -it <containerID> /bin/bashでdbコンテナに入った後、
$mysql -u rootを実施した後にmysqlに問題なく入れた。
なのでサーバー自体に問題があるわけではなかった。
ユーザー設定に問題あると思い、docker-compose.ymlとdatabase.ymlにユーザーとパスワードを追記

その後、http://localhost:port/ でアクセスしたが、以下のエラーが出た。
Access denied for user 'root'@'172.18.0.5' (using password: YES)
前に docker-compose up をした際にvolumeにキャッシュが残っていたぽかったので、volume削除。
再度 docker-compose up した後に問題なく入れた。

各種ファイル

database.yml
# 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
  host: db

development:
  <<: *default

# 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

# 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
docker-compose.yml

version: "3"

#dbとbackendのコンテナを作成
services:
  db:
    container_name: db
    #DockerHubにアップされているイメージをプル
    image: mysql:5.7.30
    #環境変数設定
    environment: 
      TZ: Asia/Tokyo #Dockerfileと同様にTimeZoneを設定
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: password
    #マウントする設定ファイルのパスを指定
    volumes:
      - mysql_data:/var/lib/mysql
    #ポートを設定
    ports:
      - 3316:3306
    #IPアドレスを設定
    networks:
      app_net: #後述のapp_netネットワーク空間の以下のIPを使用するよう設定
        ipv4_address: '172.20.0.2'

  backend:
    container_name: backend
    #ComposeFileを実行し、ビルドされるときのpathを指定
    build: ./backend/
    image: backend
    #gemを動かしてサーバー立ち上げし、0.0.0.0にバインドしてフルアクセスできるようにする
    command: bundle exec rails server -b 0.0.0.0
    #コンテナの常時起動
    tty: true
    #入力のインターフェースを起動 docker run -itのitと同義
    stdin_open: true

    #バックエンドはcashe, temp, log, gitにもマウント
    volumes:
      - ./backend:/app:cached
      - bundle_data:/usr/local/bundle:cached
      - /app/vendor
      - /app/tmp
      - /app/log
      - /app/.git
    environment:
      TZ: Asia/Tokyo

    #起動順を制御, dbの後
    depends_on:
      - db
    ports:
      - 5000:3000 # ポートフォワード
    networks:
      app_net:
        ipv4_address: '172.20.0.3'


#独自のネットワークを設定
networks:
  #app_netというネットワーク空間を定義
  app_net:
    driver: bridge #bridgeネットワークへ接続
    ipam: #IP設定
      driver: default
      config:
        - subnet: 172.20.0.0/24 #Subnetを定義

#2つのvolumeを定義
volumes:
  mysql_data:
  bundle_data:

9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10