我需要一个 docker 图像,它可以通过 php 连接到外部 MYSQL 数据库
I need a docker image that can get connected to an external MSSQL datatbase with php
我需要一个 docker 图像,它可以在执行 PHP 脚本时连接到外部 MSSQL 数据库。我已经创建了一个,但我只能通过 tsql CLI 连接到数据库。我认为它使用 Freetds。但是当我尝试使用 php 脚本时,出现错误提示 php 找不到 PDO 库...有人可以帮我找出我遗漏的内容吗?
这是我使用的图片:
FROM php:7.4-cli
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
ADD freetds.conf /etc/freetds/freetds.conf
ADD locales.conf /etc/freetds/locales.conf
CMD ["/bin/bash"]
和 PHP 脚本如下所示:
try {
$conn = new PDO("dblib:host={$sql_host};dbname={$sql_dbnm}", "$sql_user", "$sql_pswd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (Exception $e) {
var_dump($e);
die(print_r($e->getMessage()));
}
$tsql = "SELECT field FROM table";
$getResults = $conn->prepare($tsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row){
echo "{$row['field']}\n";
}
谢谢
我找到方法了。
这是我的 Dockerfile 的内容:
FROM php:7.3
# Update packages
RUN apt-get update
# Install PHP and composer dependencies
RUN apt-get install gnupg -qq git wget curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev
# Clear out the local repository of retrieved package files
RUN apt-get clean
# Install needed extensions
# Here you can install any other extension that you need during the test and deployment process
RUN docker-php-ext-install pdo_mysql zip
# adding custom MS repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
# install SQL Server drivers
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17
RUN wget http://pecl.php.net/get/sqlsrv-5.9.0.tgz
RUN pear install sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable sqlsrv
RUN wget http://pecl.php.net/get/pdo_sqlsrv-5.9.0.tgz
RUN pear install pdo_sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable pdo_sqlsrv
RUN echo "extension= pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
RUN echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
一个更简单的方法是使用 install-php-extensions
:它可以让你安装大量 PHP 扩展,只需一行:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions pdo_sqlsrv sqlsrv
有关 https://github.com/mlocati/docker-php-extension-installer
的更多详细信息
我需要一个 docker 图像,它可以在执行 PHP 脚本时连接到外部 MSSQL 数据库。我已经创建了一个,但我只能通过 tsql CLI 连接到数据库。我认为它使用 Freetds。但是当我尝试使用 php 脚本时,出现错误提示 php 找不到 PDO 库...有人可以帮我找出我遗漏的内容吗?
这是我使用的图片:
FROM php:7.4-cli
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
ADD freetds.conf /etc/freetds/freetds.conf
ADD locales.conf /etc/freetds/locales.conf
CMD ["/bin/bash"]
和 PHP 脚本如下所示:
try {
$conn = new PDO("dblib:host={$sql_host};dbname={$sql_dbnm}", "$sql_user", "$sql_pswd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (Exception $e) {
var_dump($e);
die(print_r($e->getMessage()));
}
$tsql = "SELECT field FROM table";
$getResults = $conn->prepare($tsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row){
echo "{$row['field']}\n";
}
谢谢
我找到方法了。 这是我的 Dockerfile 的内容:
FROM php:7.3
# Update packages
RUN apt-get update
# Install PHP and composer dependencies
RUN apt-get install gnupg -qq git wget curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev
# Clear out the local repository of retrieved package files
RUN apt-get clean
# Install needed extensions
# Here you can install any other extension that you need during the test and deployment process
RUN docker-php-ext-install pdo_mysql zip
# adding custom MS repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
# install SQL Server drivers
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17
RUN wget http://pecl.php.net/get/sqlsrv-5.9.0.tgz
RUN pear install sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable sqlsrv
RUN wget http://pecl.php.net/get/pdo_sqlsrv-5.9.0.tgz
RUN pear install pdo_sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable pdo_sqlsrv
RUN echo "extension= pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
RUN echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
一个更简单的方法是使用 install-php-extensions
:它可以让你安装大量 PHP 扩展,只需一行:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions pdo_sqlsrv sqlsrv
有关 https://github.com/mlocati/docker-php-extension-installer
的更多详细信息