一尘不染

如何在php docker映像中安装pdo驱动程序?

mysql

我使用带有标签的php docker容器作为基础:

php:5.6-apache

我将它与mysql:5.6可以在主机上找到的基本图像链接在一起mysql。我创建了一个数据库,并用基本值填充了表。

但是尝试访问我的应用程序,我得到:

Fatal error: Uncaught exception 'PDOException' with message
could not find driver' in /var/www/html/index.php:30 
Stack trace: #0 [internal function]: 
PDO->__construct('mysql:host=mysq...', 'root', 'root', Array) 
#1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array)
#2 /var/www/html/index.php(30): Phalcon\Db\Adapter\Pdo-__construct(Array)
#3 [internal function]: {closure}()
#4 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault))
#5 [internal function]: Phalcon\Di->get('db', NULL)
#6 [internal function]: Phalcon\Di->getShared('db')
#7 [internal function]: Phalcon\Mvc\Model\Manager->_getConnection(Object(Reviews), NULL)
#8 [internal function]: Phalcon\Mvc\Model\Manager->getReadConnection(Object(Reviews))
#9 [internal function]: Phalcon\Mvc\Model->getReadConnection()
#10 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Reviews), Object(Phalcon\Di\FactoryDefault))
#11 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Rev in /var/www/html/index.php on line 30

因此,我认为php容器缺少php-mysql通过以下方式安装的组件:

apt-get install php5-mysql

我还在以下位置添加了mysql.ini:

cat /usr/local/etc/php/conf.d/mysql.ini
; configuration for php MySQL module
; priority=20
extension=pdo_mysql.so

如果我echo phpinfo();die告诉我:

Additional .ini files parsed:
    /usr/local/etc/php/conf.d/mysql.ini,
    /usr/local/etc/php/conf.d/phalcon.ini

但是,错误仍然存​​在。

此外,在运行时:

php -i|grep PDO

我得到:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled

所以看来mysql扩展甚至没有被激活。

我究竟做错了什么?


阅读 391

收藏
2020-05-17

共1个答案

一尘不染

PHP的官方存储库具有一个名为https://github.com/docker-
library/php/tree/master/5.6的脚本docker-php-ext-install

您忘记安装运行PDO所需的扩展。

尝试创建一个像这样的docker镜像:

FROM php:5.6-apache

# PHP extensions

RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql
2020-05-17