使用TWO_TASK或者LOCAL环境变量



前一阵子,我遇到一个问题:  
rman target /  
链接如下:  
http://www.itpub.net/thread-1167136-1-1.html  
执行错误,必须输入rman target sys/xxx@yyy   

我记得我以前学习安装oracle 8i的时候,遇到的一个问题,就是ora-12560错误,就是在通过远程桌面登录服务器执行无法执行sqlplus user/passwd,必须加入sqlplus user/passwd@net_name.  

关于这个问题,如果google,在许多地方都可以看到这个问题的解决ora-12560,实际上当时我测试过,许多是无效的.如果使用远程桌面,至少在当时我没有解决这个问题的.今天测试终于知道答案,链接如下:  

http://davidyu720.itpub.net/post/31716/470434  
ORACLE8i本地登录错误ORA-12560: TNS: 协议适配器错误  
Windows2003上的ORACLE817,在服务器上不使用连接串登录,直接用SQLPLUS或SVRMGRL本地登录,却报错误ORA-12560: TNS: 协议适配器错误。  
找到原因:这是一台终端服务器,在远处终端中登录OS--再登录数据库时,会提示ORA-12560错误。直接在控制台中登录OS--再登录数据库时就正常。  
解决办法:无--也许这是8i自己的问题吧。同样的Windows环境下,9i就没有问题--因此就懒得找解决办法了。  

我通过vnc登录服务器,确实可以在服务端执行sqlplus user/passwd.而通过远程桌面确实不行.  

我记得当时在新闻组提问,对方给出的解决就是定义LOCAL环境变量,当时一试验真的可以,也没有再去想这个问题.  

这样在执行rman出错的时候,我自己也习惯的采用定义LOCAL变量的方式,至于这个变量的值是什么我自己也不清楚,我一直以为是oracle_sid. 在sqlplus时发现我发现连接的数据库是远程的数据库,因为我本机的10g,而远程的数据库是8i.  

当时事情太多,心里仅仅想着快点解决rman target /的问题.  

今天看书<Apress.Expert.Oracle.Database.10g.Administration.Sep.2005.pdf>,才发现P428页:  
有如下论述:  

Using the TWO_TASK Environment Variable  
You can bypass the use of an Oracle Net name by setting the TWO_TASK environment variable (on UNIX/Linux) or the LOCAL environment variable (on Windows).  
The TWO_TASK environment variable specifies the connect string for connecting to a remote machine. SQL*Net will check the value of the TWO_TASK environment variable and automatically  
add it to your connect string, as shown in the following example:  
$ export TWO_TASK=mydb  
Once you set the TWO_TASK environment variable, you can connect to the mydb database in the  
following way:  
$ sqlplus scott/tiger  
Note that you didn’t have to use the specification sqlplus scott/tiger@mydb , since you’re using the TWO_TASK variable.On a Windows server, the following is the equivalent for setting the TWO_TASK environment variable:  
$ SET LOCAL=<mydb>  
$ sqlplus scott/tiger  

按照这个的介绍,如果我定义TWO_TASK(linux)或者LOCAL(windows)等于某个net_name,就可以实现在输入sqlplus 不需要在输入@net_name参数(当然服务端监听一定要起来的情况修下).测试一下,果然可以!  



在Unix和Linux环境下,可以设置TWO_TASK环境变量,当用户连接数据库且没有指定服务名时,会自动利用TWO_TASK的设置作为环境变量连接数据库。

 

 

当前主机有两个数据库在运行:

[oracle@bfapp2 ~]$ ps -ef|grep ora
oracle    3899     1  0 May17 ?        00:00:00 ora_pmon_demo2
oracle    3901     1  0 May17 ?        00:00:00 ora_dbw0_demo2
oracle    3903     1  0 May17 ?        00:00:01 ora_lgwr_demo2
oracle    3905     1  0 May17 ?        00:00:01 ora_ckpt_demo2
oracle    3907     1  0 May17 ?        00:00:01 ora_smon_demo2
oracle    3909     1  0 May17 ?        00:00:00 ora_reco_demo2
oracle    3911     1  0 May17 ?        00:00:00 ora_cjq0_demo2
oracle    3913     1  0 May17 ?        00:00:18 ora_qmn0_demo2
oracle    3915     1  0 May17 ?        00:00:00 ora_s000_demo2
oracle    3917     1  0 May17 ?        00:00:00 ora_d000_demo2
oracle    3942     1  0 May17 ?        00:00:00 /oracle/ora9/product/9.2/bin/tnslsnr LISTENER -inherit
oracle    4787     1  0 May17 ?        00:00:00 ora_pmon_demo
oracle    4789     1  0 May17 ?        00:00:01 ora_dbw0_demo
oracle    4791     1  0 May17 ?        00:00:00 ora_lgwr_demo
oracle    4793     1  0 May17 ?        00:00:00 ora_ckpt_demo
oracle    4795     1  0 May17 ?        00:00:02 ora_smon_demo
oracle    4797     1  0 May17 ?        00:00:00 ora_reco_demo
oracle    4799     1  0 May17 ?        00:00:00 ora_cjq0_demo
oracle    4801     1  0 May17 ?        00:00:00 ora_s000_demo
oracle    4803     1  0 May17 ?        00:00:00 ora_d000_demo
oracle    4807     1  1 May17 ?        00:17:53 ora_j000_demo
oracle    5175     1  0 May17 ?        00:00:01 oracledemo (LOCAL=NO)
root      8812  3444  0 16:02 ?        00:00:00 sshd: oracle [priv]
oracle    8814  8812  0 16:02 ?        00:00:00 sshd: oracle@pts/1
oracle    8815  8814  0 16:02 pts/1    00:00:00 -bash
oracle    8841  8815  0 16:44 pts/1    00:00:00 ps -ef
oracle    8842  8815  0 16:44 pts/1    00:00:00 grep ora

一个实例名为demo,另一个为demo2。

看看tnsnames.ora中的配置:

[oracle@bfapp2 ~]$ more $ORACLE_HOME/network/admin/tnsnames.ora
demo =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.13.149)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = demo)
    )
  )

demo2 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.13.149)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = demo2)
    )
  )

本地服务名中配置了DEMO和DEMO2两个服务名,分别对应DEMO和DEMO2两个数据库。

检查当前ORACLE_SID环境变量的设置:

[oracle@bfapp2 ~]$ env|grep SID
ORACLE_SID=demo2

当前环境变量中设置的SID是DEMO2,下面不指定服务名连接数据库:

[oracle@bfapp2 ~]$ sqlplus test/test

SQL*Plus: Release 9.2.0.4.0 - Production on 星期二 5月 18 16:45:02 2010

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


连接到: 
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> select * from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
DEMO2.US.ORACLE.COM

SQL> exit           
从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中断开
[oracle@bfapp2 ~]$ sqlplus test/test@demo

SQL*Plus: Release 9.2.0.4.0 - Production on 星期二 5月 18 16:45:27 2010

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


连接到: 
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> select * from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
DEMO

SQL> exit
从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中断开

当不指定服务名时,由于设置了ORACLE_SID=demo2,因此连接到DEMO2数据库。如果指定DEMO服务名,可以连接到DEMO数据库中。

下面设置TWO_TASK环境变量为demo:

[oracle@bfapp2 ~]$ export TWO_TASK=demo
[oracle@bfapp2 ~]$ sqlplus test/test

SQL*Plus: Release 9.2.0.4.0 - Production on 星期二 5月 18 16:45:50 2010

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


连接到: 
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> select * from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
DEMO

SQL> conn test/test@demo2
已连接。
SQL> select * from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
DEMO2.US.ORACLE.COM

由于设置了TWO_TASK,当不指定服务名,Oracle将TWO_TASK设置的变量作为默认服务名,因此连接到DEMO数据库中。如果指定服务名连接,则不受TWO_TASK环境变量的影响。

需要注意一点,使用了TWO_TASK环境变量后,无法使用操作系统验证登陆数据库:

SQL> conn / as sysdba
ERROR:
ORA-01031: insufficient privileges


警告: 您不再连接到 ORACLE。
SQL> conn /@demo2 as sysdba
ERROR:
ORA-01031: 权限不足


SQL> conn /@demo as sysdba
ERROR:
ORA-01031: insufficient privileges


SQL> exit

原因很简单,就是TWO_TASK环境变量的存在,使得SQLPLUS没有办法/ as sysdba登陆,而永远都是/@servicename as sysdba。

 


©著作权归作者所有:来自51CTO博客作者小麦苗DB宝的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. ssh登录日志收集
  2. 【故障处理】ORA-12545: Connect failed because target host or
  3. 简简单单教你设置 ssh 免密登录
  4. Servlet过滤器使用实例(防止用户恶意登录)
  5. Oracle的SYS用户登录报权限不足(ORA-01031: insufficient privile
  6. Uboot启动参数说明
  7. 【DB笔试面试265】在Oracle中,Oracle的SYS用户登录报权限不足的常
  8. 【DB笔试面试846】在Oracle中,TWO_TASK环境变量的作用是什么?
  9. 使用python的tkinter模块实现的GUI版用户登录程序(附源码)

随机推荐

  1. Android 修改EditText光标颜色
  2. Android系统Surface机制的SurfaceFlinger
  3. android之视频播放
  4. Android中drawable使用Shape资源
  5. Android:创建窗口式Activity
  6. 初始化有EditText或AutoCompleteEditText
  7. MPAndroidChart项目实战(七)——自定义横向
  8. android开发视频教程 android培训入门教
  9. Android多屏幕适配之字体大小、行间距和
  10. 关于android中的gif实现