PHP容器无法连接到MySQL容器

时间:2019-09-19 13:56:21

标签: php mysql apache docker dockerfile

尽管所有容器都在同一网络上,但我正在尝试从PHP容器连接到容器化的MySQL DB,问题是尽管我检查了Connection failed: invalid data source name,但我却不断遇到http://localhost:8080/dbTest.php凭据正确,并且使用mysql -u root -p

demo 数据库存在

我在 mysql 日志 mbind:不允许的操作中看到此消息?
谢谢

Apache Dockerfile

FROM httpd:latest
RUN apt-get update
RUN apt-get upgrade
COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf"  >> /usr/local/apache2/conf/httpd.conf

cmd docker run -p 8080:80 / tcp -v / home / hmalabeh / tutorial / docker / lamp / files /:/ usr / local / apache2 / htdocs /- -link = php --name apache --network backend -i 8f704f51962d


PHP Dockerfile

FROM php:7.2.7-fpm-alpine3.7
RUN docker-php-ext-install mysqli
RUN docker-php-ext-enable mysqli
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 9000

cmd docker run -p 9000:9000 -v / home / hmalabeh / tutorial / docker / lamp / files /:/ var / www / html / --name php- -网络后端-i 1cc049496b03


MySQL Dockerfile

FROM mysql:latest
RUN apt-get update
RUN apt-get upgrade
ENV MYSQL_ROOT_PASSWORD=root

cmd docker run -p 3306:3306 -p 33060:33060 --name mysql --network backend --link = php -i 7bb7d4985301


dbTest.php

<?php
class DBConnect
{
    private $dsn = "mysql:dbname=demo;host=127.0.0.1;port=3306;";
    private $dbUsername = "root";
    private $dbPassword = "root";
    private $conn;

    public function connect()
    {
        try {
            echo 'Attempt Connection.     ';
            $this->conn = new PDO($dsn, $dbUsername, $dbPassword);

            echo 'Connected successfully.      ';
        } catch (PDOException $exception) {
            echo 'Connection failed: ' . $exception->getMessage();
        }
        return $this->conn;
    }
}


$co = new DBConnect();
$co->connect();
?>

我正在运行的容器

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                              NAMES
9639160c0824        8f704f51962d        "httpd-foreground"       2 hours ago         Up 2 hours          0.0.0.0:8080->80/tcp                               apache
38e630a9cb01        7bb7d4985301        "docker-entrypoint.s…"   2 hours ago         Up 2 hours          0.0.0.0:3306->3306/tcp, 0.0.0.0:33060->33060/tcp   mysql
c7f6e8db26a9        1cc049496b03        "docker-php-entrypoi…"   2 hours ago         Up 2 hours          0.0.0.0:9000->9000/tcp                             php

3 个答案:

答案 0 :(得分:2)

由于容器位于同一网络中,因此请使用容器的名称向DSN添加[core] autocrlf = false whitespace = cr-at-eol

ENTRYPOINT ["sh", "-c", "mysql -u my_user -pmy_pw my_databse < init.sql"]

Reference

答案 1 :(得分:0)

您是否已在mySql中启用远程访问?

  

nano /etc/mysql/mysql.conf.d/mysqld.cnf

替换: Users (collection) --- User1 (doc) Channels (collection) --- Channel1 (doc) ------ UsersInRoom (field within the user doc) ----------- ['User1', 'User2', 'User3'] --- Channel2 ------ UsersInRoom ----------- ['User4', 'User5', 'User6'] --- Threads (collection within Channels collection) ------ Message1 (doc) ---------- Sender: User1 ---------- Text: "Hi" ------ Message1 (doc) ---------- Sender: User2 ---------- Text: "Hello" 对于 bind-address= 127.0.0.1

答案 2 :(得分:0)

我忘记在变量前添加$->this,代码应如下所示。

<?php
class DBConnect
{
    private $dsn = "mysql:host=mysql;dbname=demo;port=3306;charset=utf8";
    private $dbUsername = "root";
    private $dbPassword = "root";
    private $conn;

    public function connect()
    {
        try {
            echo 'Attempt Connection.     ';
            $this->conn = new PDO($this->dsn, $this->dbUsername, $this->dbPassword);

            echo 'Connected successfully.      ';
        } catch (PDOException $exception) {
            echo 'Connection failed: ' . $exception->getMessage();
        }
        return $this->conn;
    }
}


$co = new DBConnect();
$co->connect();
?>