如何在 RedHat / CentOS 上连接 PHP 7.x 到 Oracle 数据库?

How do I connect PHP 7.x to Oracle database on RedHat / CentOS?

我正在努力将我的 PHP 应用程序连接到 Oracle 数据库。上周我尝试了多种选择但没有成功。

我已经成功安装了 oci8 扩展,但无法识别 oci_connect 功能。

请协助。

红帽企业版 Linux 7.6
PHP: 7.3
甲骨文:12c

经过一个多星期的努力来解决这个问题,这里总结了最终起作用的方法。我希望这对其他人有帮助。

这些说明适用于 RedHat / CentOS Linux 安装。我的经验是 RedHat / CentOS 的说明与其他 Linux 安装略有不同。避免 Oracle 指导...他们根本没有帮助!

第一步: 我按照这个优秀网站上的说明安装了 PHP 7.3 和其他 Remi rpm 包:https://tecadmin.net/install-php7-on-centos7/

第 2 步:然后我使用 Remi 存储库(参见第 1 步)安装 oci8 扩展:

$ sudo yum --enablerepo=remi-php73 install php-oci8

第 3 步:我们现在需要安装 oracle instant client 18.3 包。这在这个网站上有很好的解释:https://qiita.com/tkprof/items/2a4eb868f45fb5759110

$ cd /etc/yum.repos.d
$ sudo wget http://yum.oracle.com/public-yum-ol7.repo
$ sudo wget http://yum.oracle.com/RPM-GPG-KEY-oracle-ol7
$ sudo rpm --import RPM-GPG-KEY-oracle-ol7
$ sudo yum-config-manager --enable ol7_oracle_instantclient
$ sudo yum install oracle-instantclient18.3-basic
$ sudo yum install oracle-instantclient18.3-devel
$ sudo yum install oracle-instantclient18.3-jdbc
$ sudo yum install oracle-instantclient18.3-sqlplus
$ sudo yum list oracle-instantclient*

第 4 步: oracle 文件已在 /usr/lib/oracle/18.3 中创建,我们现在需要创建一个符号链接,以便在服务器为 运行:

时包含 oracle 文件
$ sudo sh -c "echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
$ sudo ldconfig

第 5 步:重启服务器

 $ sudo systemctl stop httpd
 $ sudo systemctl start httpd

第 6 步:我们现在可以创建一个简单的 php 文件来测试与 Oracle 的连接:

<?php     
$conn = oci_connect("username", "password", "//url/SID");

if (!$conn) {
    echo "oci8 working! However the following errors occurred: <br>";
    $m = oci_error();
    echo $m['message'], "\n";
    exit;
} else {
    print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);

如果出现系统错误,则说明安装失败。

PHP-OCI8 的最新 Oracle Instant Client 版本 19.3 更新:

>> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-basic-19.3.0.0.0-1.x86_64.rpm
>> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-devel-19.3.0.0.0-1.x86_64.rpm
>> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-jdbc-19.3.0.0.0-1.x86_64.rpm
>> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-sqlplus-19.3.0.0.0-1.x86_64.rpm

>> sudo yum localinstall oracle-instantclient19.3-basic-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-devel-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-jdbc-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-sqlplus-19.3.0.0.0-1.x86_64.rpm

// create symbolic link
eu-onesead-d001 ~]# sudo sh -c "echo /usr/lib/oracle/19.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
eu-onesead-d001 ~]# sudo ldconfig

重启服务器

为了测试连接,我创建了一个 php 文件,我将其上传到服务器并通过浏览器进行测试。注意:您显然应该谨慎行事并在测试后删除文件:

<?php
echo "Ensure you have restarted the server if you have installed any new extensions!";
echo "<br>Attempting Oracle db connection...";

// this will open the oracle extension and execute a connection
try {
$conn = oci_connect(
    "username_goes_here",
    "password_goes_here",
    "//oracle_server_address_goes_here/sid_goes_here");

echo "<br>OCI Connect extension found.. opening connnection...";

if (!$conn) {
    echo "<br>Function 'oci_connect' found and working but cannot connect to Oracle: <br>";
    $m = oci_error();
    echo $m['message'], "\n";
    exit;
} else {
    echo "<br>SUCCESS: Connected to Oracle!";
}
} catch (Exception $e) {
    echo $e->getMessage();
}

// Close the Oracle connection
oci_close($conn);
echo "<br>Connection closed";
echo "<br>For security, please delete this file off the server";