【问题标题】:"PDOException: could not find driver" inside docker container where PDO modules exist“PDOException:找不到驱动程序”在存在 PDO 模块的 docker 容器中
【发布时间】:2021-03-26 01:49:21
【问题描述】:

我想在docker容器中运行php-fpm,但是启动容器后得到错误信息:

致命错误:未捕获的 PDOException:找不到驱动程序。

我在一个单独的容器中使用 php7.4 和事件引擎和一个 postgres 数据库(我使用 docker-compose 来启动它们)并且正在开发 ubuntu20.04。

奇怪的是:我的同事安装了同样的东西而没有出现这个错误,所以错误不可能来自不正确的 docker 文件。

到目前为止我尝试了什么:

  • 我可以从 shell 使用 psql 连接到数据库。
  • 我打印出了容器中加载的所有模块,它们包括pdopdo_pgsql

错误指的是这个方法:

    public function pdoConnection(): PDO
    {
        /** @var PDO $pdo */
        $pdo = $this->makeSingleton(PDO::class, function () {
            $this->assertMandatoryConfigExists('pdo.dsn');
            $this->assertMandatoryConfigExists('pdo.user');
            $this->assertMandatoryConfigExists('pdo.pwd');

            return new PDO(
                $this->config()->stringValue('pdo.dsn'),
                $this->config()->stringValue('pdo.user'),
                $this->config()->stringValue('pdo.pwd'),
                [

                    // the next line is the line the error message refers to
                    PDO::ATTR_PERSISTENT => true,

                    // This is necessary due to the way pgBouncer handles (or not handles) prepared statements.
                    // See https://www.pgbouncer.org/faq.html#how-to-use-prepared-statements-with-transaction-pooling
                    PDO::ATTR_EMULATE_PREPARES => true,
                ]
            );
        });

        return $pdo;
    }

我检查了pdo.dnspdo.userpdo.pwd,它们都是正确的。我通过

定义了DNS
DNS = "pgsql:host=HOSTNAME port=5432 dbname=DATABASENAME"

docker 文件包括

docker-php-ext-install pdo_pgsql

【问题讨论】:

    标签: php postgresql docker pdo fpm


    【解决方案1】:

    在您的 docker 文件中尝试添加以下代码:

    docker-php-ext-install \
            pdo_pgsql
    

    希望能帮到你。

    【讨论】:

    • 这已经存在于 docker 文件中
    • @Julia 你能展示你的 dockerfile 吗?
    【解决方案2】:

    是DNS字符串,引号是错误的。它适用于:

    DNS = pgsql:host=HOSTNAME;port=5432;dbname=DATABASENAME
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2016-09-03
      • 2022-01-23
      • 2017-06-07
      相关资源
      最近更新 更多