Lucero79
Lucero79

Reputation: 169

Docker Compose phpmyadmin not connecting to MySQL

I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.

Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:

  command: --default-authentication-plugin=mysql_native_password

but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:

mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again

and

mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again

Here is a copy of my Docker Compose which I don't think is doing anything weird.

Hoping someone can clarify that for me.

version: "3.1"
services:

    site: 
      build: .
      ports :
        - "80:80"
      volumes:
        - ./www:/var/www/html/
      links:
        - database
      networks:
        - php-network

#######################################
# PHP MY ADMIN
#######################################
    phpmyadmin:
      build:
        context: .
        dockerfile: PHPMYADMIN.Dockerfile
      restart: always
      links:
        - database:mysql
      environment:
        PMA_HOST: mysql
        PMA_PORT: 3306
      ports:
        - "8080:80"
      environment:
        - MYSQL_USERNAME=admin
        - MYSQL_PASSWORD=root
      networks:
        - php-network

#######################################
# MySQL server
#######################################
    database:
      image: mysql:5.7.25
      ports:
        - "3306:3306"
      container_name: db-mysql 
      restart: always 
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=test_db
        - MYSQL_USER=admin
        - MYSQL_PASSWORD=root
      networks:
        - php-network

networks:
  php-network:
    driver: bridge

I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.

Upvotes: 1

Views: 6189

Answers (3)

Lucero79
Lucero79

Reputation: 169

I've got this working now with the following:

version: "3.1"
services:
    www:
        build: .
        ports: 
            - "8081:80"
        volumes:
            - ./www:/var/www/html/
        links:
            - db
        networks:
            - default

#######################################
# MySQL server
#######################################
    db:
        image: mysql:5.7.25
        ports: 
            - "3306:3306"
        environment:
            MYSQL_DATABASE: test_db
            MYSQL_ROOT_PASSWORD: test
        volumes:
            - ./dump:/docker-entrypoint-initdb.d
            - persistent:/var/lib/mysql
        networks:
            - default

#######################################
# PHP MY ADMIN
#######################################
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links: 
            - db:db
        ports:
            - 8080:80
        environment:
            MYSQL_USER: user
            MYSQL_PASSWORD: test
            MYSQL_ROOT_PASSWORD: test
volumes:
    persistent:

Upvotes: 1

Sorinnn
Sorinnn

Reputation: 11

It's about the container name.

database:
     image: mysql:5.7.25
     ports:
       - "3306:3306"
       - **container_name: db-mysql**

and in your phpcontainer:

 phpmyadmin:
      build:
        context: .
        dockerfile: PHPMYADMIN.Dockerfile
      restart: always
      links:
        - database:mysql
      environment:
        **PMA_HOST: mysql**
        PMA_PORT: 3306

you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.

Sorry for bad formatting.

Also use docker ps to see docker container names and to figure out which hosts do you need to connect.

Upvotes: 1

nickgryg
nickgryg

Reputation: 28603

According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:

environment:
  PMA_HOST: mysql
  PMA_PORT: 3306

But container with mysql server is accessible by database or db-mysql addresses.

So, you need to change phpmyadmin service configuration to:

environment:
  PMA_HOST: database
  PMA_PORT: 3306

Upvotes: 0

Related Questions