【DB笔试面试846】在Oracle中,TWO_TASK环境变量的作用是什么?

时间:2022-07-22
本文章向大家介绍【DB笔试面试846】在Oracle中,TWO_TASK环境变量的作用是什么?,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题

在Oracle中,TWO_TASK环境变量的作用是什么?

答案

在Linux环境下,可以设置TWO_TASK环境变量,当用户连接数据库且没有指定服务名时,会自动利用TWO_TASK的设置作为环境变量连接数据库。其中,TWO_TASK的值为tnsnames.ora文件中配置的值。示例如下所示:

[LHRDB1:oracle]:/oracle>ORACLE_SID=
[LHRDB1:oracle]:/oracle>TWO_TASK=
[LHRDB1:oracle]:/oracle>sqlplus lhr/lhr
SQL*Plus: Release 11.2.0.4.0 Production on Mon Oct 31 16:17:38 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
ERROR:
ORA-12162: TNS:net service name is incorrectly specified
[LHRDB1:oracle]:/oracle>more $ORACLE_HOME/net*/ad*/tns*
# tnsnames.ora Network Configuration File: /oracle/app/oracle/product/11.2.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
lhrdb =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.59.130)(PORT = 1521)) 
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = lhrdb)
    )
  )
[LHRDB1:oracle]:/oracle>export TWO_TASK=lhrdb
[LHRDB1:oracle]:/oracle>sqlplus lhr/lhr
SQL*Plus: Release 11.2.0.4.0 Production on Mon Oct 31 16:17:53 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
LHR@lhrdb> 
[LHRDB1:oracle]:/oracle>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Mon Oct 31 16:18:07 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
ERROR:
ORA-01017: invalid username/password; logon denied
Enter user-name: 

需要注意的是,当配置了TWO_TASK环境变量后,就无法使用操作系统验证来登录数据库了,会报“ORA-01017: invalid username/password; logon denied”的错误。

类似地,在Windows环境下的变量是LOCAL,其设置方法和Linux下的TWO_TASK一致,如下所示:

C:Userslhr>sqlplus lhr/lhr
SQL*Plus: Release 11.2.0.1.0 Production on Mon Oct 31 16:28:25 2016
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
ERROR:
ORA-12560: TNS:protocol adapter error
Enter user-name:
C:Userslhr>more "%ORACLE_HOME%/network/admin/tnsnames.ora"
# tnsnames.ora Network Configuration File: D:apporacleproduct11.2.0.1networkadmintnsnames.ora
# Generated by Oracle configuration tools.
lhrdb =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 22.188.194.64)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = lhrdb)
    )
  )
C:Userslhr>set LOCAL=lhrdb
C:Userslhr>sqlplus lhr/lhr
SQL*Plus: Release 11.2.0.1.0 Production on Mon Oct 31 16:29:25 2016
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL> 

& 说明:

有关TWO_TASK和LOCAL的更多内容可以参考我的BLOG:http://blog.itpub.net/26736162/viewspace-2140246/、http://blog.itpub.net/26736162/viewspace-2123414/

本文选自《Oracle程序员面试笔试宝典》,作者:小麦苗

DB宝分享的IT资料:https://mp.weixin.qq.com/s/Iwsy-zkzwgs8nYkcMz29ag

● 本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用

● 作者博客地址:http://blog.itpub.net/26736162/abstract/1/

● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解

● 版权所有,欢迎分享本文,转载请保留出处