现象:
为数据库设置多个服务名(通过SCOPE=both设置,同时修改参数文件)
SQL> show parameter service_names; NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
service_names string vmdb SQL> alter system set service_names='vmdb,sn01,sn02' scope=both; System altered.
重启监听后,监听状态并未显示服务sn01,sn02
[oracle@CentOS ~]$ lsnrctl stop LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 26-MAR-2018 18:29:21 Copyright (c) 1991, 2009, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=CentOS)(PORT=1521)))
The command completed successfully
[oracle@CentOS ~]$ lsnrctl start LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 26-MAR-2018 18:29:24 Copyright (c) 1991, 2009, 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.1.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/CentOS/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=1521))) Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=CentOS)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 26-MAR-2018 18:29:24
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/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/CentOS/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=1521)))
Services Summary...
Service "vmdb" has 1 instance(s).
Instance "vmdb", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
通过另一台机器通过服务名sn01连接可以连接上:
[oracle@centos-sample ~]$ sqlplus test/test@192.168.8.141/sn01 SQL*Plus: Release 11.2.0.1.0 Production on Sun Mar 25 20:06:35 2018 Copyright (c) 1982, 2009, Oracle. All rights reserved. ERROR:
ORA-28002: the password will expire within 7 days Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL>
在数据库中可以查询v$session视图的service_name来判断会话是由通过哪个服务名连接创建的:
SQL> col paddr format a20
SQL> col Sid format 9999999
SQL> col username format a15
SQL> col service_name format a20
SQL> Select paddr,Sid,serial#,username,service_name From v$session Where username Is Not Null; PADDR SID SERIAL# USERNAME SERVICE_NAME
-------------------- -------- ---------- --------------- --------------------
000000008DC95250 17 19 SYS SYS$USERS
000000008DC98310 36 7 TEST vmdb
000000008DC96290 37 22 TEST sn01
000000008DC99350 38 11 TEST vmdb
重启数据库:
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started. Total System Global Area 776646656 bytes
Fixed Size 2217384 bytes
Variable Size 583010904 bytes
Database Buffers 188743680 bytes
Redo Buffers 2674688 bytes
Database mounted.
Database opened.
再次查看监听,sn01,sn02服务名显示出来了:
[oracle@CentOS ~]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 26-MAR-2018 18:30:29 Copyright (c) 1991, 2009, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=CentOS)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 26-MAR-2018 18:29:24
Uptime 0 days 0 hr. 1 min. 5 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/CentOS/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=1521)))
Services Summary...
Service "sn01" has 1 instance(s).
Instance "vmdb", status READY, has 1 handler(s) for this service...
Service "sn02" has 1 instance(s).
Instance "vmdb", status READY, has 1 handler(s) for this service...
Service "vmdb" has 2 instance(s).
Instance "vmdb", status UNKNOWN, has 1 handler(s) for this service...
Instance "vmdb", status READY, has 1 handler(s) for this service...
Service "vmdbXDB" has 1 instance(s).
Instance "vmdb", status READY, has 1 handler(s) for this service...
The command completed successfully
但是监听配置文件里不会自动增加服务名sn01,sn02:
[oracle@CentOS ~]$ cat /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools. SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = vmdb)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = vmdb)
)
) LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = CentOS)(PORT = 1521))
) ADR_BASE_LISTENER = /u01/app/oracle
listener.ora文件典型配置:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = vmdb)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = vmdb)
)
(SID_DESC =
(GLOBAL_DBNAME = sn01)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = vmdb)
)
(SID_DESC =
(GLOBAL_DBNAME = sn02)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = vmdb)
)
) LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = CentOS)(PORT = 1521))
)
以下解释来自老盖的《深入浅出ORACLE》:
监听器文件主要包含两个部分:
- 第一部分LISTENER信息,这部分包含了监听的协议、地址以及端口等信息。
- 第二部分SID_LIST_LISTENER信息,这部分信息用于提供对外的数据库服务列表。第一个SID_DESC部分(SID_NAME = PLSExtProc)是数据库缺省就包含的对外部存储过程提供的本地监听,此外三个SID_DESC部分是对数据库的三个SERVICE_NAMES所设置的监听服务,对于同一个SID对应的数据库,可以对外提供多个服务名供客户端访问。
设置服务名的参数为GLOBAL_DBNAME,当处理客户端连接请求时,监听器首先尝试将GLOBAL_DBNAME和客户端请求中的SERVICE_NAME相匹配;如果客户端连接请求的是SID信息,则Oracle不检查GLOBAL_DBNAME设置,而是对监听器中设置的SID_NAME进行匹配。
启动这个监听后,可以看到对于不同服务名Oracle所启动的监听信息。首先输出的信息显示了监听器文件地址以及监听日志文件位置(监听器日志在诊断数据库异常或攻击信息时非常有用)。
通过服务名,Oracle可以将客户端和服务器彻底隔离开来,对于客户端来说,它不用关心数据库的名字、实例名到底是什么,它只需要知道数据库对外提供的服务名就行了,这个名字可能和实例名相同,也可能不相同。
(注:通过查询会话视图v$session的service_names,可以区分哪些会话来自哪个service_name,所以可以通过提供不同的service_name给不同的下游系统,用来区分哪些会话由哪些系统创建)
从8i开始,oracle引入了动态服务注册(Dynamic Service Registration)的功能,所谓动态注册是指当实例启动之后,由后台进程PMON在监听器中注册数据库服务信息。在动态注册机制下,原来监听器中的SID_LIST部分将不再需要。动态注册的服务名,由于监听器确切地知道实例的状态,所以正常状态通常显示为READY,而对于静态注册的服务名,则状态显示为UNKNOW。
以下演示,删除监听配置文件listener.ora后,监听器依然可以知道动态注册的服务名:
[oracle@CentOS ~]$ cd /u01/app/oracle/product/11.2./dbhome_1/network/admin/
[oracle@CentOS admin]$ ll
总用量
-rw-r--r-- oracle oinstall 3月 : listener.ora
drwxr-xr-x. oracle oinstall 5月 samples
-rw-r--r--. oracle oinstall 5月 shrept.lst
-rw-r-----. oracle oinstall 5月 tnsnames.ora
[oracle@CentOS admin]$ lsnrctl stop LSNRCTL for Linux: Version 11.2.0.1. - Production on -MAR- :: Copyright (c) , , Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=CentOS)(PORT=)))
The command completed successfully [oracle@CentOS admin]$ ll
总用量
-rw-r--r-- oracle oinstall 3月 : listener.ora
drwxr-xr-x. oracle oinstall 5月 samples
-rw-r--r--. oracle oinstall 5月 shrept.lst
-rw-r-----. oracle oinstall 5月 tnsnames.ora
[oracle@CentOS admin]$ mv listener.ora listener.ora_bak
[oracle@CentOS admin]$ lsnrctl start LSNRCTL for Linux: Version 11.2.0.1. - Production on -MAR- :: Copyright (c) , , Oracle. All rights reserved. Starting /u01/app/oracle/product/11.2./dbhome_1/bin/tnslsnr: please wait... TNSLSNR for Linux: Version 11.2.0.1. - Production
Log messages written to /u01/app/oracle/diag/tnslsnr/CentOS/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=))) Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1. - Production
Start Date -MAR- ::
Uptime days hr. min. sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Log File /u01/app/oracle/diag/tnslsnr/CentOS/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=)))
The listener supports no services
The command completed successfully
[oracle@CentOS admin]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0.1. - Production on -MAR- :: Copyright (c) , , Oracle. All rights reserved. Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1. - Production
Start Date -MAR- ::
Uptime days hr. min. sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Log File /u01/app/oracle/diag/tnslsnr/CentOS/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=CentOS)(PORT=)))
Services Summary...
Service "sn01" has instance(s).
Instance "vmdb", status READY, has handler(s) for this service...
Service "sn02" has instance(s).
Instance "vmdb", status READY, has handler(s) for this service...
Service "vmdb" has instance(s).
Instance "vmdb", status READY, has handler(s) for this service...
Service "vmdbXDB" has instance(s).
Instance "vmdb", status READY, has handler(s) for this service...
The command completed successfully
可以看到只有动态注册的服务名,没有了UNKNOW。
启动监听时,虽然显示The listener supports no services,但是由于动态注册是PMON进程主动向监听器注册,所以监听状态依然能看到动态注册的服务处于READY状态,可以对外提供访问服务。