在有Oracle Listener的动态注册之前,采用的是静态注册,所谓静态注册是指Oracle实例在启动时,读取listener.ora里的配置,然后注册到Listener,它主要有两个缺点:
1. Listener不知道Oracle实例的实时状态
2. listener.ora里的配置比较麻烦,常需要手动修改。
所谓动态注册是指Oracle实例启动后,会通过pmon进程实时的把实例状态和参数(instance_name,service_name)同步给Listener,其中参数instance_name如果为空,则默认为SID,参数service_name如果为空,则默认为db_name.db_domain
有了动态注册之后,我们甚至不需要listener.ora,这时命令lsnrctl start将启动默认的Listener(TCP协议、1521端口,Service和Instance分别来自参数service_name和instance_name。
这里有一个小tip:pmon并不是真正实时同步Oracle实例至Listener,而是隔几十秒,但你可以通过alter system register命令手动同步。
下面我们来看一个例子:
在这个例子里,我们没有配置listener.ora,listener.ora启动后的状态如下:
STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production Start Date 18-SEP-2013 16:58:01 Uptime 0 days 0 hr. 12 min. 54 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Log File /opt/oracle/diag/tnslsnr/data/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.15)(PORT=1521))) Services Summary... Service "orcl.localdomain" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... The command completed successfully
instance_name和service_names参数配置如下:
SQL>show parameter instance_name NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ instance_name string orcl SQL>show parameter service_name NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ service_names string orcl.localdomain
从上面我们可以发现:Listener里的Service、Instance是和instance_name、service_names参数相对应。这里需要特别说明的是:参数service_names可以指定多个service_name,它们之间用逗号隔开。
在客户端tnsnames.ora里我们可以设置SID等于instance_name:
test = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.15)(PORT = 1521)) ) (CONNECT_DATA = (sid = orcl) ) )
也可以设置service_name等于services_names中的任意一个:
test = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.15)(PORT = 1521)) ) (CONNECT_DATA = (service_name = orcl.localdomain) ) )
上面那个动态注册的例子非常简单,是因为我们使用的是默认的监听器(这也是一般推荐的做法),但如果想要使用非默认的监听器该怎么办?(比如端口不是1521的)
此时, local_listener就派上用场了,它需要配合listener.ora和tnsnames.ora使用(注意:这里的tnsnames.ora是在服务器端,而不是在客户端)。
首先,我们需要把非默认的监听器添加到listener.ora:
LISTENER_2 = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = data)(PORT = 1522)) ) )
然后,我们在服务器端的tnsnames.ora里添加:
tnsnames.ora:
LISTENER_2 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.15)(PORT = 1522)) )
接着,我们以sys用户设置local_listener:
SQL>alter system set local_listener=listener_2; System altered. SQL>alter system register; System altered.
执行如下命令启动listener_2并查看其状态:
$ lsnrctl start listener_2 $ lsnrctl status listener_2 STATUS of the LISTENER ------------------------ Alias listener_2 Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production Start Date 18-SEP-2013 17:36:43 Uptime 0 days 0 hr. 2 min. 53 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /opt/oracle/product/11.2.0/network/admin/listener.ora Listener Log File /opt/oracle/diag/tnslsnr/data/listener_2/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=data)(PORT=1522))) Services Summary... Service "orcl.localdomain" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... The command completed successfully