php oci_connect 连接失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14646007/
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
oci_connect connection failed
提问by shadyyx
I am having serious problem connecting to external ORA DB 11g from local Zend server CE.
OCI8 is enabled and running version 1.4.6 (due to phpinfo()).
我在从本地 Zend 服务器 CE 连接到外部 ORA DB 11g 时遇到严重问题。OCI8 已启用并运行版本 1.4.6(由于phpinfo())。
I have tried many connection options (listed below) with the same error returned:
我尝试了许多连接选项(如下所列),但返回了相同的错误:
oci_connect(): ORA-28547: connection to server failed, probable Oracle Net admin error
After googling for whole day I am only able to say that this error means that PHP was able to comunicate with the server but was unable to connect to a concrete service/database and that the error shouldn't come from PHP itself...
在谷歌搜索了一整天后,我只能说这个错误意味着 PHP 能够与服务器通信,但无法连接到具体的服务/数据库,并且错误不应该来自 PHP 本身......
I have set environment variable TNS_ADMINto c:\oracle_instantclient_11_2where the file tnsnames.orais located containing this connection description:
我已经设置环境变量TNS_ADMIN,以c:\oracle_instantclient_11_2该文件tnsnames.ora位于包含这方面的描述:
MYDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = X.X.X.X)(PORT = 1521))
    )
    (CONNECT_DATA = (SID = MYDB)(SERVER = DEDICATED))
  )
Using this description like
使用这样的描述
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=X.X.X.X)(PORT=1521)))(CONNECT_DATA=(SID=MYDB)(SERVER=DEDICATED)))
I am able to connect to the server and the service/database with sqlplusconsole, so the connection is very right. I am also using the very same HOST, PORT and SID to connect to the server with Sqldevelopertool. The problem is when connecting to the server within a PHP...
我可以使用sqlplus控制台连接到服务器和服务/数据库,因此连接非常正确。我还使用相同的 HOST、PORT 和 SID 通过Sqldeveloper工具连接到服务器。问题是在 PHP 中连接到服务器时...
What have I tried so far:
到目前为止我尝试了什么:
oci_connect("user", "password", "X.X.X.X:1521", "AL32UTF8", 0);
oci_connect("user", "password", "MYDB", "AL32UTF8", 0);
oci_connect("user", "password", "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=X.X.X.X)(PORT=1521)))(CONNECT_DATA=(SID=MYDB)(SERVER=DEDICATED)))", "AL32UTF8", 0);
All of these oci_connectcalls above return the same error mentioned.
oci_connect上面的所有这些调用都返回提到的相同错误。
I had also tried the ezconnectway for 11g as stated here- [//]host_name[:port][/service_name][:server_type][/instance_name]:
我也尝试过此处所述ezconnect的 11g 方法- :[//]host_name[:port][/service_name][:server_type][/instance_name]
oci_connect("user", "password", "X.X.X.X:1521/MYDB", "AL32UTF8", 0);
but the problem is I do not know the service name, only the service ID(SID), thus the error returned is this:
但问题是我不知道service name,只有service ID( SID),因此返回的错误是这样的:
oci_connect(): ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
that says there is no service running with the service name provided (or that the ORA listener does not know of such service).
这表示没有使用提供的服务名称运行的服务(或者 ORA 侦听器不知道此类服务)。
PHP version: 5.3.14
Appache v.: 2.2.22 (32bit) Zend
Zend server CE: 5.3.6
PHP 版本:5.3.14 
Appache v.:2.2.22(32 位)Zend 
Zend 服务器 CE:5.3.6
PHP info for OCI8:
OCI8 的 PHP 信息:
OCI8 Support                   enabled
Version                        1.4.6
Revision                       $Revision: 313688 $
Active Persistent Connections  0
Active Connections             0
Oracle Instant Client Version  Unknown
Temporary Lob support          enabled
Collections support            enabled
Directive                     Local Value   Master Value
oci8.connection_class         no value      no value
oci8.default_prefetch         100           100
oci8.events                   Off           Off
oci8.max_persistent           -1            -1
oci8.old_oci_close_semantics  Off           Off
oci8.persistent_timeout       -1            -1
oci8.ping_interval            60            60
oci8.privileged_connect       Off           Off
oci8.statement_cache_size     20            20
Maybe the problem is that there is unknownversion of Oracle instant clientthough it's path is set within both the TNS_ADMINand PATHenvironment variables...
也许问题是有一个unknown版本,Oracle instant client虽然它的路径是在TNS_ADMIN和PATH环境变量中设置的......
My question is:does anybody know of what have I done wrong? Am I missing something? I have googled for a whole day yesterday so probably (with 99% chance) any google links You would like to provide me with I have already seen and tried...
我的问题是:有人知道我做错了什么吗?我错过了什么吗?昨天我用谷歌搜索了一整天,所以可能(有 99% 的机会)你想提供给我的任何谷歌链接我已经看过并尝试过......
Though this question could be considered as an exact duplicate of this one- it has not been yet answered and I guess nobody will return back to that old question even if I post a comment I am having the connection problems too. Also keep in mind that in that similar question a different error is returned and asked about.
尽管这个问题可以被视为一个确切的重复这一个-它尚未回答,我想没有人会返回到那个老问题,即使我后我遇到的连接问题太多评论。还要记住,在那个类似的问题中,返回并询问了不同的错误。
回答by shadyyx
Due to several misconfigurations and 3 days lost while looking for a solution I moved to develop on Linux server and all of the problems are gone.
由于在寻找解决方案时出现了几次错误配置和 3 天的损失,我搬到了 Linux 服务器上进行开发,所有问题都消失了。
What I have found:
我发现了什么:
- both 
php_oci8.dllandphp_oci8_11g.dllare depending on the Oracle Instant Client libraries- these libraries does not contain 
oci_functions (likeoci_connect), onlyociXfunctions (likeociLogon) which is strange... 
 - these libraries does not contain 
 - though I am pretty sure I have downloaded Oracle Instant Client Basicand all of the extensions, I was not able to connect to another Oracle server due to unknown charset and the error was saying I am using only Liteinstant client...
 - I tried both 64bit and 32bit instant client version at no avail
 - my Apache is 64bit, windows is 64bit, PHP is 32bit, remote Oracle server is 64bit, remote Linux server is 64bit...
 - tried many environment settings (
ORA_HOME,TNS_ADMIN, adjustedPATHto look to instant client installation) at no avail - tried uninstalling local Oracle XE server due to possible environment settings interference at no avail
 - almost lost my head - at no avail...
 
- 双方
php_oci8.dll并php_oci8_11g.dll都依赖于Oracle即时客户端库- 这些库不包含
oci_函数(如oci_connect),只有ociX功能(像ociLogon),这是奇怪的... 
 - 这些库不包含
 - 虽然我很确定我已经下载了 Oracle Instant Client Basic和所有扩展,但由于字符集未知,我无法连接到另一个 Oracle 服务器,并且错误是说我只使用了Lite即时客户端......
 - 我尝试了 64 位和 32 位即时客户端版本都无济于事
 - 我的 Apache 是 64 位,windows 是 64 位,PHP 是 32 位,远程 Oracle 服务器是 64 位,远程 Linux 服务器是 64 位...
 - 尝试了许多环境设置(
ORA_HOME,TNS_ADMIN,调整PATH以查看即时客户端安装)无济于事 - 由于可能的环境设置干扰,尝试卸载本地Oracle XE服务器无济于事
 - 几乎失去了我的头 - 无济于事......
 
So finaly on Linux server I have no problems connecting to remote Oracle server. Somewhere (while surfing over thousands of PHP-Oracle related pages) I have found an information that "one shouldn't develop PHP application connecting to Oracle server under windows" and should stick to UNIX system instead...
所以最后在 Linux 服务器上我没有问题连接到远程 Oracle 服务器。在某处(在浏览数千个 PHP-Oracle 相关页面时)我发现了一条信息,“不应在 Windows 下开发连接到 Oracle 服务器的 PHP 应用程序”,而应该坚持使用 UNIX 系统......
So anybodyexperiencing similar or same problems - be so kind and do not waste Your time, install a VirtualBox, run Linux on it and move forward!
所以任何遇到类似或相同问题的人——请不要浪费你的时间,安装一个 VirtualBox,在它上面运行 Linux 并继续前进!
回答by Yasir Hashmi
to connect php to Oracle 11g version 11.2 you need to do following;
要将 php 连接到 Oracle 11g 11.2 版,您需要执行以下操作;
Step-1: login to you db with sys as sysdba and execute following scripts.
步骤 1:使用 sys 作为 sysdba 登录到您的数据库并执行以下脚本。
**
**
execute dbms_connection_pool.start_pool();
execute dbms_connection_pool.restore_defaults();
**
**
Step-2: in you PHP script
第 2 步:在你的 PHP 脚本中
**
**
$conn = oci_connect("username", "password", "//hostname/servicename");
if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
else {
   print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
**
**
Note: i). Make sure PHP_OCI8 and PHP_OCI8_11g exertions are enabled
注:一)。确保启用 PHP_OCI8 和 PHP_OCI8_11g 应用
ii). Oracle 11 is case sensitive.
ii). Oracle 11 区分大小写。
Best Regards Yasir Hashmi
最好的问候 Yasir Hashmi
回答by Developer
I have had the same issue and tried to connect from my local machine to a remote server. after 2 weeks of tring I finally got it to work.
我遇到了同样的问题,并试图从我的本地机器连接到远程服务器。经过 2 周的尝试,我终于让它工作了。
the solution is very simple but not documented in the PHP documentation
解决方案非常简单,但没有记录在 PHP 文档中
so let us take the sample PHP provided:
所以让我们以提供的示例 PHP 为例:
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
what they did not mention is that it points to the default port on the server.
他们没有提到的是它指向服务器上的默认端口。
if yours is set up to a different one you need to specify that. see the new example below:
如果您的设置为不同的,则需要指定。请参阅下面的新示例:
$conn = oci_connect('hr', 'welcome', 'localhost:1234/XE');
$conn = oci_connect('hr', 'welcome', 'localhost:1234/XE');
try that with your specified port.
用你指定的端口试试。
Hope this helps
希望这可以帮助
回答by Bren1818
Just adding my two cents, as I Banged my head against the wall with this one... If all else fails, try this, Once you have downloaded the instant client, http://www.oracle.com/technetwork/topics/winsoft-085727.html, copy it's extracted contents to the apache/bin folder. It'll likely ask you to over-write the oci.dll. Do so, then restart apache/php. With luck this will fix the problem...
只需添加我的两分钱,因为我用这个把我的头撞在墙上......如果一切都失败了,试试这个,一旦你下载了即时客户端,http://www.oracle.com/technetwork/topics/ winsoft-085727.html,将它提取的内容复制到 apache/bin 文件夹。它可能会要求您覆盖 oci.dll。这样做,然后重新启动 apache/php。幸运的是,这将解决问题......
Good luck.
祝你好运。
回答by Néstor Waldyd
My solution on fedora 17:
我在 Fedora 17 上的解决方案:
1. yum install httpd httpd-devel.
2. yum install php php-mysql php-pear php-devel
3. Install oracle instantclient:
rpm -Uvh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm 
rpm -Uvh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm 
4. pecl install oci8
This gives:
**
downloading oci8-1.4.7.tgz ...
Starting to download oci8-1.4.7.tgz (Unknown size)
.....done: 168,584 bytes
10 source files, building
running: phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
Please provide the path to the ORACLE_HOME directory.
Use 'instantclient,/path/to/instant/client/lib' if you're compiling
with Oracle Instant Client [autodetect] :' 
** 
Just press enter.
5. Enable the OCI8 extension by creating a file, oci8.ini for example, with the following line at /etc/php.d/:
extension=oci8.so
6. service httpd restart

