Docker mysql Error: Connection refused

Asked

Viewed 2,170 times

-1

Hi !

I’m creating my Docker development environment. But I’m having some problems:

I can connect to the mysql container by an SGBD, but I can’t connect through php-fpm, it generates an error :

SQLSTATE[HY000] [2002] Connection refused. 

Follow my setup:

Docker-Compose.yml

version: "3"
services:
  nginx:
    image: "nginx:1.17.2"
    container_name: "nginx-php-general"
    volumes: 
      - "./nginx/www:/usr/share/nginx/html/"
      - "./nginx/config/1-web.conf:/etc/nginx/conf.d/1-web.conf"
      - "./nginx/logs/web.access.log:/var/log/nginx/web.access.log"
      - "./nginx/logs/web.error.log:/var/log/nginx/web.error.log"
    ports:
      - "80:80"
    networks:
      - "networks-php-general"
    depends_on: 
      - "php-fpm"
  php-fpm:
    build: 
      "./php"
    container_name: "php-fpm-php-general"
    volumes: 
      - "./nginx/www:/usr/share/nginx/html/"
    networks:
      - "networks-php-general"
  mysql: 
    image: "mysql:8.0.17"
    container_name: "mysql-php-general"
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      MYSQL_DATABASE: "default-base"
      MYSQL_USER: "guest"
      MYSQL_PASSWORD: ""
    ports:
      - "3306:3306"
    networks:
      - "networks-php-general"

networks:
  networks-php-general:
    driver: "bridge"

php connection.

<?php
try {
    $dbh = new PDO('mysql:host=mysql-php-general;port=3306;dbname=default-base', 'guest', '');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

Apart from the connection between mysql container and php-fpm the other parts are working right.

Who can help me be very grateful, I’m cracking my head to know what’s going on.

Vlw!

2 answers

1

Isn’t the default Mysql port 3306 instead of 3360? I don’t know if this image contains any changes to be using another. Connection refused message indicates that you were able to find the host, so it might be the same port.

  • It was a great observation friend I made the appropriate changes, thank you. However it is still generating the error.

1


When the network is created what becomes available is the name of the service and not the container name as described in the networking documentation. Soon the address for the database service is "mysql" and not "mysql-php-general".

<?php
try {
    $dbh = new PDO('mysql:host=mysql;port=3306;dbname=default-base', 'guest', '');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
  • I spent a lot of time trying to figure it out. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.