编者按:遇到了就MEMO一下。
编辑|SQL和数据库技术(ID:SQLplusDB)
以前一直不觉得TWO_TASK和LOCAL这两个环境变量有啥用,
但是到了19c都是CDB/PDB的时候,因为连接PDB必须用TNS服务名,突然感觉又非常好用了。尤其对于一些升级为了维护兼容性的情况。
在Unix/Linux环境下,可以设置TWO_TASK环境变量。
在Windows环境下,可以设置LOCAL环境变量。
设置TWO_TASK/LOCAL的情况下,当用户连接数据库且没有指定TNS服务名时,会自动利用TWO_TASK/LOCAL的设置作为TNS服务名。
比如一般连接要
sqlplus user/password@pdb1
通过设置TWO_TASK=PDB1,就可以直接通过下面连接pdb1了。
sqlplus user/password
参考:
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:89412348059
If you are in a Unix environment, you can normally set 2 environment variables:
ORACLE_HOME
ORACLE_SID
that will connect you to a local database identified by that home and sid. If you wanted to be able to connect to a remote database or a database on that machine via SQLNet, you can also set:
TWO_TASK = <tnsconnect string>
The setting of TWO_TASK overrides the ORACLE_SID when set. You will not connecting to a local database with two_task but rather using sqlnet to connect to a remote database.
So, instead of:
$ sqlplus scott/tiger@some_db
I can:
$ setenv TWO_TASK some_db
$ sqlplus scott/tiger
and the @some_db is implied.
https://www.orafaq.com/wiki/TWO_TASK
TWO TASK
TWO_TASK (or LOCAL on Windows) is an environment variable used to specifies the default remote Oracle service. Connections will be made to a remote database (via SQL*Net) without specifying a service name (tnsnames.ora entry). It will appear to programs that they connect to a local database, while in fact they are routed to a remote database.
This environment variable is equivalent to the LOCAL registry entry on the Windows platforms. If both ORACLE_SID and TWO_TASK or LOCAL are defined, TWO_TASK or LOCAL takes precedence.
Usage
Windows:
set LOCAL=oraservice1
Unix/ Linux:
$ export TWO_TASK=oraservice1
or
$ setenv TWO_TASK oraservice1
Where 'oraservice1' is defined in the local TNSNAMES.ORA file or in an Names/LDAP server.
Example
One would normally connect to a remote database by specifying a service name (connect string). for example:
$ sqlplus scott/tiger@connect_str
By setting TWO_TASK you can omit the connect string (it is implied):
$ TWO_TASK=connect_str; export TWO_TASK
$ sqlplus scott/tiger