Oracle 数据库Listener动态注册和静态注册
一 注册概述
注册就是将数据库作为一个服务注册到监听程序。客户端和数据库服务器建立连接时,监听程序通过已注册的服务名监听客户端的连接。启动数据库时,默认有两条信息注册到监听器中,分别为数据库服务器对应的实例和服务。通过对外提供的服务名,客户端不需要知道数据库名和实例名,只需知道该数据库对应的服务名就可以实现对数据库的连接,该服务名可能与实例名相同,也可以不相同。
二 动态注册
动态注册是在实例启动的时候,或使用命令Alter system register,或每隔一分钟,PMON进程会向监听进行动态注册,PMON进程根据init.ora中的instance_name,service_names两个参数将实例和服务动态注册到listener中。
查看service_names和instance_name。
SQL> show parameter instance_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string orcl
SQL> show parameter service_names
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
service_names string orcl
SQL>
如果init.ora中的instance_name,service_names两个参数没有指定值,那么系统将取默认值,注册到监听器中的实例名将取init.ora文件中的db_name值,服务名将取init.ora文件中的db_name+db_domain值。可以在service_names参数中指定多个服务值,值之间用逗号隔开,这对于共享服务器配置很有用。
演示一:
采用动态注册时,不需要有listener.ora。
(1)首先将listener.ora文件重命名。
[oracle@strong admin]$ mv listener.ora listener.ora.bak
[oracle@strong admin]$ ls
listener.ora.bak samples shrept.lst tnsnames.ora
(2)启动监听器,并查看其状态。
[oracle@strong ~]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 06-AUG-2017 01:59:29
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Starting /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Log messages written to /u01/app/oracle/diag/tnslsnr/strong/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=strong.oracle.com)(PORT=1521)))
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 06-AUG-2017 01:59:30
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Log File /u01/app/oracle/diag/tnslsnr/strong/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=strong.oracle.com)(PORT=1521)))
The listener supports no services
The command completed successfully
[oracle@strong ~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 06-AUG-2017 02:00:23
Copyright (c) 1991, 2013, 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.4.0 - Production
Start Date 06-AUG-2017 01:59:30
Uptime 0 days 0 hr. 0 min. 53 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Log File /u01/app/oracle/diag/tnslsnr/strong/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=strong.oracle.com)(PORT=1521)))
Services Summary...
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...
The command completed successfully
[oracle@strong ~]$
说明:Instance状态为READY,说明采用动态注册监听器。
演示二:
如果有listener.ora文件,那么该文件中不能有关于该数据库的静态配置信息,否则将被当做静态注册,如下:
(1)恢复listener.ora文件。
[oracle@strong admin]$ mv listener.ora.bak listener.ora
(2)启动监听器并查看其状态。
[oracle@strong ~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 06-AUG-2017 02:07:15
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=strong.oracle.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 06-AUG-2017 02:06:28
Uptime 0 days 0 hr. 0 min. 47 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/strong/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=strong.oracle.com)(PORT=1521)))
Services Summary...
Service "orcl" has 2 instance(s).
Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...
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...
The command completed successfully
说明:Instance状态为UNKNOWN,说明采用静态注册监听器。
动态注册默认只注册到默认的监听器上(名称为LISTENER,端口为1521,协议为TCP),因为PMON只会动态注册端口为1521的监听,否则不能动态注册,如果使用非默认端口注册,则需要配置local_listener参数。
演示三:
更改监听器端口为1522,则显示为只有静态注册,如下:
[oracle@strong ~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 06-AUG-2017 03:13:36
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=strong.oracle.com)(PORT=1522)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 06-AUG-2017 03:07:19
Uptime 0 days 0 hr. 6 min. 16 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/strong/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=strong.oracle.com)(PORT=1522)))
Services Summary...
Service "orcl" has 1 instance(s).
Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
说明:Instance状态为UNKNOWN,说明采用静态注册监听器。
演示四:
端口为非默认端口时,注册动态监听需要配置local_listener参数。
SQL> alter system set LOCAL_LISTENER='(ADDRESS = (PROTOCOL = TCP)(HOST = strong.oracle.com)(PORT = 1522))';
System altered.
SQL> alter system register;
System altered.
SQL> show parameter local_listener
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
local_listener string (ADDRESS = (PROTOCOL = TCP)(HO
ST = strong.oracle.com)(PORT =
1522))
三 静态注册
静态注册是实例启动时读取linstener.ora文件的配置,将实例和服务注册到监听程序,默认读取数据库服务器对应的实例名和服务名。
静态注册时,listener.ora中的GLOBAL_DBNAME向外提供服务名,SID_NAME提供注册的实例名。静态注册的linstener.ora文件内容如下:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = strong.oracle.com)(PORT = 1521))
)
ADR_BASE_LISTENER = /u01/app/oracle
根据上述内容,GLOBAL_DBNAME对应的时Oracle对外的服务名,即初始化参数里的service_names,SID_NAME对应的是初始化参数里的instance_name。
综上静态注册和动态注册,Instance状态为UNKNOWN时表明此服务是静态注册,此时监听器用来表明它不知道关于该实例的任何消息,只有当客户发出连接请求时,它才检查该实例是否存在。
动态注册的Instance状态为READY,关闭数据库时,动态注册的数据库都会动态从监听器中注销,而与之相关的信息姜葱状态列表消失。
注:整理自网络和自己试验验证