コンパイル後にPHP 5.6をSQLデータベースに接続する方法

コンパイル後にPHP 5.6をSQLデータベースに接続する方法

Debian Server 9サーバーのソースからPHP 5.6をインストールしました。 Debian 9のOpenSSLバージョンはPHP 5.6では新しいバージョンなので、PHP 5.6で使用するには/opt/opensslで以前のバージョンをコンパイルする必要があります。 (バージョンopenssl-1.0.1t)

PHP5.6のコンパイルは次のように設定します。

./configure --prefix=/opt/PHP/php-5.6 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-jpeg-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-openssl=/opt/openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/x86_64-linux-gnu --enable-ftp --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm

コマンドの後、PHP5.6がmake動作しています。make install

接続をテストするために、次のPHPスクリプトを作成しました(良い値も隠しました)。

    <?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

私はこの結果を試しました。

 /opt//PHP/php-5.6/bin/php connect_db.php
Connection failed: SQLSTATE[HY000] [2002] No such file or directory

同じサーバー上で、次のコンパイル構成を使用してソースからPHP7.1をコンパイルしました。

./configure --prefix=/opt/PHP/php-7.1 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-zlib --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-jpeg-dir=/usr --with-png-dir=/usr --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/x86_64-linux-gnu --enable-ftp --with-imap --with-imap-ssl --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm

同じPHPスクリプトを試しましたが、正常に動作します。

/opt//PHP/php-7.1/bin/php connect_db.php
Connected successfully

PHP7.1コンポーネントと同じ効果を得るためにPHP5.6コンパイルを設定する方法は?

ベストアンサー1

スクリプト行を変更すると、次のような事実が見つかりました。

$servername = "localhost";

これを通して

$servername = "127.0.0.1";

このスクリプトはPHP5.6とPHP7.1を使用してデータベースに接続します。

 /opt/PHP/php-5.6/bin/php connect_db.php
Connected successfully

問題がどこで発生するかを確認するには、いつでもコメントを残してください。

おすすめ記事