listener之一二

一、註冊
    註冊就是將數據庫作爲一個服務註冊到監聽程序。客戶端不需要知道數據庫名和實例名,只需要

知道該數據庫對外提供的服務名就可以申請連接到數據庫。這個服務名可以與實例名一致,也可以不

一致,且可以註冊多個服務。
    註冊可以分爲靜態註冊與動態註冊,下面分別介紹靜態註冊和動態註冊。
二、靜態註冊
    靜態註冊就是預先配置好listener.ora文件,配置信息如下:
#listener.ora
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2/db_1)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (SID_NAME = orcl)##數據庫的實例名
      (ORACLE_HOME = /u01/app/oracle/product/10.2/db_1)##監聽程序所在路徑
      (GLOBAL_DBNAME = test01)##數據庫對外提供的服務名
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db01)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )

當用戶通過客戶端申請連接數據庫時,首先通過監聽程序讀取listener.ora中的配置信息(無論數據庫

是否啓動,用戶的連接請求都會通過監聽程序的檢查,因爲靜態監聽是無法識別數據庫的啓動狀態的

,只有用戶試圖連接數據庫時,數據庫纔會返回相關信息。)

下面我們來看下數據庫處於打開和關閉狀態時,服務test01的實例狀態:
1、打開狀態
[oracle@db01 admin]$ lsnrctl status

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 16-OCT-2013 06:58:27

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db01)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                16-OCT-2013 06:39:09
Uptime                    0 days 0 hr. 19 min. 18 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2/db_1/network/log/listener.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db01)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl_XPT" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "test01" has 1 instance(s).
  Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...   

=>實例orcl的狀態爲unknown
The command completed successfully

此時當用戶通過客戶端連接數據庫時:
SQL> conn system/hat@test01
已連接。

2、關閉狀態:
[oracle@db01 admin]$ lsnrctl status

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 16-OCT-2013 06:54:44

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db01)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                16-OCT-2013 06:39:09
Uptime                    0 days 0 hr. 15 min. 35 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2/db_1/network/log/listener.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db01)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "test01" has 1 instance(s).
  Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...   

=>實例orcl的狀態爲unknown
The command completed successfully

此時當用戶通過客戶端連接數據庫時:
SQL> conn system/hat@test01
ERROR:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux Error: 2: No such file or directory
由此可知,用戶的連接請求通過了監聽的檢查,但數據庫沒有打開,於是就返回了ora-01034的錯誤。

通過上面的實驗,驗證了當監聽靜態註冊時,服務的實例狀態均爲unknown,監聽程序無法知道數據庫

是否處於打開狀態。

三、動態註冊
    動態註冊就是不需預先配置listener.ora文件,而是instance在啓動的時候PMON進程根據

init.ora中的instance_name、service_names兩個參數將實例和服務動態的註冊到listener中。
    註冊到監聽器中的實例名從init.ora文件中的instance_name參數取得。如果該參數沒有設定值,

那麼它將取init.ora文件中的db_name值。
    註冊到監聽器中的服務名從init.ora文件中的參數service_names(該參數可以設置多個值,且值

以逗號分隔)和db_domain取得。如果service_names參數沒有設定值,數據庫將取init.ora文件中的

db_name值。
    採取動態註冊時,listener.ora配置如下:
#listener.ora
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2/db_1)
      (PROGRAM = extproc)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db01)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )

    動態註冊默認只註冊到默認的監聽器上(名稱是LISTENER、端口是1521、協議是TCP),如果需要向

非默認監聽註冊,則只需要配置local_listener參數。

四、如何區分靜態註冊與動態註冊
方法一:通過lsnrctl status監測
靜態註冊的監聽實例狀態爲unknown,動態註冊的監聽實例狀態爲ready
方式二:查看listener.ora文件
listener.ora文件中添加了如下信息的爲靜態註冊,沒有添加爲動態註冊
(SID_DESC =
  (SID_NAME = orcl)##數據庫的實例名
  (ORACLE_HOME = /u01/app/oracle/product/10.2/db_1)##監聽程序所在路徑
  (GLOBAL_DBNAME = test01)##數據庫對外提供的服務名
)

注:1、由於動態註冊監聽是通過pmon進程來動態添加的,因此一旦數據庫關閉,那麼動態註冊的服務便立即從監聽程序中消除。
2、如果想遠程啓動數據庫,需要通過靜態註冊。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章