带有Apache和MySQL的Docker PHP 5.6.23 mysqli_connect不是扩展名为

时间:2017-11-29 02:02:08

标签: php mysql apache docker

docker环境提供文件,但我无法使用mysqli_connect($host, $username, $password, $dbName)建立数据库连接而不会收到mysqli_connect is not a function的错误。

选中使用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);

1 个答案:

答案 0 :(得分:0)

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

$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);