我需要您的帮助才能在我的 Red Hat 8.9 服务器上以 PHP8 连接到 Oracle。
OCI8 已启用并安装了 HTTPD Web 服务器。这是我的代码:
<?php
// Enable error reporting in PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Connect to the Oracle database using oci_connect
$conn = oci_connect("toakdbi", "toakdbi_123", "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=AvaniServer)(PORT=1521)))(CONNE_DATA=(SID=orcl)))");
if (!$conn) {
$e = oci_error();
echo "Connection failed: " . $e['message'];
} else {
echo "</br>Connected to Oracle!";
}
?>
这是我得到的错误:
Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that LD_LIBRARY_PATH includes the directory with Oracle Instant Client libraries in /var/www/html/db2.php on line 10
Warning: oci_connect(): Error while trying to retrieve text for error ORA-01804 in /var/www/html/db2.php on line 10
Warning: Trying to access array offset on false in /var/www/html/db2.php on line 14
Connection failed:
我的配置如下:
/etc/httpd/conf/httpd.conf
SetEnv LD_LIBRARY_PATH /home/oracle/Avani/dbhome_1/lib
SetEnv NLS_LANG American_America.UTF8
SetEnv PATH /usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin
PassEnv LD_LIBRARY_PATH
/etc/sysconfig/httpd
export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export ORACLE_HOME=/home/oracle/Avani/dbhome_1
export ORACLE_BASE=/home/oracle
export PATH=/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin
~/.bashrc
# .bashrc
# User specific aliases and functions
export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export NLS_LANG=American_America.UTF8
export TNS_ADMIN=/home/oracle/Avani/dbhome_1/network/admin
export PATH=$PATH:$ORACLE_HOME/bin:$LD_LIBRARY_PATH:.
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH:$ORACLE_HOME/bin
~/.bash_profile
#.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
export ORACLE_HOME=/home/oracle/Avani/dbhome_1
export ORACLE_SID=orcl
export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export NLS_LANG=American_America.UTF8
export TNS_ADMIN=/home/oracle/Avani/dbhome_1/network/admin
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH:$ORACLE_HOME/bin:$PATH:.
# User specific environment and startup programs
export PATH
/etc/systemd/system/httpd.service.d/httpd.conf
[Service]
Environment="LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib"
Environment="PATH=/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin"
使用 Oracle Instant Client 时切勿设置 ORACLE_HOME。 另外,对于 Instant Client,设置 ORACLE_SID 没有意义。 所以取消这些设置。 尝试使用安装文档中提到的 ldconfig 命令,而不是设置 LD_LIBRARY_PATH。
你的道路
/home/oracle/Avani/dbhome_1/lib/instantclient
看起来很可疑。 如果您已经安装了数据库,则无需安装Instant Client。 DB /lib 目录已经有正确的库。
总的来说,我建议安装 Instant Client RPM,而不是 Instant Client ZIP。 RPM 将在其安装过程中正确设置库搜索路径。 然后您可以取消设置所有 Oracle 环境变量。 通过 Apache 传递环境变量会很棘手,因为您需要将它们放入正确的文件中。 在 shell 中设置变量通常是不够的并且不起作用
您的代码中有一个小拼写错误,应该是
CONNECT_DATA
而不是 CONNE_DATA
。
// Connect to the Oracle database using oci_connect
$conn = oci_connect("toakdbi", "toakdbi_123", "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=AvaniServer)(PORT=1521)))(CONNECT_DATA=(SID=orcl)))");