Docker MYSQL '[2002] Connection refused'

72,684

Solution 1

The '[2002] Connection refused' means you can reach the database server, but you don't have right access for the user (in your case admin). By default mariadb have a root user with the password given by MYSQL_ROOT_PASSWORD and this user can connect from any server (%).

If you want use an over login to your databases, you have to create it in the databases server with the right granting on databases from chosen locations.

The problem here is that you have named your database server as 'mysql' (service name in the docker-compose file). But by default phpmyadmin tries to connect to a database server named 'db'. Adding PMA_HOST: mysql under the environment section of the phpmyadmin service will resolve this problem.


I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server)

Solution 2

I had this challenge because I am running 3 different containers with different IP Addresses

db - 172.18.0.3 - container for MYSQL
app - 172.18.0.2 - container for Laravel app

so I fixed it by editing my Laravel .env file

DB_CONNECTION=mysql
DB_HOST=172.18.0.3
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
...

To get your containers Ip addresses. From your docker host

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

UPDATED: For latest docker versions and based on the services name, "mysql", in your docker-compose.yml

mysql:
  image: mariadb
  ports:
    - 3306:3306

You can try this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username

DB_HOST is the name of MySQL service name defined in docker-compose.yml

Solution 3

I've managed to connect to the mysql instance using mysql command line tool, this is the command I used - mysql -u root -p -h 127.0.0.1, and the entering the admin password. Is that a sufficient solution for you?

Solution 4

In my case I was running mysql in a docker container whose port was mapped to the host mac (3306:3306). I tried connecting to this database from a phpmyadmin docker container using 127.0.0.1 . But it won't work because the localhost on the phpmyadmin docker container does not have the required mysql running.

To connect to the host from docker network

docker.for.mac.host.internal

Docker Networking Docker Compose Networking

Share:
72,684

Related videos on Youtube

Frank Millc
Author by

Frank Millc

Updated on July 09, 2022

Comments

  • Frank Millc
    Frank Millc almost 2 years

    I was trying out Docker for the first time. Got a LEMP stack up and running, but I can't connect to the MYSQL Database. Not on my Symfony application, not on PHPMyAdmin. The applications are returning the following error code:

    An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

    This is my docker-compose.yml:

    nginx:
        image: tutum/nginx
        ports:
            - "80:80"
        links:
            - phpfpm
        volumes:
            - ./nginx/default:/etc/nginx/sites-available/default
            - ./nginx/default:/etc/nginx/sites-enabled/default
    
            - ./logs/nginx-error.log:/var/log/nginx/error.log
            - ./logs/nginx-access.log:/var/log/nginx/access.log
    phpfpm:
        build: phpfpm/
        ports:
            - "9000:9000"
        volumes:
            - ./public:/usr/share/nginx/html
    mysql:
      image: mariadb
      ports:
        - 3306:3306
      environment:
        MYSQL_ROOT_PASSWORD: admin
    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      restart: always
      links:
        - mysql
      ports:
        - 8183:80
      environment:
        MYSQL_USERNAME: admin
        MYSQL_ROOT_PASSWORD: admin
        PMA_ARBITRARY: 1
    

    Dockerfile PHPFPM:

        FROM php:fpm
    
    RUN docker-php-ext-enable opcache
    RUN apt-get update \
      && apt-get install -y --no-install-recommends libpq-dev \
      && docker-php-ext-install mysqli pdo_pgsql pdo_mysql
    

    GitHub URL: https://github.com/MolengraafFrank/DockerSymfony

    Could someone help me out? Thank you for your time.

    • Yaron Idan
      Yaron Idan over 7 years
      please add the contents of phpfpm/ lib, since without it we cannot test your docker-compose setup.
    • Frank Millc
      Frank Millc over 7 years
      I have added the Dockerfile. Is that enough?
    • Yaron Idan
      Yaron Idan over 7 years
      Does you docker-compose up command finish successfully? When I try to run it the nginx container raises an error and the process crashes.
    • Frank Millc
      Frank Millc over 7 years
      Yaron, I will commit my whole dockerproject to git today. I will post the url here! Thank you in advance!
    • Frank Millc
      Frank Millc over 7 years
      Yaron, this is the url to the GitHub repository: github.com/MolengraafFrank/DockerSymfony.
  • Frank Millc
    Frank Millc over 7 years
    Thank you. I've done this, but I am still getting the [2002] Connection refused error.
  • Yaron Idan
    Yaron Idan over 7 years
    @FrankMolengraaf if you've made any changes to your docker-compose.yml please update your question with them
  • Frank Millc
    Frank Millc over 7 years
    This works, but my application in PHPFPM still can't connect to the database.
  • Frank Millc
    Frank Millc over 7 years
    Edited the PHPFPM docker-compose. It works for 100% now!
  • Danilo Colasso
    Danilo Colasso over 4 years
    This solved to me, thank you. But there is a way to use just "localhost"?
  • Nico Haase
    Nico Haase about 4 years
    Please add some further explanation to your answer - according to the shared configuration, the container for phpMyAdmin is properly linked
  • PatricNox
    PatricNox over 3 years
    It may work but you should know that the ip address will change on every restart, therefore isn't a consistent solution.
  • Gabriel Glauber
    Gabriel Glauber over 2 years
    This works for me using a mysql on host and connecting from docker.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.