杨大师(http://space.itpub.net/4227/viewspace-663069)介绍过TWO_TASK环境变量的作用,指出“在Unix和Linux环境下,可以设置TWO_TASK环境变量,当用户连接数据库且没有指定服务名时,会自动利用TWO_TASK的设置作为环境变量连接数据库。”,并用两个本地数据库为例说明了TWO_TASK的使用。
但TOM大师(http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:89412348059)也曾经回答过相关的问题,进一步指出TWO_TASK可以让你通过SQLNet连接一个本地库和一个远程库。
这里我用一个本地库与一个远程库的实例,再次验证下。
当前配置的两个连接串(一个本地数据库,一个远程数据库):
[oracle@liu admin]$ more tnsnames.ora
BISAL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = bisal)
)
)
TEST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = x.x.x.x)(PORT = 1524))
)
(CONNECT_DATA =
(SERVICE_NAME = star)
)
)
当前TWO_TASK环境变量为空:
[oracle@liu Desktop]$ echo $TWO_TASK
[oracle@liu Desktop]$
当未指定服务名连接数据库时,默认是bisal这个本地数据库,即由系统的ORACLE_SID指定:
[oracle@liu Desktop]$ echo $ORACLE_SID
bisal
[oracle@liu Desktop]$ env | grep SID
ORACLE_SID=bisal
[oracle@liu Desktop]$
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:38:08 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
BISAL
SQL>
设定TWO_TASK=TESTTKT:
[oracle@liu Desktop]$ export TWO_TASK=TESTTKT
[oracle@liu Desktop]$ echo $TWO_TASK
TESTTKT
[oracle@liu Desktop]$
[oracle@liu Desktop]$
使用TESTTKT的用户名登陆,但不指定服务名:
[oracle@liu Desktop]$ sqlplus star/star
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:39:31 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.7.0 - Production
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
ALL9IT2
SQL>
此时默认连接的就是TESTTKT这个远程库了。
此时关闭本地库bisal的监听:
LSNRCTL> stop bisal
验证监听状态:
[oracle@liu Desktop]$ tnsping bisal
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 11-AUG-2013 15:06:18
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
/opt/oracle/102/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = bisal)))
TNS-12541: TNS:no listener
再次连接bisal库:
[oracle@liu Desktop]$ sqlplus bisal/xxx@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 15:04:54 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ERROR:
ORA-12541: TNS:no listener
Enter user-name:
打开监听,再用@bisal的方式连接bisal本地库:
[oracle@liu Desktop]$ sqlplus bisal/xxx@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:40:08 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
BISAL
SQL>
但之前可以通过操作系统验证登陆的本地库sys账户:
[oracle@liu Desktop]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:40:37 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
禁止访问了。
只能通过如下方式访问之前的本地库sys账户:
[oracle@liu Desktop]$ sqlplus bisal/bisal@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:43:52 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> conn sys/oracle as sysdba
Connected.
SQL> show user
USER is "SYS"
SQL>
总结:
1、未设置TWO_TASK环境变量时,不指定服务名连接的默认是ORACLE_SID的值。
2、当设置TWO_TASK环境变量时,不指定服务名连接的默认是TWO_TASK的值。
3、当使用@服务名方式连接时,会忽略ORACLE_SID或TWO_TASK的值。
4、当使用TWO_TASK环境变量时,操作系统验证登录数据库的方式被禁止,提示ORA-01031: insufficient privileges。
5、因为TWO_TASK设置后,屏蔽了ORACLE_SID的作用,因此即使对于本地库,也需要通过SQLNet的方式连接,也就是需要监听,一般对于从本机访问本地库可能不打开监听,但此时需要打开监听,才能通过@服务名的方式连接到本地库。
6、补充:tnsnames.ora文件默认采用的路径如下:
$HOME(~)/.tnsnames.ora
$TNS_ADMIN/tnsnames.ora
/var/opt/oracle/tnsnames.ora or /etc/tnsnames.ora (depending on platform)
$ORACLE_HOME/network/admin/tnsnames.ora
7、关于TWO和TASK的来源,这个词来源于20世纪80年代,TWO TASK表示两个一起工作的含义,仅仅是意会,并不局限于两个。
8、个人理解,设置TWO_TASK,能够覆盖ORACLE_SID的作用,改变不带@服务名默认连接的数据库(本地or远程),要求所有的sqlplus连接都是通过SQLNet的方式,禁止操作系统验证。仅此而已。
但TOM大师(http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:89412348059)也曾经回答过相关的问题,进一步指出TWO_TASK可以让你通过SQLNet连接一个本地库和一个远程库。
这里我用一个本地库与一个远程库的实例,再次验证下。
当前配置的两个连接串(一个本地数据库,一个远程数据库):
[oracle@liu admin]$ more tnsnames.ora
BISAL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = bisal)
)
)
TEST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = x.x.x.x)(PORT = 1524))
)
(CONNECT_DATA =
(SERVICE_NAME = star)
)
)
当前TWO_TASK环境变量为空:
[oracle@liu Desktop]$ echo $TWO_TASK
[oracle@liu Desktop]$
当未指定服务名连接数据库时,默认是bisal这个本地数据库,即由系统的ORACLE_SID指定:
[oracle@liu Desktop]$ echo $ORACLE_SID
bisal
[oracle@liu Desktop]$ env | grep SID
ORACLE_SID=bisal
[oracle@liu Desktop]$
使用/的方式连接:
[oracle@liu Desktop]$ sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:38:08 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
BISAL
SQL>
设定TWO_TASK=TESTTKT:
[oracle@liu Desktop]$ export TWO_TASK=TESTTKT
[oracle@liu Desktop]$ echo $TWO_TASK
TESTTKT
[oracle@liu Desktop]$
[oracle@liu Desktop]$
使用TESTTKT的用户名登陆,但不指定服务名:
[oracle@liu Desktop]$ sqlplus star/star
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:39:31 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.7.0 - Production
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
ALL9IT2
SQL>
此时默认连接的就是TESTTKT这个远程库了。
此时关闭本地库bisal的监听:
LSNRCTL> stop bisal
验证监听状态:
[oracle@liu Desktop]$ tnsping bisal
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 11-AUG-2013 15:06:18
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
/opt/oracle/102/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = bisal)))
TNS-12541: TNS:no listener
再次连接bisal库:
[oracle@liu Desktop]$ sqlplus bisal/xxx@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 15:04:54 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ERROR:
ORA-12541: TNS:no listener
Enter user-name:
打开监听,再用@bisal的方式连接bisal本地库:
[oracle@liu Desktop]$ sqlplus bisal/xxx@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:40:08 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
BISAL
SQL>
但之前可以通过操作系统验证登陆的本地库sys账户:
[oracle@liu Desktop]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:40:37 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ERROR:
ORA-01031: insufficient privileges
Enter user-name:
禁止访问了。
只能通过如下方式访问之前的本地库sys账户:
[oracle@liu Desktop]$ sqlplus bisal/bisal@bisal
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Aug 11 14:43:52 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> conn sys/oracle as sysdba
Connected.
SQL> show user
USER is "SYS"
SQL>
总结:
1、未设置TWO_TASK环境变量时,不指定服务名连接的默认是ORACLE_SID的值。
2、当设置TWO_TASK环境变量时,不指定服务名连接的默认是TWO_TASK的值。
3、当使用@服务名方式连接时,会忽略ORACLE_SID或TWO_TASK的值。
4、当使用TWO_TASK环境变量时,操作系统验证登录数据库的方式被禁止,提示ORA-01031: insufficient privileges。
5、因为TWO_TASK设置后,屏蔽了ORACLE_SID的作用,因此即使对于本地库,也需要通过SQLNet的方式连接,也就是需要监听,一般对于从本机访问本地库可能不打开监听,但此时需要打开监听,才能通过@服务名的方式连接到本地库。
6、补充:tnsnames.ora文件默认采用的路径如下:
$HOME(~)/.tnsnames.ora
$TNS_ADMIN/tnsnames.ora
/var/opt/oracle/tnsnames.ora or /etc/tnsnames.ora (depending on platform)
$ORACLE_HOME/network/admin/tnsnames.ora
7、关于TWO和TASK的来源,这个词来源于20世纪80年代,TWO TASK表示两个一起工作的含义,仅仅是意会,并不局限于两个。
8、个人理解,设置TWO_TASK,能够覆盖ORACLE_SID的作用,改变不带@服务名默认连接的数据库(本地or远程),要求所有的sqlplus连接都是通过SQLNet的方式,禁止操作系统验证。仅此而已。