skip to Main Content

I have started using Docker since yesterday but I cant figure out to fix the mysql connection refused error. I was wondering if any of you guys could help me as I don’t know wat to do hahah

This is my docker-compose.yml

version: '3'
services:
  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASENAME: my_test_database
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_USER: develop
      MYSQL_PASSWRD: develop
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    ports:
      - '3306:3306'
  www:
    depends_on:
      - db
    image: php:8.0.30-apache
    volumes:
      - "./:/var/www/html"
    ports:
      - 80:80
      - 443:443
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_HOST=db
      - PMA_PORT=3306

and this is inside my dockerfile

FROM php:8.0.30-apache

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

hope anyone could help me thanks!

I was expecting a connection with my database server

EDIT: changed PWA to PMA

2

Answers


  1. The issue is here:

    environment:
          - PWA_HOST=localhost
    

    Here localhost refers to IP-address of the phpmyadmin service’s container, and not to your machine’s IP. To make other services communicate to db service you have to provide db service’s name wherever you would provide IP-address of the machine running mysql.

    Also note, that you mistyped the name of the environment variable(should be PMA_HOST according to phpMyAdmin docker image page)

    So, the correct configuration would be:

    environment:
          - PMA_HOST=db
          - PMA_PORT=3306
    
    Login or Signup to reply.
  2. There are a number of issues in your docker-compose.yml.

    db service

    • MYSQL_DATABASENAME should be MYSQL_DATABASE
    • MYSQL_PASSWRD should be MYSQL_PASSWORD
    • Avoid MYSQL_HOST environment variable as it causes issues with this image (see docs)
    • MYSQL_PORT is unnecessary, as you are setting it to the default port (not even sure this env var is used)

    phpmyadmin service

    • You need to map the port for PMA 8080:80 – will make PMA available at http://localhost:8080 on host.
    • PMA_PORT is unnecessary, as 3306 is the default.

    Your dockerfile is not being used by anything as the www service is using image, not build (read this example in the docs)

    It’s a good idea to set a restart policy.

    version: '3'
    services:
      db:
        image: mysql:latest
        restart: always
        environment:
          MYSQL_DATABASE: my_test_database
          MYSQL_USER: develop
          MYSQL_PASSWORD: develop
          MYSQL_ALLOW_EMPTY_PASSWORD: 1
        ports:
          - 3306:3306
      www:
        depends_on:
          - db
        image: php:8.0.30-apache
        restart: always
        volumes:
          - "./:/var/www/html"
        ports:
          - 80:80
          - 443:443
      phpmyadmin:
        depends_on:
          - db
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - 8080:80
        environment:
          - PMA_HOST=db
    

    If you want to use your Dockerfile for your www service, change it to (assuming Dockerfile is in project root):

      www:
        depends_on:
          - db
        build: .
        restart: always
        volumes:
          - "./:/var/www/html"
        ports:
          - 80:80
          - 443:443
    

    Docs: Compose Build Specification

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search