简体   繁体   English

在 docker 中获取与 MySQL 服务器相关的 php 错误:SQLSTATE[HY000] [2002] 连接被拒绝

[英]Getting php error related to MySQL server in docker: SQLSTATE[HY000] [2002] Connection refused

When I try to connect php whith the container that has the mysql server I get this error当我尝试将 php 连接到具有 mysql 服务器的容器时,出现此错误

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:这是 docker 撰写文件:

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:

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.现在我将主机更改为 127.0.0.1,连接被拒绝。 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而且 MySQL 服务器不可能没有运行,因为我可以使用 php-server 连接到它,但是 nginx 网络服务器无法连接

To resolve this I checked the.network of my project为了解决这个问题,我检查了我项目的.network

docker network ls

Which resulted in this:结果是:

NETWORK ID NAME DRIVER SCOPE网络 ID 名称驱动程序 SCOPE

2d29fdfc097d bridge bridge local 2d29fdfc097d 桥桥本地

daacebb20248 getting-started_default bridge local daacebb20248 getting-started_default 网桥本地

417454d96fb2 host host local 417454d96fb2 主机主机本地

86e9b81f44b9 none null local 86e9b81f44b9 无 null 本地

57081b8817bf projeto-ecommerce_default bridge local 57081b8817bf projeto-ecommerce_default 桥本地

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:然后,在连接 class 中,我将容器的“名称”添加到主机连接变量中:

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

    return $conexao;
}

And worked!并且工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM