Docker无法使用PHP连接到mariadb [英] Docker can't connect to mariadb with PHP

查看:281
本文介绍了Docker无法使用PHP连接到mariadb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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.

这篇关于Docker无法使用PHP连接到mariadb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆