简体   繁体   English

如何使用 mysql 在 docker 中配置和运行旧的 php 5.4 Web 应用程序?

[英]How to configure and run old php 5.4 web application in docker with mysql?

I have an old application based on php 5.4 and mysql.我有一个基于 php 5.4 和 mysql 的旧应用程序。 I had developed that in 2015. I want to run that application using docker as changing php version for individual application is not a feasible solution.我在 2015 年开发了它。我想使用 docker 运行该应用程序,因为更改单个应用程序的 php 版本不是一个可行的解决方案。

What I did is as follows:我所做的如下:

Dockerfile文件

FROM php:5.4-apache

COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

RUN a2enmod rewrite

COPY . /var/www/html/
RUN chown -R www-data:www-data /var/www/html

RUN docker-php-ext-install mysql

CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

docker-compose.yml docker-compose.yml

version: "3.7"
services:
  webapp:
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html

    # first load the 'db' service
    depends_on:
    - db
    links:
      - db

  db:
      image: mysql:5.7
      restart: always
      ports:
      - "13306:3306"
      environment:
        MYSQL_USER: root
        MYSQL_PASSWORD: admin
        MYSQL_ROOT_PASSWORD: admin
        MYSQL_DATABASE: pizzabite
      volumes:
      - ./mysql:/var/lib/mysql

The problem here is I was accessing mysql using old mysql methods ie mysql_connect() etc.这里的问题是我使用旧的 mysql 方法访问 mysql,即 mysql_connect() 等。

database.php数据库.php

<?php
// Connecting to the database server
$servername = "localhost:13306";
$username = "root";
$password = "admin";
$port = "13306";
$dbname = "pizzabite";


$connect = mysql_connect($servername, $username, $password) or die("<br/>Check your server connection...");

//  $connect = mysqli_connect($servername, $username, $password, $dbname,$port); // I do not want to use mysqli_* function

$selectedDB = mysql_select_db($dbname) or die(mysqli_error());
//include('date.php');
?>
    

This file is included everywhere using include() but when run using docker this file gives error:使用include()到处都包含此文件,但是当使用 docker 运行时,此文件会出现错误:

Warning: mysql_connect(): No such file or directory in /var/www/html/database.php on line 10

Check your server connection...

When using 0.0.0.0:13306 it gives me this error:使用0.0.0.0:13306 时,它给了我这个错误:

Warning: mysql_connect(): Connection refused in /var/www/html/database.php on line 10

Check your server connection...

docker-compose ps command output: docker-compose ps命令输出:

      Name                       Command               State                 Ports               
---------------------------------------------------------------------------------------------------
pizza_corner_db_1       docker-entrypoint.sh mysqld      Up      0.0.0.0:13306->3306/tcp, 33060/tcp
pizza_corner_webapp_1   /usr/sbin/apachectl -D FOR ...   Up      0.0.0.0:8000->80/tcp 

You are trying to connect to localhost:13306 , because your PC port 13306 is forwarded to the mysql container.您正在尝试连接到localhost:13306 ,因为您的 PC 端口13306已转发到 mysql 容器。

However, that code is executed from the php-apache container, so 'localhost' is the container, not your pc.但是,该代码是从php-apache容器执行的,因此 'localhost' 是容器,而不是您的 pc。 There is no port 13306 available there.那里没有可用的端口13306

You should connect to db:13306 because in your docker-compose the database container is called db .您应该连接到db:13306因为在您的 docker-compose 中,数据库容器称为db

Also, be sure to enable mysql in your php container:另外,请务必在您的 php 容器中启用 mysql:

RUN docker-php-ext-install mysql && docker-php-ext-enable mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP 5.0到5.4的服务器上运行laravel 5.4网站? - How to run laravel 5.4 websites on a server with PHP version 5.0 to 5.4? 如何在Laravel 5.4和MySQL中运行此子查询 - How to run this subquery in Laravel 5.4 and MySQL 如何配置PHP以在Web服务器上运行与后台进程相同的版本 - How to configure PHP to run the same version on the web server as in a background process 如何在heroku上上传PHP和MySQL Web应用程序? - How to upload PHP and MySQL web application on heroku? 配置数据库连接以在任何地方运行Web应用程序 - Configure database connection to run web application anywhere 配置PHP应用程序以安全地运行多个站点 - Configure PHP application to safely run for multiple sites 如何在Docker Swarm / Kubernetes中使用Nginx运行PHP应用程序 - How to run PHP application with Nginx in Docker Swarm/Kubernetes 如何将新的Web应用程序部署到具有旧数据库的旧应用程序 - how to deploy new web application to old application with old database PHP 5.4 PDO无法使用旧的不安全身份验证连接到MySQL 4.1+ - PHP 5.4 PDO could not connect to MySQL 4.1+ using the old insecure authentication 共享服务器上的PHP 5.4旧身份验证,无需编辑mysql服务器即可修复 - PHP 5.4 old auth on shared server, fixing without editing the mysql server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM