gpt4 book ai didi

php - 带有 Apache 和 MySQL mysqli_connect 的 Docker PHP 5.6.23 不是添加了扩展的函数

转载 作者:可可西里 更新时间:2023-11-01 09:00:01 26 4
gpt4 key购买 nike

docker 环境提供文件,但我无法使用 mysqli_connect($host, $username, $password, $dbName) 建立数据库连接,而不会出现 mysqli_connect is不是函数

检查 php.ini 使用 phpinfo() 并访问容器 docker exec -it server bash 并目视检查文件。在网上搜索了这个 PHP 版本和 Apache 的相关问题,找到了这个 issue ,但它并没有解决问题,尽管我已经将更改添加到 dockerfile 中。

谁能弄清楚为什么 mysql 扩展没有加载? docker-compose 文件是完整的,dockerfiles 除了复制 php.ini 之外是最小的,所以容器都可以从下面的代码构建,并替换 env 变量。

Docker-Compose 文件

version: '2'
services:
server:
container_name: server
build:
context: .docker/server
dockerfile: apache-php.dockerfile
image: ${APP_DOMAIN}/server
ports:
- "${APP_PORT}:80"
volumes:
- ${APP_SRC_PATH}:/var/www/html
networks:
- wv-net
db:
container_name: db
image: mysql:5.7.20
ports:
- "${DB_PORT}:3306"
env_file:
- .docker/database/env/mysql.env
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- wvsqldata:/var/lib/mysql
restart: always
networks:
- wv-net
networks:
wv-net:
driver: "bridge"
volumes:
wvsqldata:
driver: "local"

php.ini

复制到容器中的 php.ini 文件我已经取消注释所有 mysql 扩展以彻底:

extension=php_curl.dll
extension=php_mbstring.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_openssl.dll
extension=php_pdo_mysql.dll

PHP-Apache Dockerfile

FROM php:5.6.32-apache-jessie

RUN apt-get update \
&& apt-get install -y nano \
&& docker-php-ext-install pdo pdo_mysql mysql mysqli

EXPOSE 80

COPY config/php.ini /usr/local/etc/php/

MySQL Dockerfile

FROM mysql:5.7.20

EXPOSE 3306

index.php

用过这个script检查 php.ini 并查看 mysql 模块是否正在加载。它显示扩展未被注释,但 mysql 扩展未加载。

<?php 

if ( !function_exists('mysql_connect') ) {
echo '<pre>mysql extension loaded: ', extension_loaded('mysql') ? 'yes':'no', "\r\n";
$cf = get_cfg_var('cfg_file_path');
echo 'ini file: ', $cf, "\r\n";
if ( !$cf || !file_exists($cf) ) {
echo "no config file\r\n";
}
else {
echo "mysql config options:\r\n";
$mc = array_filter( file($cf), function($e) { return false!==stripos($e, 'mysql') && false!==stripos($e, 'extension'); });
echo join("", $mc);
}
die('no function mysql_connect</pre>');
}

哪些输出:

mysql extension loaded: no
ini file: /usr/local/etc/php/php.ini
mysql config options:
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
; Records communication from all extensions using mysqlnd to the specified log
no function mysql_connect

还尝试使用 mysqli 连接,但同样失败:

$link = mysqli_connect('localhost', $username, $password, $dbName);

if (!$link) {
echo "ERROR: Unable to connect to MySQL." . PHP_EOL . '<br>';
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL . '<br>';
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL . '<br>';
exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

最佳答案

解决了这个问题。我使用 localhost 作为连接中的 DBHOST,而不是 db 的 docker 容器名称:

$link = mysqli_connect('db', $username, $password, $dbName);

if (!$link) {
echo "ERROR: Unable to connect to MySQL." . PHP_EOL . '<br>';
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL . '<br>';
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL . '<br>';
exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

关于php - 带有 Apache 和 MySQL mysqli_connect 的 Docker PHP 5.6.23 不是添加了扩展的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com