Oracle 11g PHP oci_connect 失败 OCIEnvNlsCreate()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16345515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:56:59  来源:igfitidea点击:

Oracle 11g PHP oci_connect fails OCIEnvNlsCreate()

phporacle

提问by seedhom

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

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

Error: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories

错误:oci_connect():OCEInvNlsCreate() 失败。您的系统有问题 - 请检查 ORACLE_HOME 和 LD_LIBRARY_PATH 是否已设置并指向正确的目录

phpinfo() relevant sections:

phpinfo() 相关部分:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

As you see, the environment variables are correctly set, and I can echo them on the page with with 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.

如您所见,环境变量设置正确,我可以在页面上用getenv(). 我还检查了读取权限,我可以从网页读取这些目录中的所有文件。客户端库文件在那里(我安装并重新安装了两次)完全安装。这些库是可读和可加载的。

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.

我尝试了从谷歌搜索中找到的所有技巧,但没有任何效果。
任何帮助表示赞赏。

回答by Hristo Valkanov

Got OCIEnvNlsCreate() failed? Welcome to hell...

OCIEnvNlsCreate() 失败了吗?欢迎来到地狱...

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". There is a wide range of solutions including, but not limited to configuring, reinstalling and praying to "insert random god name here".

让我们从问题开始。错误本身非常普遍。它的文档总结为“lib 崩溃了……不知道为什么”。解决方案很广泛,包括但不限于配置、重新安装和祈祷“在此处插入随机神名”。

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. A simple guide can be found hereor everywhere on the internet. Why?

2.从您的 php 文件中删除变量 LD_LIBRARY_PATH,如果存在(是的,它在 1 中显示的方式),并将其放入您的 apache2.conf 或 httpd.conf 或 /etc/environment 或您的发行版/版本的任何文件中有为环境变量。可以在此处或互联网上的任何地方找到简单的指南。为什么?

Do not set Oracle environment variables using putenv() in a PHP script because Oracle libraries may be loaded and initialized before your script runs. Variables set with putenv() may then cause conflicts, crashes, or unpredictable behavior. Some functions may work but others might give subtle errors. The variables should be set up before the web server is started.

不要在 PHP 脚本中使用 putenv() 设置 Oracle 环境变量,因为可能在脚本运行之前加载和初始化 Oracle 库。使用 putenv() 设置的变量可能会导致冲突、崩溃或不可预测的行为。某些功能可能有效,但其他功能可能会产生细微的错误。应在启动 Web 服务器之前设置变量。

as stated in the installation docs. Also restart apache afterwards. It's distribution/version based, but most probably the command is service httpd restart.

如安装文档中所述。之后也重新启动apache。它是基于发行版/版本的,但最有可能的命令是service httpd restart.

  1. Reinstall 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).

  2. Make sure u got the right LD_LIBRARY_PATH. Funny thing to say, right? You probably didn't notice that oracle created the folder client/lib/in N different locationswhich 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. Sanity check?

  3. Reinstall 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.

  1. 重新安装 oci8。需要多少次。不要强迫它。强迫只会让事情变得更糟。另外,尝试编译它(又名下载包使用./configure -'all the fancy commands you'll need to use'然后make install)。

  2. 确保你做对了LD_LIBRARY_PATH。说的很有趣,对吧?您可能没有注意到 oracleclient/lib/N 个不同位置创建了包含或多或少相同文件的文件夹,它们之间的唯一区别是选择这些文件夹中的一个除外将导致上述错误。完整性检查?

  3. 重新安装 php/apache。您可能有一些导致问题的旧安装。因此,使用apt-get purge php*oryum remove php*或任何对您的发行版执行相同技巧的方法将是一个好的开始。

EDITAfter PHP upgrade the problem reoccurred, this time with a different solution.

编辑PHP 升级后问题再次出现,这次使用不同的解决方案。

  1. EDIT SYSCONFIGEdit the file /etc/sysconfig/httpd. Add those 2 lines at the end:

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

  2. GIVE UP AND READ THE MANUALit can be found here.

  1. 编辑 SYSCONFIG编辑文件 /etc/sysconfig/httpd。在最后添加这两行:

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

  2. 放弃并阅读可在此处找到的手册

If none of those things help, feel free to browse the wall of sorrow, for a solution...

如果这些都没有帮助,请随时浏览悲伤,以寻求解决方案......

回答by sulica

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
  1. 错误:错误: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 将被授权连接数据库。

回答by Glauber Monteiro

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 hereto apache/bin

将即时客户端的所有文件在这里输入图像描述复制到 apache/bin

回答by Chad

I've dealt with these same issues myself except on an Ubuntu system. We got around it by setting the environment variables directly in /etc/environment and leaving them out of the apache config. Just add the variables at the bottom of the environment file and they'll apply system wide.

除了在 Ubuntu 系统上,我自己也处理过这些相同的问题。我们通过直接在 /etc/environment 中设置环境变量并将它们排除在 apache 配置之外来解决它。只需在环境文件底部添加变量,它们就会在系统范围内应用。

回答by Firman Syah

add your custom environment variable on apache init script. I'm using centos, so, init script is located on /etc/init.d/httpd

在 apache init 脚本上添加您的自定义环境变量。我使用的是 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

希望这有帮助

回答by IvanJijon

I just solved this same issue in :

我刚刚解决了同样的问题:

  • Apache
  • CentOS 7
  • PHP 5
  • Oracle 12cR1
  • 阿帕奇
  • CentOS 7
  • PHP 5
  • 甲骨文 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. There has to be a config file for nginx.

在 apache 中有一个文件,您可以在其中设置环境变量 : /etc/sysconfig/httpd. 必须有一个 nginx 的配置文件。

I added the required environment variables. Assuming that

我添加了所需的环境变量。假如说

  • ORACLE_HOME is /oracle/product/12.1.0/dbhome_1
  • Oracle client was installed under /oracle/product/12.1.0/client
  • ORACLE_HOME 是/oracle/product/12.1.0/dbhome_1
  • 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/httpdI just restarted Apache's service httpd and that was it:

更新后,/etc/sysconfig/httpd我刚刚重新启动了 Apache 的服务 httpd,就是这样:

$ sudo systemctl restart httpd

I hope it helps !

我希望它有帮助!