1.现象
SQL plus 连接实例报错,已确定实例是open状态。
[root@localhost ~]# su - oracle
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Sat Mar 15 10:20:56 2025
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
ERROR:
ORA-12162: TNS:net service name is incorrectly specified
Enter user-name:
2.原因分析
ORA-12162: TNS:net service name is incorrectly specified是由于客户端指定的TNS服务名错误导致的Oracle数据库错误。可能是没有指定正确的SID。
官方解释:
ORA-12162: TNS:net service name 指出了提供给客户端的TNS服务名(数据库连接字符串)不正确。这个错误通常出现在当客户端使用像SQLPlus这样的工具时,可以检查TNSNAMES.ORA文件,以检查是否存在名称冲突,判断是否正确指定了正确的服务名称。
[oracle@localhost ~]$ echo $ORACLE_SID
[oracle@localhost ~]$ export ORACLE_SID=study
[oracle@localhost ~]$ echo $ORACLE_SID
study
再次尝试连接:
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Sat Mar 15 10:23:27 2025
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
SQL> select name,open_mode from v$database;
NAME OPEN_MODE
--------- --------------------
STUDY READ WRITE
3.优化作业
修改 .bash_profile 文件
[oracle@localhost ~]$ vi .bash_profile
修改如下行内容:
export ORACLE_SID=study
[oracle@localhost ~]$ source .bash_profile
这样就不用经常执行 export ORACLE_SID=study 命令了。