一、动态监听

oracle监听器是一个独立的进程,通常在数据库实例启动前先启动,当oracle实例在open时,会动态向监听器程序注册其service_names和instance_name。实例启动的时候,PMON进程会根据instance_name,service_name参数将实例和服务动态注册到listerer中。

1. listener.ora默认配置文件

# cat /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora

SID_LIST_LISTENER =  
  (SID_LIST =    
    (SID_DESC =    
      (SID_NAME = PLSExtProc)    
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)    
      (PROGRAM = extproc)    
    )    
  )

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

2. 查看监听状态,可以通过status READY状态看到是采用的动态监听。

[oracle@node1 ~]$ lsnrctl status     

LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 21-11014 22:27:29

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=node1)(PORT=1521)))  
STATUS of the LISTENER    
------------------------    
Alias                     LISTENER    
Version                   TNSLSNR for Linux: Version 10.2.0.5.0 - Production    
Start Date                21-11014 22:26:37    
Uptime                    0 days 0 hr. 0 min. 52 sec    
Trace Level               off    
Security                  ON: Local OS Authentication    
SNMP                      OFF    
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora    
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log    
Listening Endpoints Summary...    
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=node1)(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...    
The command completed successfully    
[oracle@node1 ~]$

3. 监听启动与关闭

$ lsnrctl start  
$ lsnrctl stop

4. 手工动态注册监听

数据库在监听器运行之后,open数据库时动态注册才会进行,如果监听器重启,动态注册信息将会暂时丢失,直到PMON进程会周期性扫描实例中service_names和instance_name后再进行动态注册。也可以手动注册,仅限于默认监听。

SQL> alter system register;

 

二、静态监听

静态注册就是通过实例启动时读取listener.ora文件的配置,将实例GLOBAL_DBNAME和服务注册到监听程序。无论数据库是否已启动,默认地都有两条信息注册到监听器中,可以随时保证可以远程通过监听连接到数据库。

1. listener.ora配置静态监听配置文件

# cat /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora

SID_LIST_LISTENER =  
  (SID_LIST =    
    (SID_DESC =    
      (SID_NAME = PLSExtProc)    
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)    
      (PROGRAM = extproc)    
    )    
    (SID_DESC =      
      (SID_NAME = orcl)        
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)        
      (SID_NAME = orcl)        
    )
   
  )

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

2. 查看监听状态,可以通过status UNKNOWN状态看到是采用的动态监听。

[oracle@node1 admin]$ lsnrctl status

LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 21-11014 22:58:16

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=node1)(PORT=1521)))  
STATUS of the LISTENER    
------------------------    
Alias                     LISTENER    
Version                   TNSLSNR for Linux: Version 10.2.0.5.0 - Production    
Start Date                21-11014 22:58:04    
Uptime                    0 days 0 hr. 0 min. 11 sec    
Trace Level               off    
Security                  ON: Local OS Authentication    
SNMP                      OFF    
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora    
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log    
Listening Endpoints Summary...    
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=node1)(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 UNKNOWN, has 1 handler(s) for this service...    
The command completed successfully    
[oracle@node1 admin]$

3. 如果在实际环境中在客户端访问数据库时出现如下错误提示,可以采用静态监听的方式去解决。

ORA-12514: TNS :监听程序当前无法识别连接描述符中请求的服务_监听程序不支持服务

 

三、监听中配置(IP=FIRST)参数作用

1,在没有配置IP=FIRST之前服务器监听器会在本机所有IP地址1521端口上进行监听,如果本机上有多个IP地址,可以访问。

[root@node1 ~]# netstat -tunlp |grep 1521  
tcp        0      0 0.0.0.0:1521                0.0.0.0:*                   LISTEN      4071/tnslsnr       

2. 配置(IP=FIRST)

$ cat /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora

SID_LIST_LISTENER =  
  (SID_LIST =    
    (SID_DESC =    
      (SID_NAME = PLSExtProc)    
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)    
      (PROGRAM = extproc)    
    )    
    (SID_DESC =    
      (SID_NAME = orcl)    
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)    
      (SID_NAME = orcl)    
    )    
  )

LISTENER =  
  (DESCRIPTION_LIST =    
    (DESCRIPTION =    
      (ADDRESS = (PROTOCOL = TCP)(HOST = node1)(PORT = 1521)(IP=FIRST))    
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))    
    )    
  )

3. 重启监听后的效果,本机主机名配置在127.0.0.1上,监听应该在127.0.0.1上, 也可以指定本机上一个IP地址做为监听地址,这里仅是示例。

$ cat /etc/hosts  
127.0.0.1               node1 localhost.localdomain localhost

$ lsnrctl stop  
$ lsnrctl start

$ netstat -tunlp |grep 1521  
tcp        0      0 127.0.0.1:1521              0.0.0.0:*                   LISTEN      4157/tnslsnr