ORA-12154使用tns别名连接时

时间:2012-11-10 19:31:54

标签: oracle oracle11g tnsnames ora-12154

我在Win7x64笔记本电脑上安装了Oracle 11.2,几乎一切正常。但有一点我不明白。我可以通过以下方式连接到示例HR模式:

sqlplus hr/password

但不是:

sqlplus hr/password@orcl

我得到ORA-12154: TNS:could not resolve the connect identifier specified

我偶然发现了这一点,因为我期待第二种选择是正确的。为什么我可以省略TNS别名?我是Oracle初学者,但是当我连接到其他环境时,需要TNS别名或者我无法建立连接。

我的tnsnames.ora中有以下内容:

LISTENER_ORCL = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))

ORACLR_CONNECTION_DATA = (DESCRIPTION =
  (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)))
  (CONNECT_DATA = (SID = CLRExtProc)(PRESENTATION = RO)))

ORCL, DUMMY = (DESCRIPTION =
  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
  (CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = orcl)))

我确信它已被使用,因为tnsping orcltnsping dummy都可以。有人可以向我解释一下吗?

更新1 lsnrctl status的输出如下:

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for 64-bit Windows: Version 11.2.0.1.0 - Production
Start Date                11-NOV-2012 00:43:22
Uptime                    0 days 0 hr. 15 min. 53 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   C:\app\Ronald\product\11.2.0\dbhome_1\network\admin\listener.ora
Listener Log File         c:\app\ronald\diag\tnslsnr\Ronald-PDC\listener\alert\log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1521ipc)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
  Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully

SELECT name FROM v$database的结果是一个数据库:ORCL

更新2 tnsping orcl的输出(为便于阅读而格式化)。

Used parameter files:
C:\app\Ronald\product\11.2.0\dbhome_1\network\admin\sqlnet.ora

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =
  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
  (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))
OK (20 msec)

1 个答案:

答案 0 :(得分:2)

您是否真的创建了名为ORCL的数据库实例?

使用命令

查看数据库的实际名称
lsnrctl status

这应该告诉你监听器知道哪些服务,这是你在使用@语法连接时所说的。在没有@ORCL的情况下连接的原因是,在这种情况下,您正在使用进程间通信(IPC)并绕过侦听器,因为数据库与SQL * Plus客户端会话在同一台计算机上。

在第一个示例中连接时,您还可以找到数据库名称:

SELECT name FROM v$database;

编辑:

尝试使用EZConnect语法,看看是否有效:

sqlplus user/pw@localhost/ORCL

请记住TNSPING除了联系监听器之外什么都不做 - 它不验证可以访问数据库。

还有一件事:看起来TCP可能不是侦听器配置的协议?检查你的listener.ora文件(与你的tnsnames.ora文件在同一目录中)。 LISTENER的条目应如下所示:

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

我的TNSPING输出在输出开始附近的一行中看起来有点不同:

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=1521)))