1

I face an issue while trying to make docker-compose contains Rails API and MySQL and others. After docker-compose up and send a request to the Rails API I get this error "#<Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'database' (111 \"Connection refused\")>"

My docker-compose

services:
  database:
    image: mysql:8.0.23
    restart: always
    volumes:
      - 'db_data:/var/lib/mysql'
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USERNAME: root
      MYSQL_PASSWORD: root

  app:
    build: .
    restart: always
    depends_on:
      - 'database'
      - 'redis'
    ports:
      - '3000:3000'
    volumes:
      - '.:/app'
      - 'gem_cache:/usr/local/bundle/gems'
    env_file: .env
    environment:
      RAILS_ENV: development

database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  host: <%= ENV['DATABASE_HOST'] %>
  port: <%= ENV['DATABASE_PORT'] || '5432' %>
  username: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: <%= ENV['DATABASE_NAME_DEV'] %>
...

.env

...
DATABASE_HOST=database
...

What's the problem here?

1 Answer 1

2

MySQL uses 3306 as the default port, and you should assign 3306 to the DATABASE_PORT environment.

Sign up to request clarification or add additional context in comments.

2 Comments

Now I got this error Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.22.0.4' (using password: NO)>
Assign root to DATABASE_PASSWORD as well ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.