首页 > 解决方案 > 无法从我的 PHP 脚本连接到 Oracle DB

问题描述

下面的 PHP 代码放在 /var/www/html/condb.php 文件中。

$conn = @oci_connect('sysdba', 'password', 'localhost/ORCL');
if (!$conn) {
die("Database Connection Error");

我正在使用 RHEL 7.2,并在我的 64 位机器上安装了 Oracle 12c。

我按照以下步骤安装 OCI:

1.yum install php-devel
2.pecl install oci8-2.0.10(在提示输入时提供了我的$ORACLE_HOME路径)
3.在/etc/php.ini中添加了extension=oci8.so条目
4.service httpd restart

输入时我可以看到oci8

root@oracle# php -m

现在,我可以在浏览器中查看放置在 /var/www/html/ 中的静态网页。但我无法通过上述脚本连接到 Oracle DB。

如果有人可以帮助我,那将是非常有帮助的。

如果在 DB 或 php.ini 文件中缺少任何配置,请指导。谢谢。

编辑 1:错误日志

[Sun Nov 25 18:15:37.769964 2018] [mpm_prefork:notice] [pid 14650] 
AH00170: caught SIGWINCH, shutting down gracefully
[Sun Nov 25 18:15:45.847496 2018] [suexec:notice] [pid 2853] AH01232: 
suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
AH00558: httpd: Could not reliably determine the servers fully 
qualified domain name, using fe80::72b8:d28b:5663:72bb. Set the 
'ServerName' directive globally to suppress this message
[Sun Nov 25 18:15:45.879946 2018] [auth_digest:notice] [pid 2853] 
AH01757: generating secret for digest authentication ...
[Sun Nov 25 18:15:45.880445 2018] [lbmethod_heartbeat:notice] [pid 
2853] AH02282: No slotmem from mod_heartmonitor
[Sun Nov 25 18:15:46.064159 2018] [mpm_prefork:notice] [pid 2853] 
AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) PHP/5.4.16 configured 
-- resuming normal operations
[Sun Nov 25 18:15:46.064191 2018] [core:notice] [pid 2853] AH00094: 
Command line: '/usr/sbin/httpd -D FOREGROUND'
[Sun Nov 25 18:15:49.588372 2018] [:error] [pid 3395] [client 
::1:49329] PHP Warning:  oci_connect(): Error while trying to retrieve 
text for error ORA-01804\n in /var/www/html/condb.php on line 8
[Sun Nov 25 18:15:54.600169 2018] [:error] [pid 3395] [client 
::1:49329] PHP Warning:  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 in /var/www/html/condb.php on l.

编辑2:我已经尝试在谷歌搜索上述错误时添加它:

在此路径 /etc/sysconfig/httpd 下面添加

ORACLE_HOME=/oracle/product/12.1.0/db_1
LD_LIBRARY_PATH = /oracle/product/12.1.0/db_1/lib:/lib:/usr/lib 
TNS_ADMIN=/oracle/product/12.1.0/db_1/network/admin 
ORACLE_SID=db1

我也更正了我的 /etc/hosts 以克服 httpd 错误

127.0.0.1    localhost.localdomain  localhost
<myip_here>   oracle_12C.localdomain  oracle_12C

在这些更改之后,我重新启动了“service httpd restart”。

现在,如果我运行放置在 /var/www/html/condb.php 文件中的 PHP 代码,我可以在 error_log 中看到下面的内容。

PHP Warning:  oci_connect(): ORA-12541: TNS:no listener

我的听众在听。请检查下面并纠正我。

[oracle@oracle-12c admin]$ lsnrctl status

LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 27-NOV-2018 18:10:28

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.143.244.1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 12.1.0.2.0 - Production
Start Date                27-NOV-2018 16:58:40
Uptime                    0 days 1 hr. 11 min. 47 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /oracle/product/12.1.0/db_1/network/admin/listener.ora
Listener Log File         /oracle/diag/tnslsnr/oracle-12c/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.143.244.1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=oracle-12c)(PORT=5500))(Security=(my_wallet_directory=/oracle/admin/orcl/xdb_wallet))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "db1", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "db1", status READY, has 1 handler(s) for this service...
Service "pdborcl" has 1 instance(s).
  Instance "db1", status READY, has 1 handler(s) for this service...
The command completed successfully

编辑 3

我已将代码更改如下:

$conn = oci_connect('sysdba', 'password', 'localhost:1539/ORCL');

现在 error_log 显示好像用户名/密码很好,我使用相同的方式通过 sys / 作为 sysdba 命令进行连接。

PHP Warning:  oci_connect(): ORA-01017: invalid username/password; logon denied 

编辑 4
正如一些视频所建议的,listener.ora 现在看起来像:

 LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.143.244.1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

#Added below part

SID_LIST_LISTENER =
(SID_LIST=
        ( SID_DESC =
                (SID_NAME =db1)
                (ORACLE_HOME = /oracle/product/12.1.0/db_1)
        )
)

仍然得到相同的“没有听众”。

标签: phporacle

解决方案


推荐阅读