简体   繁体   English

Oracle 11g PHP oci_connect 失败 OCIEnvNlsCreate()

[英]Oracle 11g PHP oci_connect fails OCIEnvNlsCreate()

Problem: Linux CentOS/Oracle 11g/InstantClient/PHP 5.3.3问题:Linux CentOS/Oracle 11g/InstantClient/PHP 5.3.3

Error: oci_connect(): OCIEnvNlsCreate() failed.错误:oci_connect():OCEInvNlsCreate() 失败。 There is something wrong with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories您的系统有问题 - 请检查 ORACLE_HOME 和 LD_LIBRARY_PATH 是否已设置并指向正确的目录

phpinfo() relevant sections: phpinfo() 相关部分:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

As you see, the environment variables are correctly set, and I can echo them on the page with with getenv() .如您所见,环境变量已正确设置,我可以使用getenv()在页面上回显它们。 I also checked the read permissions and I can read all the files in these directories from the webpage.我还检查了读取权限,我可以从网页读取这些目录中的所有文件。 The client library files are there (I installed and reinstalled twice) full install.客户端库文件在那里(我安装并重新安装了两次)完全安装。 The libraries are readable and loadable.这些库是可读和可加载的。

ldd did not show any errors. ldd 没有显示任何错误。

Here is the call from php file:这是来自 php 文件的调用:

$test = getenv('LD_LIBRARY_PATH')."  ".getenv('ORACLE_HOME');
$this->dbconn = oci_connect("myuser","mypassword", "localhost/orcl") or die("<b>Connection Failed: $test </b>").oci_error();

I tried all the tricks I found from googling, nothing worked.我尝试了从谷歌搜索中找到的所有技巧,但没有任何效果。
Any help is appreciated.任何帮助表示赞赏。

Got OCIEnvNlsCreate() failed? OCIEnvNlsCreate() 失败了吗? Welcome to hell...欢迎来到地狱...

I know that the thread is old, but the problem is still present.我知道线程很旧,但问题仍然存在。 And usually the answers to this problem redirect to third party sites, usually 404s .通常这个问题的答案会重定向到第三方网站,通常是 404s 。

Let's start with the problem.让我们从问题开始。 The error itself is pretty generic.错误本身非常普遍。 Its documentation sums up to "lib crashed...dunno why".它的文档总结为“lib 崩溃了……不知道为什么”。 There is a wide range of solutions including, but not limited to configuring, reinstalling and praying to "insert random god name here".解决方案很广泛,包括但不限于配置、重新安装和祈祷“在此处插入随机神名”。

Here is a short list of the most common solutions:以下是最常见解决方案的简短列表:

1. Put the variable in your .php file. 1. 将变量放入您的 .php 文件中。

    putenv("LD_LIBRARY_PATH=$newld");

where $newld is the link to your library.

2. Remove the variable LD_LIBRARY_PATH , if exists(yeah, the way it was shown in 1.), from your php file and place it into your apache2.conf or httpd.conf or /etc/environment or whatever file your distribution/version has for the environment variables. 2.从您的 php 文件中删除变量 LD_LIBRARY_PATH ,如果存在(是的,它在 1 中显示的方式),并将其放入您的 apache2.conf 或 httpd.conf 或 /etc/environment 或您的发行版/版本的任何文件中有为环境变量。 A simple guide can be found here or everywhere on the internet.可以在此处或互联网上的任何地方找到简单的指南。 Why?为什么?

Do not set Oracle environment variables using putenv() in a PHP script because Oracle libraries may be loaded and initialized before your script runs.不要在 PHP 脚本中使用 putenv() 设置 Oracle 环境变量,因为可能在脚本运行之前加载和初始化 Oracle 库。 Variables set with putenv() may then cause conflicts, crashes, or unpredictable behavior.使用 putenv() 设置的变量可能会导致冲突、崩溃或不可预测的行为。 Some functions may work but others might give subtle errors.某些功能可能有效,但其他功能可能会产生细微的错误。 The variables should be set up before the web server is started.应在启动 Web 服务器之前设置变量。

as stated in the installation docs.如安装文档中所述。 Also restart apache afterwards.之后也重新启动apache。 It's distribution/version based, but most probably the command is service httpd restart .它是基于发行版/版本的,但最有可能的命令是service httpd restart

  1. Reinstall oci8 .重新安装 oci8 As many times as it take.需要多少次。 Don't force it.不要强迫它。 Forcing only makes it worse.强迫只会让事情变得更糟。 Also, try to compile it (aka download the package use ./configure -'all the fancy commands you'll need to use' and then make install ).另外,尝试编译它(也就是下载包使用./configure -'all the fancy commands you'll need to use' ,然后make install )。

  2. Make sure u got the right LD_LIBRARY_PATH .确保你得到了正确的LD_LIBRARY_PATH Funny thing to say, right?说的很有趣,对吧? You probably didn't notice that oracle created the folder client/lib/ in N different locations which contains more or less the same files, and the only difference, between them, is that choosing all but one of those folders will result in the mentioned error .您可能没有注意到 oracle 在N 个不同位置创建了文件夹client/lib/ ,其中包含或多或少相同的文件,它们之间的唯一区别是选择这些文件夹中的一个除外将导致上述错误 Sanity check?完整性检查?

  3. Reinstall php/apache .重新安装 php/apache It's probable that you had some old installation that is causing issues.您可能有一些导致问题的旧安装。 So use apt-get purge php* or yum remove php* or whatever does the same trick for your distribution, will be a good start.因此,使用apt-get purge php*yum remove php*或任何对您的发行版有相同技巧的东西,将是一个好的开始。

EDIT After PHP upgrade the problem reoccurred, this time with a different solution.编辑PHP 升级后问题再次出现,这次使用不同的解决方案。

  1. EDIT SYSCONFIG Edit the file /etc/sysconfig/httpd.编辑 SYSCONFIG编辑文件 /etc/sysconfig/httpd。 Add those 2 lines at the end:在最后添加这两行:

    export LD_LIBRARY_PATH=/path/to.lib export ORACLE_HOME=/path/to/home导出 LD_LIBRARY_PATH=/path/to.lib 导出 ORACLE_HOME=/path/to/home

  2. GIVE UP AND READ THE MANUAL it can be found here .放弃并阅读可在此处找到的手册

If none of those things help, feel free to browse the wall of sorrow , for a solution...如果这些都没有帮助,请随时浏览悲伤,以寻求解决方案......

I've also dealt with these same issues myself and I found a solution.我自己也处理过这些相同的问题,并找到了解决方案。

The solution in my case was:在我的情况下的解决方案是:

  1. Error: Error: oci_connect(): OCIEnvNlsCreate() failed错误:错误:oci_connect():OCEInvNlsCreate() 失败

Grant permissions to client directory and files:授予对客户端目录和文件的权限:

sudo chmod -R 777 /usr/lib/oracle/11.2/client64

and

sudo chmod -R 777 /usr/include/oracle/11.2/client64

2.Error which I got later: ORA-12546: TNS:permission denied 2.我后来得到的错误:ORA-12546: TNS:permission denied

setsebool -P httpd_can_network_connect 1 

Apache would be authorized to connect the db. Apache 将被授权连接数据库。

You need to copy all content of the instant client to apache/bin您需要将即时客户端的所有内容复制到 apache/bin

im using xampp and working for me.我正在使用 xampp 并为我工作。

copy all files of the instant client enter image description here to apache/bin将即时客户端的所有文件在这里输入图像描述复制到 apache/bin

I've dealt with these same issues myself except on an Ubuntu system.除了在 Ubuntu 系统上,我自己也处理过这些相同的问题。 We got around it by setting the environment variables directly in /etc/environment and leaving them out of the apache config.我们通过直接在 /etc/environment 中设置环境变量并将它们排除在 apache 配置之外来解决它。 Just add the variables at the bottom of the environment file and they'll apply system wide.只需在环境文件底部添加变量,它们就会在系统范围内应用。

add your custom environment variable on apache init script.在 apache init 脚本上添加您的自定义环境变量。 I'm using centos, so, init script is located on /etc/init.d/httpd我使用的是 centos,因此,init 脚本位于 /etc/init.d/httpd

simply, copy paste your oracle user's .bash_profile onto /etc/init.d/httpd简单地,将您的 oracle 用户的 .bash_profile 复制粘贴到 /etc/init.d/httpd

this is mine.这是我的。

ORACLE_HOSTNAME=ora11g.home.com; export ORACLE_HOSTNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME
ORACLE_SID=ora11g; export ORACLE_SID
ORACLE_TERM=xterm; export ORACLE_TERM
PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export CLASSPATH

hope this help希望这有帮助

I just solved this same issue in :我刚刚解决了同样的问题:

  • Apache阿帕奇
  • CentOS 7 CentOS 7
  • PHP 5 PHP 5
  • Oracle 12cR1甲骨文 12cR1

The message says :消息说:

please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories请检查 ORACLE_HOME 和 LD_LIBRARY_PATH 是否设置并指向正确的目录

In apache, there is a file where you can set the environment variable : /etc/sysconfig/httpd .在 apache 中,有一个文件可以设置环境变量: /etc/sysconfig/httpd There has to be a config file for nginx.必须有一个 nginx 的配置文件。

I added the required environment variables.我添加了所需的环境变量。 Assuming that假如说

  • ORACLE_HOME is /oracle/product/12.1.0/dbhome_1 ORACLE_HOME 是/oracle/product/12.1.0/dbhome_1
  • Oracle client was installed under /oracle/product/12.1.0/client Oracle客户端安装在/oracle/product/12.1.0/client

Add :添加 :

ORACLE_HOME=/oracle/product/12.1.0/dbhome_1
LD_LIBRARY_PATH=/oracle/product/12.1.0/dbhome_1/lib:/oracle/product/12.1.0/dbhome_1/network/lib:/oracle/product/12.1.0/client/lib

I also added :我还补充说:

TNS_ADMIN=/oracle/product/12.1.0/dbhome_1/network/admin
ORACLE_SID=<your sid>

After updating /etc/sysconfig/httpd I just restarted Apache's service httpd and that was it:更新/etc/sysconfig/httpd我刚刚重新启动了 Apache 的服务 httpd,就是这样:

$ sudo systemctl restart httpd

I hope it helps !我希望它有帮助!

My solution for this php_warning was below:我对此 php_warning 的解决方案如下:

Added below lines to the httpd config and the warning dissapeared.在 httpd 配置中添加了以下几行,警告消失了。

# cat /etc/sysconfig/httpd  |grep -v "#"
LANG=C
ORACLE_HOME=/oracle/product/19
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib

The error was: [Wed Dec 02 10:06:27.291278 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_connect(): OCIEnvNlsCreate() failed.错误是:[Wed Dec 02 10:06:27.291278 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP 警告:oci_connect(): OCIEnvNlsCreate() 失败。 There is something wrong with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories in /var/www/html/websrv/ws_reg.php on line 57 [Wed Dec 02 10:06:27.291401 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_connect(): Error while trying to retrieve text for error ORA-01804\\n in /var/www/html/websrv/ws_reg.php on line 57 [Wed Dec 02 10:06:27.291531 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_close() expects parameter 1 to be resource, boolean given in /var/www/html/websrv/ws_reg.php on line 588您的系统有问题 - 请检查 ORACLE_HOME 和 LD_LIBRARY_PATH 是否已设置并指向第 57 行 [Wed Dec 02 10:06:27.291401 2020] [/var/www/html/websrv/ws_reg.php] 中的正确目录:error] [pid 1744] [client 127.0.0.1:39610] PHP 警告: oci_connect(): 尝试检索文本时出错 ORA-01804\\n in /var/www/html/websrv/ws_reg.php on line 57 [Wed Dec 02 10:06:27.291531 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP 警告:oci_close() 期望参数 1 为资源,布尔值在 /var/www/html 中给出/websrv/ws_reg.php 第 588 行

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM