Home > Software design >  Getting php error related to MySQL server in docker: SQLSTATE[HY000] [2002] Connection refused
Getting php error related to MySQL server in docker: SQLSTATE[HY000] [2002] Connection refused

Time:12-28

When I try to connect php whith the container that has the mysql server I get this error

NOTICE: PHP message: PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /app/public/src/ConexaoBD.php:5
Stack trace:
#0 /app/public/src/ConexaoBD.php(5): PDO->__construct()
#1 /app/public/src/ProdutoDAO.php(8): ConexaoBD::getConexao()
#2 /app/public/home.php(15): ProdutoDAO->consultarProdutos()
#3 {main}
  thrown in /app/public/src/ConexaoBD.php on line 5
172.19.0.8 -  26/Dec/2022:14:02:05  0000 "GET /home.php" 500

This is the docker compose file:

version: '3.1'
services:
    memcached:
        image: 'memcached:alpine'

    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '8001:8025'

    redis:
        image: 'redis:alpine'

    mysql:
        image: 'mysql:8.0'
        working_dir: /app
        volumes:
            - ./init:/docker-entrypoint-initdb.d
        environment:
            - MYSQL_ROOT_PASSWORD=1234
            - MYSQL_PASSWORD=1234
        ports:
            - '8002:3306'
        

    clickhouse:
        image: 'yandex/clickhouse-server:latest'

    webserver:
        image: 'nginx:alpine'
        working_dir: /app
        volumes:
            - '.:/app'
            - './phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf'
        ports:
            - '8000:80'

    php-fpm:
        build: phpdocker/php-fpm
        working_dir: /app
        volumes:
            - '.:/app'
            - './phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/8.2/fpm/conf.d/99-overrides.ini'

This is the connection class:

class ConexaoBD{

    public static function getConexao():PDO{
        $conexao = new PDO("mysql:host=127.0.0.1;port=8002;dbname=lisbuy","root","1234",
        array(PDO::ATTR_PERSISTENT => true));

        return $conexao;
    }
}

Now that I changed the host to 127.0.0.1, the connection is refused. And there's no way that the MySQL server isn't running, cause i can connect to it using php-server, but the nginx webserver won't connect

CodePudding user response:

To resolve this I checked the network of my project

docker network ls

Which resulted in this:

NETWORK ID NAME DRIVER SCOPE

2d29fdfc097d bridge bridge local

daacebb20248 getting-started_default bridge local

417454d96fb2 host host local

86e9b81f44b9 none null local

57081b8817bf projeto-ecommerce_default bridge local

Then I run:

docker network inspect 57081b8817bf

And got this:

"98e3771dbbf9070f18da96614e586faeed0274d9933c093c2e45984de005f930": {
            "Name": "projeto-ecommerce-mysql-1",
            "EndpointID": "f6003fd274f8701e96b6f4e8bfc552eb5f6fdb22c2976980519705f9736c1d0d",
            "MacAddress": "02:42:ac:13:00:05",
            "IPv4Address": "172.19.0.5/16",
            "IPv6Address": ""

Then, in the connection class I just added the "Name" of the container to the host connection variable:

public static function getConexao():PDO{
    $conexao = new PDO("mysql:host=projeto-ecommerce-mysql-1;dbname=lisbuy","root","1234");

    return $conexao;
}

And worked!

  • Related