ORA-12154

解决ORA-12154错误

From:http://blog.sina.com.cn/s/blog_4e5afd3601009e92.html

 

Oracle数据库的连接配置,做好在同一网段内调试。如果跨网段,就确定ip 1521端口是否开放。我就尝试过被网管玩了一把,真是TMD。

 

ORA-12154: TNS:could not resolve service name的解决方法

今天测试人员在部署新的应用时,应用服务器连接不到数据库
[oracle@localhost admin]$ sqlplus hangan/hangan@hatest
 
SQL*Plus: Release 9.2.0.4.0 - Production on Thu Apr 26 16:24:35 2007
 
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
 
ERROR:
ORA-12154: TNS:could not resolve service name
错误指出客户不能查找到tnsnames.ora文件中所列举的服务。
查错误码显示:
ORA-12154: TNS:could not resolve service name
Cause: Oracle Net could not locate the net service name specified in the tnsnames.ora configuration file.
Action: Perform these steps:
1.      Verify that a tnsnames.ora file exists.
See Also:
"Localized Configuration File Support" for configuration file location information
2.      Verify that there are not multiple copies of the tnsnames.ora file.
3.      In the tnsnames.ora file, verify that the net service name specified in your connect string is mapped to a connect descriptor.
4.      Verify that there are no duplicate copies of the sqlnet.ora file.
5.      If you are using domain names, verify that your sqlnet.ora file contains a NAMES.DEFAULT_DOMAIN parameter. If this parameter does not exist, you must specify the domain name in your connect string.
6.      If you are not using domain names, and this parameter exists, delete it or disable it by commenting it out.
7.      If you are connecting from a login dialog box, verify that you are not placing an "@" symbol before your connect net service name.
8.      Activate client tracing and repeat the operation.
Cause: Oracle Net could not locate the database service name or net service name specified in the directory server.
Action: Perform these steps:
1.      Verify that the database service or net service name entry exists in the directory that this computer was configured to use.
See Also:
Chapter 8, "Setting Up Directory Server Usage" for directory setup instructions
2.      Verify that the sqlnet.ora file includes the following entry: NAMES.DIRECTORY_PATH=(ldap, other_naming_methods)
根据文档中的提示第三步3.(1.2没问题了)检查tnsnames.ora文件中的服务名称是否正确的配置,经过检查(SERVICE_NAME = hatest)没有问题(更没有语法错误)
然后按照文档做第4.5步,到第6步时发现问题,sqlnet.ora文件中引用了domain域(注:我们这边没使用域)
[oracle@localhost admin]$ cat sqlnet.ora
# SQLNET.ORA Network Configuration File: /home/oracle/OraHome1/network/admin/sqlnet.ora
# Generated by Oracle configuration tools.
 
NAMES.DEFAULT_DOMAIN = localdomain
 
NAMES.DIRECTORY_PATH= (TNSNAMES, ONAMES, HOSTNAME)--这个没问题啦
好了那还等什么编辑该文件把domain干掉。编辑完后赶快tnsping hatest
[oracle@localhost admin]$ tnsping hatest
 TNS Ping Utility for Linux: Version 9.2.0.4.0 - Production on 26-APR-2007 16:33:39
 Copyright (c) 1997 Oracle Corporation.  All rights reserved.
 Used parameter files:
/home/oracle/OraHome1/network/admin/sqlnet.ora
 Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.248.155)(PORT = 1521))) (CONNECT_DATA = (SID = hatest) (SERVER = DEDICATED)))
OK (30 msec)
没问题通了。在连接数据库
[oracle@localhost admin]$ sqlplus hangan/hangan@hatest
 SQL*Plus: Release 9.2.0.4.0 - Production on Thu Apr 26 16:33:49 2007
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
 
SQL> exit
<think>好的,我现在需要帮助用户解决ORA-12154错误的问题。首先,我得先了解这个错误的具体情况。根据用户提供的引用内容,ORA-12154通常发生在Oracle数据库连接时,提示无法解析连接标识符。用户提到TNSPING正常,但sqlplus报错,同时其他工具如dbeaver或PL/SQL Developer可以登录,这说明问题可能出在特定的客户端配置上。 首先,我应该回顾ORA-12154的常见原因。根据引用中的信息,可能的原因包括TNS_ADMIN环境变量未正确设置、tnsnames.ora文件路径错误、文件权限问题或连接字符串格式错误。用户提供的引用[3]和[4]都提到了TNS_ADMIN的设置问题,特别是要指向包含tnsnames.ora的目录,通常是在NETWORK/ADMIN下。 接下来,我需要整理解决步骤。第一步是检查TNS_ADMIN环境变量是否正确设置。用户可能在使用sqlplus时没有正确配置这个变量,导致无法找到tnsnames.ora。例如,引用[3]中提到TNS_ADMIN指向F:\Program\instantclient_11_2\NETWORK\ADMIN,而引用[4]也强调了这一点。如果这个变量未设置或路径错误,sqlplus就无法解析连接标识符。 第二步是验证tnsnames.ora文件是否存在且配置正确。用户需要确认文件中的服务名是否正确,是否有语法错误。有时候,虽然TNSPING能成功,但实际连接时使用的服务名可能有拼写错误。例如,引用[2]中的示例显示了一个可能的配置,但用户需要确保自己的配置与之匹配。 第三步是检查文件权限问题。如果tnsnames.ora或其所在目录的权限不足,可能导致sqlplus无法读取。这在Linux系统中比较常见,如引用[2]提到的环境,但Windows下也可能存在类似问题。 第四步是确认连接字符串格式是否正确。用户在使用sqlplus时,可能需要使用正确的语法,比如sqlplus username/password@service_name。如果服务名错误或格式不对,也会引发此错误。 另外,用户提到在输入账号密码后出现ORA-12532错误,这可能是因为在连接过程中出现了其他问题,比如网络配置或监听程序的问题。但首要解决的是ORA-12154,因为它直接关系到连接标识符的解析。 还需要注意,不同客户端工具可能使用不同的配置方式。例如,PL/SQL Developer可能通过自己的设置找到tnsnames.ora,而sqlplus依赖于环境变量。这种情况下,用户需要确保sqlplus运行的环境中有正确的TNS_ADMIN设置。 最后,建议用户检查Oracle客户端的版本是否兼容,以及是否有多个客户端版本导致配置冲突。例如,同时安装了完整版Oracle客户端和Instant Client,可能会导致路径混乱。 总结一下,解决步骤应该包括检查环境变量、验证配置文件、检查权限、确认连接字符串,以及可能的客户端版本问题。需要逐步排除这些可能性,用户可能需要逐一尝试这些步骤来确定问题根源。</think>### ORA-12154错误原因及解决方案 ORA-12154错误表示Oracle客户端无法解析连接标识符(如服务名或别名),通常由以下原因导致: --- #### **原因分析** 1. **TNS配置问题** - `tnsnames.ora`文件路径未正确配置,或客户端未找到该文件[^3][^4] - 文件内容存在语法错误或服务名拼写错误[^2] 2. **环境变量未设置** - `TNS_ADMIN`环境变量未指向包含`tnsnames.ora`的目录[^3] 3. **权限问题** - `tnsnames.ora`文件或所在目录权限不足(Linux/Unix系统常见) 4. **连接字符串格式错误** - 使用`sqlplus`时未正确指定服务名或格式错误[^1] --- #### **解决步骤** 1. **检查`TNS_ADMIN`环境变量** - 确认变量指向`tnsnames.ora`所在目录(例如:`F:\Program\instantclient_11_2\NETWORK\ADMIN`)[^4] - Windows:通过系统属性设置;Linux:在`.bashrc`或`.profile`中添加`export TNS_ADMIN=/path/to/network/admin` 2. **验证`tnsnames.ora`文件** - 确保文件存在且包含正确的服务名配置,例如: ``` ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = orcl)) ) ``` - 检查服务名是否与连接字符串一致 3. **测试TNSPING** - 运行`tnsping <服务名>`,确认能解析到监听地址[^1] 4. **检查连接命令格式** - 使用`sqlplus`时,格式应为: ```bash sqlplus username/password@服务名 ``` - 若仍报错,尝试直接使用完整TNS描述符: ```bash sqlplus username/password@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=service)))" ``` 5. **排查权限问题** - 在Linux中,确保`tnsnames.ora`文件权限为`644`,目录权限为`755`[^2] 6. **多版本客户端冲突** - 若安装多个Oracle客户端(如Instant Client和完整版),确保`PATH`环境变量优先级正确 --- #### **典型场景示例** - **问题**:PL/SQL Developer可连接,但`sqlplus`报ORA-12154 **原因**:PL/SQL通过自身配置找到`tnsnames.ora`,而`sqlplus`依赖`TNS_ADMIN`环境变量 **解决**:为`sqlplus`设置`TNS_ADMIN`变量,或将其路径添加到系统`PATH` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值