迁移到新服务器上的PHP网站,使用浏览器访问时提示“不支持MySQL”等信息,如何处理?

更新时间:
复制 MD 格式

本文介绍迁移到新服务器上的PHP网站,使用浏览器访问时提示“不支持MySQL”等信息的问题描述、问题原因和解决方案。

问题描述

PHP网站(该网站需使用PHP执行程序代码)迁移到新服务器后,在使用浏览器访问时提示如下任意一种信息。

  • 系统不支持MySQL。

  • PHP不支持MySQL扩展。

  • 其他类似错误信息,示例如下。

    Warning: Undefined property: POMO_FileReader::$_f in /usr/share/nginx/html/wordpress/wp-includes/pomo/streams.php on line 125
    Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

问题原因

因为MySQL扩展功能自PHP 5.5.0版本开始被废弃,并且从PHP 7.0.0版本开始被移除,当网站迁移到新服务器时一般会迁移到PHP 7.0.0及以上版本,由于网站迁移后的新服务器的PHP版本过高,网站程序无法再连接MySQL,导致该问题。

您可以使用php -v命令,查看当前服务器的PHP版本。执行以下命令确认服务器的 PHP 版本及已安装的 MySQL 相关扩展,可以看到 PHP 版本为 8.0.27,已安装的 MySQL 扩展为 mysqli、mysqlnd、pdo_mysql,不包含已被移除的旧版 mysql 扩展。

[root@xxx ~]# php -v
PHP 8.0.27 (cli) (built: Jan  3 2023 16:17:26) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.27, Copyright (c) Zend Technologies
[root@xxx ~]# php -m|grep -i mysql
mysqli
mysqlnd
pdo_mysql

解决方案

您可以在新服务器上安装并运行5.5.0以下版本的PHP源码包,来为网站提供MySQL扩展功能。

说明

本文操作步骤以在CentOS 7.9操作系统为例,具体操作以您实际的操作系统版本为准。

  1. 登录迁移后的ECS实例。

    具体操作,请参见连接方式概述

  2. 执行如下命令,安装PHP依赖的软件包。

    yum install -y gcc gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel curl curl-devel libxslt-devel openssl-devel
  3. 执行如下命令,从PHP官网下载PHP 5.5.0及以下版本的源码包。

    说明

    后续操作均以下载PHP 5.4.45版本的源码包为例,具体版本号以您实际源码包版本为准。

    wget https://www.php.net/distributions/php-5.4.45.tar.gz
  4. 安装PHP。

    1. 执行如下命令,解压下载的源码包。

      tar -zxvf php-5.4.45.tar.gz
    2. 执行如下命令,进入源码包目录。

      cd php-5.4.45
    3. 执行如下命令,安装PHP源码包。

      ./configure --prefix=/usr/local/php5.4.45 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysql --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-sockets --enable-sysvsem --enable-xml --enable-zip
      • 如果回显显示Thank you for using PHP,则表示PHP源码包配置成功。执行成功后,返回如下类似信息。

        License:
          This software is subject to the PHP License, available in this
          distribution in the file LICENSE.   By continuing this installation
          process, you are bound by the terms of this license agreement.
          If you do not agree with the terms of this license, you must abort
          the installation process at this point.
        Thank you for using PHP.
        config.status: creating php5.spec
        config.status: creating main/build-defs.h
        config.status: creating scripts/phpize

        其中 Thank you for using PHP. 表示 PHP 源码 configure 执行成功。

      • 如果安装过程中缺少某个软件包,会出现相关的报错,您可以根据报错提示安装缺失的软件包,然后再重新安装PHP源码包。

    4. 执行如下命令,编译并安装PHP。

      make && make install

      等待PHP安装完成即可。

  5. 配置PHP运行版本为5.4.45版本。

    1. 执行如下命令,进入PHP的安装目录。

      cd /usr/local/php5.4.45/etc
    2. 执行如下命令,把默认的配置文件复制为php-fpm.conf

      cp php-fpm.conf.default php-fpm.conf
    3. 执行如下命令,启动PHP服务。

      /usr/local/php5.4.45/sbin/php-fpm 
  6. 重新访问网站。

    如果访问成功,该问题解决。

相关文档