[RAILS] docker + mysql can't connect / Can't connect to local MySQL server through socket'var / run / mysqld / mysqld.sock' (2)

Overview

I'm in the middle of creating a portfolio. I stumbled when accessing mysql, so I will write it including the meaning of the memorandum. I would be happy if it is delivered to beginners who have stumbled in the same place.

Flow so far

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

Solution

① Set user and password in docker-compose.yml ② Set the same user and password in database.yml

Flow to reach the cause

After entering the db container with $ docker exec -it <containerID> / bin / bash After executing $ mysql -u root, I entered mysql without any problem. So there wasn't a problem with the server itself. I think there is a problem with the user settings, so add the user and password to docker-compose.yml and database.yml

After that, I accessed it via http: // localhost: port /, but I got the following error. Access denied for user 'root'@'172.18.0.5' (using password: YES) When I did docker-compose up before, it seemed that the cache remained in volume, so I deleted volume. After doing docker-compose up again, I put it in without any problem.

Various files

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"

#Create db and backend containers
services:
  db:
    container_name: db
    #Pull the image uploaded to Docker Hub
    image: mysql:5.7.30
    #Environment variable settings
    environment: 
      TZ: Asia/Tokyo #Set TimeZone as well as Dockerfile
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: password
    #Specify the path of the configuration file to mount
    volumes:
      - mysql_data:/var/lib/mysql
    #Set port
    ports:
      - 3316:3306
    #Set IP address
    networks:
      app_net: #App described later_Set to use the following IPs in the net network space
        ipv4_address: '172.20.0.2'

  backend:
    container_name: backend
    #Execute ComposeFile and specify the path when it is built
    build: ./backend/
    image: backend
    #Run gem to start the server, 0.0.0.Bind to 0 for full access
    command: bundle exec rails server -b 0.0.0.0
    #Always start the container
    tty: true
    #Launch input interface docker run-synonymous with it of it
    stdin_open: true
    
    #Backend is cache, temp, log,Mount on git
    volumes:
      - ./backend:/app:cached
      - bundle_data:/usr/local/bundle:cached
      - /app/vendor
      - /app/tmp
      - /app/log
      - /app/.git
    environment:
      TZ: Asia/Tokyo
    
    #Control startup order,After db
    depends_on:
      - db
    ports:
      - 5000:3000 #Port forwarding
    networks:
      app_net:
        ipv4_address: '172.20.0.3'


#Set up your own network
networks:
  #app_Define a network space called net
  app_net:
    driver: bridge #bridge Connect to network
    ipam: #IP settings
      driver: default
      config:
        - subnet: 172.20.0.0/24 #Define Subnet

#Define two volumes
volumes:
  mysql_data:
  bundle_data:

Recommended Posts

docker + mysql can't connect / Can't connect to local MySQL server through socket'var / run / mysqld / mysqld.sock' (2)
Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2) error
Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2) error remedy
Can't connect to local MySQL server through socket'/var/lib/mysql/mysql.sock' (2) until resolution.
ERROR 2002 (HY000) when starting MySQL: Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
To connect from Spring to MySQL on virtual server (unsolved)
[Java] Connect to MySQL
Connect to AWS RDS from your local PC Docker environment
Connect to MySQL 8 with Java
[Android] Connect to MySQL (unfinished)