使用TWO_TASK或者LOCAL環境變數

lhrbest發表於2017-06-03

使用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引數(當然服務端監聽一定要起來的情況修下).測試一下,果然可以!


UnixLinux環境下,可以設定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)
    )
  )

本地服務名中配置了DEMODEMO2兩個服務名,分別對應DEMODEMO2兩個資料庫。

檢查當前ORACLE_SID環境變數的設定:

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

當前環境變數中設定的SIDDEMO2,下面不指定服務名連線資料庫:

[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,當不指定服務名,OracleTWO_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

 






About Me

...............................................................................................................................

● 本文整理自網路

● 本文在itpub(http://blog.itpub.net/26736162)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● QQ群:230161599     微信群:私聊

● 聯絡我請加QQ好友(646634621),註明新增緣由

● 於 2017-06-02 09:00 ~ 2017-06-30 22:00 在魔都完成

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

...............................................................................................................................

拿起手機使用微信客戶端掃描下邊的左邊圖片來關注小麥苗的微信公眾號:xiaomaimiaolhr,掃描右邊的二維碼加入小麥苗的QQ群,學習最實用的資料庫技術。

使用TWO_TASK或者LOCAL環境變數
DBA筆試面試講解
歡迎與我聯絡

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2140246/,如需轉載,請註明出處,否則將追究法律責任。

相關文章