再說Oracle Listener動靜態註冊

realkid4發表於2012-09-23

 

監聽器ListenerOracle Client Process和例項遠端建立連線關係的必經之路。藉助ListenerClient Process可以實現和一個特定的Server Process建立連線關係,開啟Session會話,操作Oracle Instance

 

Oracle體系中,Listener是一個獨立的程式。Listener的執行是獨立於Oracle例項的。在Windows或者Unix/Linux架構中,監聽器都是被實現為一個獨立的系統程式Process。監聽器在工作中,是侍候在特定的埠Port上,接受Client Process通過Oracle Net協議訪問。同時,監聽器需要知道所在伺服器上的Oracle例項提供的Service Name和對應目錄,這個過程,我們稱之為“Register”(註冊)。

 

對於註冊過程,Oracle監聽器提供動靜態兩種註冊方法。不同的註冊方法有不同的配置方式和特點,也有各自獨特的使用場景。本篇就著重介紹一些動靜態註冊問題。

 

1Listener預設動態監聽配置

 

我們的實驗從筆者虛擬機器的實際配置開始,選擇Oracle版本為11gR2

 

 

SQL> select * from v$version;

 

BANNER

------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

PL/SQL Release 11.2.0.1.0 - Production

CORE        11.2.0.1.0         Production

 

 

使用lsnrctl命令檢視監聽器狀態。

 

 

[oracle@bsplinux ~]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 15:49:00

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

 

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bsplinux)(PORT=1521)))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                17-JUL-2012 19:52:13

Uptime                    65 days 19 hr. 56 min. 47 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1521)))

Services Summary...

Service "ora11g" has 1 instance(s).

  Instance "ora11g", status READY, has 1 handler(s) for this service...

Service "ora11gXDB" has 1 instance(s).

  Instance "ora11g", status READY, has 1 handler(s) for this service...

The command completed successfully

 

 

status命令引數結果輸出中,我們可以看到監聽器的工作狀態。此時,Listener使用Oracle NET引數檔案listener.ora,同時監聽器註冊兩個服務專案,名稱為ora11gora11gXDB。從兩個服務的READY狀態,可以知道使用動態註冊方法。

 

我們在作業系統層面檢視listener.ora檔案。

 

 

[oracle@bsplinux admin]$ pwd

/u01/app/oracle/network/admin

[oracle@bsplinux admin]$ ls -l

total 40

-rw-r--r-- 1 oracle oinstall  288 May 22 21:21 listener1205229PM2122.bak

-rw-r--r-- 1 oracle oinstall  288 May 22 19:57 listener.ora

drwxr-xr-x 2 oracle oinstall 4096 May 22 15:54 samples

-rw-r--r-- 1 oracle oinstall  187 May  9  2007 shrept.lst

-rw-r--r-- 1 oracle oinstall  310 May 22 21:22 tnsnames.ora

 

 

[oracle@bsplinux admin]$ cat listener.ora

# listener.ora Network Configuration File: /u01/app/oracle/network/admin/listener.ora

# Generated by Oracle configuration tools.

 

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = TCP)(HOST = bsplinux)(PORT = 1521))

    )

  )

 

ADR_BASE_LISTENER = /u01/app

 

 

Oracle Net Service三大配置檔案為:listener.orasqlnet.oratnsnames.ora。其中listener.ora定義了監聽器資訊。在上面的監聽器配置資訊中,定義了監聽器監聽埠1521(預設埠)。注意,檔案中沒有定義與ora11g例項有關的內容。這個就是使用了動態序號產生器制。

 

2、動態監聽

 

所謂動態監聽,就是在listener.ora檔案中,不需要定義例項Instance具體名稱和位置。Oracle例項會在執行之後,由後臺程式pmon會週期性的(1-3分鐘)到指定埠1521進行狀態資訊通知。Listener就會自動的進行更新Service情況。

 

在上一個部分,我們已經看到了存在listener.ora檔案情況下的動態註冊情況。事實上,如果沒有listener.ora檔案,我們也是可以實現預設的動態註冊監聽器的功能。

 

首先,我們刪除listener.ora檔案。

 

 

[oracle@bsplinux admin]$ mv listener.ora listener_bak.ora

[oracle@bsplinux admin]$ ls -l | grep listener.ora

[oracle@bsplinux admin]$

 

 

啟動監聽器。

 

 

[oracle@bsplinux admin]$ lsnrctl start

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 15:51:50

 

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

 

Starting /u01/app/oracle/bin/tnslsnr: please wait...

 

TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Log messages written to /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1521)))

 

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 15:51:51

Uptime                    0 days 0 hr. 0 min. 0 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1521)))

The listener supports no services

The command completed successfully

 

注意,上面的內容中沒有Listener Parameter的內容。監聽器在1521埠等待。過一分鐘左右,註冊內容顯示。

 

--Pmon存在

[oracle@bsplinux admin]$ ps -ef | grep pmon

oracle   10442 10359  0 15:52 pts/0    00:00:00 grep pmon

oracle   24187     1  0 Sep11 ?        00:02:33 ora_pmon_ora11g

 

[oracle@bsplinux admin]$ lsnrctl status

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 15:52:58

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

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 15:51:51

Uptime                    0 days 0 hr. 1 min. 7 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1521)))

Services Summary...

Service "ora11g" has 1 instance(s).

  Instance "ora11g", status READY, has 1 handler(s) for this service...

Service "ora11gXDB" has 1 instance(s).

  Instance "ora11g", status READY, has 1 handler(s) for this service...

The command completed successfully

 

 

動態註冊在實際中是非常有用的。pmon後臺程式是Oracle例項生死的重要指標,週期性的pmon註冊到Listener中,實際上也是實現了一個功能:例項生存狀態彙集。

 

當我們的例項崩潰,但是監聽器存活的時候,監聽器不能受到pmon的週期性註冊資訊。監聽器也就可以實現某種型別的failover

 

但是,動態註冊也不是萬能的。其中一個問題就是,動態註冊只能進行1521的埠註冊。如果我們變化埠,就不能實現動態註冊方式了。

 

我們修改listener.ora檔案中的埠資訊,如果使用非1521埠,就必須使用listener.ora配置。

 

 

 

[oracle@bsplinux admin]$ ls -l

total 44

-rw-r--r-- 1 oracle oinstall  288 May 22 21:21 listener1205229PM2122.bak

-rw-r--r-- 1 oracle oinstall  288 May 22 19:57 listener_bak.ora

-rw-r--r-- 1 oracle oinstall  288 Sep 21 15:56 listener.ora

drwxr-xr-x 2 oracle oinstall 4096 May 22 15:54 samples

-rw-r--r-- 1 oracle oinstall  187 May  9  2007 shrept.lst

-rw-r--r-- 1 oracle oinstall  310 May 22 21:22 tnsnames.ora

 

 

--修改引數埠為1522

[oracle@bsplinux admin]$ cat listener.ora

# listener.ora Network Configuration File: /u01/app/oracle/network/admin/listener.ora

# Generated by Oracle configuration tools.

 

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = TCP)(HOST = bsplinux)(PORT = 1522))

    )

  )

 

ADR_BASE_LISTENER = /u01/app

 

 

啟動監聽器,使用1522埠。

 

 

[oracle@bsplinux admin]$ lsnrctl start

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 15:58:05

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

 

Starting /u01/app/oracle/bin/tnslsnr: please wait...

 

TNSLSNR for Linux: Version 11.2.0.1.0 - Production

System parameter file is /u01/app/oracle/network/admin/listener.ora

Log messages written to /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bsplinux)(PORT=1522)))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 15:58:05

Uptime                    0 days 0 hr. 0 min. 0 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

The listener supports no services

The command completed successfully

 

 

等待三分鐘或者手工進行註冊,都不能讓1522埠的監聽器註冊內容。

 

 

--三分鐘和手工註冊嘗試

[oracle@bsplinux admin]$ lsnrctl status

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 16:01:12

 

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

 

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bsplinux)(PORT=1522)))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 15:58:05

Uptime                    0 days 0 hr. 3 min. 6 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

The listener supports no services

The command completed successfully

 

--手工註冊監聽器

SQL> conn / as sysdba

Connected.

SQL> alter system register;

System altered.

 

[oracle@bsplinux admin]$ lsnrctl status

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 16:02:42

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

 

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bsplinux)(PORT=1522)))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 15:58:05

Uptime                    0 days 0 hr. 4 min. 37 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

The listener supports no services

The command completed successfully

 

 

上面告訴我們,雖然動態註冊有很多優勢,但是隻能使用1521埠。這就會產生很大的限制。從安全形度看,我們通常要避免使用預設埠,防止惡意的埠掃描。

 

那麼,我們很多時候,就要選擇靜態註冊方法。

 

3、靜態註冊

 

靜態註冊是一種古老的註冊手段。簡單的說,就是監聽器的例項註冊資訊是直接寫在listener.ora檔案裡的。當監聽器啟動之後,Oracle會根據配置的listener.ora定義的服務去進行連線匹配。

 

這個過程中,一個重點在於:Oracle的註冊資訊是寫死在檔案裡面的。監聽器並不能像動態註冊那樣切實知道Oracle例項的情況,所以靜態註冊的狀態取值通常是UNKNOW

 

配置監聽器靜態註冊:

 

 

[oracle@bsplinux admin]$ cat listener.ora

# listener.ora Network Configuration File: /u01/app/oracle/network/admin/listener.ora

# Generated by Oracle configuration tools.

 

SID_LIST_LISTENER =

 (SID_LIST =

   (SID_DESC =

     (GLOBAL_DBNAME = ora11g)

     (ORACLE_HOME = /u01/app/oracle)

     (SID_NAME = ora11g)

   )

 )

 

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = TCP)(HOST = bsplinux)(PORT = 1522))

    )

  )

 

 

啟動監聽器:

 

 

[oracle@bsplinux admin]$ lsnrctl start

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 16:38:59

 

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

 

Starting /u01/app/oracle/bin/tnslsnr: please wait...

(篇幅原因,省略……)

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

Services Summary...

Service "ora11g" has 1 instance(s).

  Instance "ora11g", status UNKNOWN, has 1 handler(s) for this service...

The command completed successfully

 

--連線

SQL> conn scott/tiger@ora11g;

Connected.

 

 

靜態註冊的一個問題,在於監聽程式是不知道Oracle例項的真實生存情況。即使例項已經崩潰、關閉,監聽器也不能知道這個資訊。

 

 

--關閉Oracle伺服器

[oracle@bsplinux admin]$ sqlplus /nolog

 

SQL*Plus: Release 11.2.0.1.0 Production on Fri Sep 21 16:48:40 2012

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

 

SQL> conn / as sysdba

Connected.

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL>

 

--監聽器依然提供例項資訊;

[oracle@bsplinux admin]$ lsnrctl status

 

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-SEP-2012 16:50:19

 

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

 

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bsplinux)(PORT=1522)))

STATUS of the LISTENER

------------------------

Alias                     LISTENER

Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date                21-SEP-2012 16:38:59

Uptime                    0 days 0 hr. 11 min. 20 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /u01/app/oracle/network/admin/listener.ora

Listener Log File         /u01/app/diag/tnslsnr/bsplinux/listener/alert/log.xml

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=bsplinux)(PORT=1522)))

Services Summary...

Service "ora11g" has 1 instance(s).

  Instance "ora11g", status UNKNOWN, has 1 handler(s) for this service...

The command completed successfully

 

 

 

4、靜態註冊與遠端伺服器啟動

 

靜態註冊幫助我們解決了一個問題,就是我們如何在Oracle伺服器停機的時候,還能夠遠端通過Oracle Net連線到Oracle,並啟動伺服器。

 

在動態註冊時,我們遠端連線到監聽器時,監聽器是不能識別請求連線的名稱的。這就造成我們不能遠端登入。使用靜態註冊,恰恰可能幫助我們實現這個需求。

 

 

C:\Users\Tomas>tnsping ora11gp

 

TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 21-9 -

2012 09:38:25

 

Copyright (c) 1997, 2010, Oracle.  All rights reserved.

 

已使用的引數檔案:

D:\oracle\product\11.2.0\client_1\network\admin\sqlnet.ora

 

已使用 TNSNAMES 介面卡來解析別名

嘗試連線 (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.39.69)(PORT = 1522))) (CONNECT_DATA = (SERVICE_NAME = ora11g)))

OK (0 毫秒)

 

 

使用sqlplus進行連線。

 

 

C:\Users\Tomas>sqlplus /nolog

 

SQL*Plus: Release 11.2.0.1.0 Production on 星期五 9 21 09:38:39 2012

 

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

 

SQL> conn sys/oracle@ora11gp as sysdba

已連線到空閒例程。

SQL> startup

ORACLE 例程已經啟動。

 

Total System Global Area  422670336 bytes

Fixed Size                  1336960 bytes

Variable Size             318769536 bytes

Database Buffers           96468992 bytes

Redo Buffers                6094848 bytes

資料庫裝載完畢。

資料庫已經開啟。

SQL>

 

 

確定伺服器啟動。

 

 

[oracle@bsplinux ~]$ ps -ef | grep pmon

oracle   11002     1  0 16:55 ?        00:00:00 ora_pmon_ora11g

oracle   11147 10784  0 17:02 pts/1    00:00:00 grep pmon

 

 

5、結論

 

無論是動態註冊,還是靜態註冊,都是Oracle應對不同場景的解決方案。我們可以針對不同的情況,進行個性化選擇。

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

相關文章