Search code examples
mysqldockerdocker-composedockerfileadminer

Mysql refuses Connection to Adminer on Docker


I know this is a duplicate of this, but since that was never answered, I am re-posting this question.

I am trying to build a basic connection of php-apache and mysql containers.

docker-compose.yml

version: '3.1'

services: 
  php:
    build: 
      context: .
      dockerfile: Dockerfile
    ports: 
      - 80:80
    volumes: 
      - ./src:/var/www/html
  
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    # Exposing the ports doesn't help either
    # ports:
    #   - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Dockerfile

FROM php:7.2-apache
RUN docker-php-ext-install mysqli

I run a simple docker-compose up command, and access Adminer on localhost:8080.
However, despite using the default login details, i.e

server: db  
username: root
password: example

I get the error SQLSTATE[HY000] [2002] Connection refused.

Screenshot:
enter image description here

I think the issue might be some configuration issue on my local machine, because I couldn't use docker based LAMP stacks made by other people too.

Any help is greatly appreciated,
Thank You!


Solution

  • Turns out Patience was the answer.
    If I wait long enough(around 4.5 minutes), the mysql container lets out a small log detail.

    db_1       | 2021-03-09T08:21:03.882088Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
    

    After that the login in works.

    Note: There is a similar line that is logged, but that is regarding port 0, this is 3306. Attempting a failure immediately after that fails.
    One must wait for the line with port 3306.

    If any of you can give me feedback or advice on how I could make my mysql container initialize faster,
    I'd greatly appreciate it.