代码改变世界

记录一则数据库连接故障ORA-12560,ORA-12518

2016-02-18 13:18  AlfredZhao  阅读(...)  评论(...编辑  收藏

环境:Win Server 2008 R2 + Oracle 11.2.0.1
故障:客户反映数据库连接不上,本机sysdba和网络连接都连接不上。

一、确认故障

二、排查原因

一、确认故障

1.1 服务器尝试sysdba登录

``` PS C:\Users\Administrator> sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 2月 18 09:39:14 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

ERROR:
ORA-12560: TNS: 协议适配器错误

<h2 id="1.2">1.2 服务器尝试通过网络连接</h2>

PS C:\Users\Administrator> sqlplus system/oracle@xxoradb

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 2月 18 09:34:01 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

ERROR:
ORA-12518: TNS: 监听程序无法分发客户机连接

<h1 id="2">二、排查原因</h1>
<h2 id="2.1"> 2.1 ORA-12560错误初步排查原因 </h2>
ORA-12560错误官方说明:

$ oerr ora 12560
12560, 00000, "TNS:protocol adapter error"
// *Cause: A generic protocol adapter error occurred.
// *Action: Check addresses used for proper protocol specification. Before
// reporting this error, look at the error stack and check for lower level
// transport errors. For further details, turn on tracing and reexecute the
// operation. Turn off tracing when the operation is complete.

MOS相关文档:Bequeath connection fails with ORA-12560 on Windows (文档 ID 1986438.1)
可能原因:
> Environmental variables are not set correctly. This is a common issue on Windows.
> The following commands do not show proper settings:
>  echo %ORACLE_HOME%
>  echo %ORACLE_SID%
>  echo %PATH%

解决方法:
> Please do the following:
> 
>  set ORACLE_HOME=                               <<<<< enter the Oracle Home path here
>  set ORACLE_SID=                                <<<<< enter the Oracle SID name here
>  set PATH=%PATH%;                               <<<<< add the path of %ORACLE_HOME%/bin <<<<< this is the directory where "sqlplus.exe" exists
>  echo %ORACLE_HOME%                             <<<<< this verifies the Oracle home value
>  echo %ORACLE_SID%                              <<<<< this verifies the Oracle SID value

可是<font color="red">目前情况是,环境变量ORACLE_SID设置没有问题。而Windows环境,ORACLE_HOME并不需要在环境变量中设置,而是在注册表中</font>
关于这个ORACLE_HOME环境变量的问题可参考MOS的另一篇文章:How to Set or Switch Oracle Homes on Windows (Doc ID 969581.1)

由于业务目前已经是中断状态,所以可以重启监听和数据库服务,检查注册表信息,但问题依旧。
这里实际走了弯路,因为sqlplus命令是正常使用的,所以在环境变量方面就没多想PATH的问题。

<h2 id="2.2">2.2 ORA-12518错误排查原因 </h2>
ORA-12518错误官方说明:

$ oerr ora 12518
12518, 00000, "TNS:listener could not hand off client connection"
// *Cause: The process of handing off a client connection to another process
// failed.
// *Action: Turn on listener tracing and re-execute the operation. Verify
// that the listener and database instance are properly configured for
// direct handoff. If problem persists, call Oracle Support.
// *Comment: The problem can be worked around by configuring dispatcher(s)
// to specifically handle the desired presentation(s), and connecting
// directly to the dispatcher, bypassing the listener.

查看当前监听配置文件:

listener.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora

Generated by Oracle configuration tools.

SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
)
)

LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = WIN-1XXXXXXXXXX)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC521))
)
)

ADR_BASE_LISTENER = D:\app\Administrator

修改监听配置文件加入静态监听配置内容:

listener.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora

Generated by Oracle configuration tools.

SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
)
(SID_DESC =
(GLOBAL_DBNAME = xxoradb)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(SID_NAME = xxoradb)
)
)

LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = WIN-1XXXXXXXXXX)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC521))
)
)

ADR_BASE_LISTENER = D:\app\Administrator

重启监听后再测试,发现加入静态监听后可以连接到数据库了,同时服务器上的PL/SQL工具也可以正常连接到数据库了。

C:\Users\Administrator>sqlplus system/oracle@xxoradb

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 2月 18 10:52:24 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

<font color="red">这样,就先解决了客户端网络连接的问题。</font>
<h2 id="2.3">2.3 ORA-12560错误再次排查原因</h2>上面解决了客户端的连接问题,但问题还没有完美解决,因为目前本地sysdba仍然还是无法登陆,依然报错ORA-12560:

C:\Users\Administrator>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 2月 18 10:59:11 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

ERROR:
ORA-12560: TNS: 协议适配器错误

请输入用户名:

<font color="red">这时候就怀疑可能是机器上安装了多个Oracle程序导致,也就是说这个sqlplus命令可能是某个客户端的sqlplus程序,</font>
下面验证下是不是这个情况:

看到Oracle产品目录下,11.2.0文件夹下有多个文件夹存在

D:\app\Administrator\product\11.2.0>dir
驱动器 D 中的卷没有标签。
卷的序列号是 0A43-E08A

D:\app\Administrator\product\11.2.0 的目录

2016/01/26 12:26

.
2016/01/26 12:26 ..
2016/01/26 12:35 client_1
2016/01/26 12:23 dbhome_1
2016/01/26 12:25 dbhome_2
0 个文件 0 字节
5 个目录 200,137,936,896 可用字节

看起来的确安装有多个Oracle软件,从命名规则来看,应该是1个客户端,两个DB。

查看当前系统注册表中ORACLE_HOME的值为`D:\app\Administrator\product\11.2.0\dbhome_1`
那么我们使用的sqlplus程序是不是这个路径下的呢?
进一步查看系统环境变量PATH的值,

D:\app\Administrator\product\11.2.0\client_1\BIN;D:\app\Administrator\product\11.2.0\dbhome_1\BIN;D:\app\Administrator\product\11.2.0\dbhome_1;C:\Program Files (x86)\Common Files\NetSarang;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

<font color="red">果然第一个查找路径是客户端的,不是我们想要的服务端程序路径,第二个路径才是。</font>
现在问题就明朗了,我们把客户端的这个路径`D:\app\Administrator\product\11.2.0\client_1\BIN;`删掉。
然后重新打开cmd,发现已经可以正常sysdba登录

C:\Users\Administrator>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期四 2月 18 11:16:53 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

最后,再确认下,PL/SQL等工具也可以重新正常登录。至此,问题完美解决。