Docker 无法使用 PHP 连接到 mariadb

2022-01-15 00:00:00 docker nginx mariadb php mysql

我是 Docker 新手,一直试图弄清楚如何使用 PHP 连接到我的 MariaDB 容器,但没有成功.

I'm new to Docker and have been trying to figure out how I can connect to my MariaDB container with PHP with no success.

我尝试在stackoverflow和google上搜索,但找不到任何有用的信息,所以我希望你们能帮助我.

I tried to search on stackoverflow and google but couldn't find any useful info, so I am hoping you guys could help me out.

奇怪的是,当我尝试使用 JetBrains DataGrip 和 localhost、mysql、root、admin 连接到 MariaDB 时,我可以连接到数据库,但不能使用 PDO.

The weird thing is that when I try to connect to MariaDB with JetBrains DataGrip with localhost, mysql, root, admin I can connect to the database but not with PDO.

我真的希望你能帮助我,谢谢你的时间.

I really hope you could help me out, thanks for your time.

这里有以下项目文件:

这是我的 docker-compose.yml 文件

This is my docker-compose.yml file

version: "3.1"
services:

  nginx:
    image: nginx:alpine
    container_name: nginx
    volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    links:
       - php

  php:
    image: php:7.1-fpm
    container_name: php
    links:
      - mariadb:mysql
    volumes:
      - ./public:/public
    ports:
      - "9000:9000"

  mariadb:
    image: mariadb:10.1
    container_name: database
    environment:
      MYSQL_ROOT_PASSWORD: admin
    ports:
      - "3306:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    links:
      - mariadb
    ports:
      - 8183:80
    environment:
      PMA_HOST: mariadb
      PMA_USER: root
      PMA_PASSWORD: admin
      PMA_ARBITRARY: 1

我的 nginx.conf 文件:

My nginx.conf file:

server {
    listen 80 default;

    client_max_body_size 108M;

    access_log /var/log/nginx/application.access.log;


    root /public;
    index index.php;

    if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
    }

    location ~ .php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
    }

}

还有我的 index.php 文件

And my index.php file

<?php

    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $database = "mysql";

    try {
        $conn = new PDO("sqlite:host=".$servername.";dbname=" . $database, $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = $conn->prepare("SELECT * FROM testDB");
        $sql->execute();

        while($result = $sql->fetch(PDO::FETCH_ASSOC)){
            $result['myTestData'];
        }

    }
    catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }

?>

推荐答案

这是因为你试图从你的 php 容器中访问主机名 localhost 的数据库服务器,它解析为 php 容器本身(与 127.0.0.1 相同).

This is because you are trying to reach database server with hostname localhost from your php container, which resolves to the php container itself (same with 127.0.0.1).

您应该将您的 $servername 变量更改为等于您撰写文件中的 mariadb 服务名称,例如mariadb".

You should change your $servername variable to be equal to your mariadb service name in your compose file, e.g. "mariadb".

在您的情况下,位于单个默认网络中的每个容器都由其服务名称(除其他外)解析,这就是您不需要链接的原因,这在某种程度上已被弃用.

In your case every container, being in a single default network, is resolved by its service name (among other things), that's why you don't need links, which are somewhat deprecated.

另外(以防万一)确保您的数据库容器已实际启动并准备好接收连接.第一次开始需要一段时间.

Also (just in case) make sure that your db container is actually started and ready to receive connections. It takes a while to start for the first time.

相关文章