【Oracle】service_name和service_names的关系

简介:
从oracle9i开始,后台进程PMON自动在监听器中注册初始化参数SERVICE_NAMES中定义的服务名,SERVICE_NAMES默认为db_name.db_dimain。客户端tns配置中SERVICE_NAME的名称必须是SERVICE_NAMES或其中的一个NAME。
1 listenter.ora文件中的 GLOBAL_DBNAME和tnsnames.ora文件SERVICE_NAME的配置一样时,GLOBAL_DBNAME = A, 连接串中的SERVICE_NAME = A,此时就可以连接上数据库。根据tnsnames.ora文件中的连接串中的SERVICE_NAME = A 找到监听器listener.ora中的SID_DESC = (GLOBAL_DBNAME= A),之后在找到SID_NAME, 来连接到数据库实例。
2 listenter.ora文件中的 GLOBAL_DBNAME和tnsnames.ora文件的 SERVICE_NAME的配置一样时 ,tnsnames.ora中的SERVICE_NAME要与参数文件中的SERVICE_NAMES 参数一样或者是其中之一,否则无法通过tns连接到数据库。
tnsnames.ora文件中的service_name 和 listener.oar文件中的GLOBAL_DBNAME一样的情况:
查看tnsnames.ora文件的配置信息:
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>cat tnsnames.ora 
# tnsnames.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
YANGDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb)
    )
  )
YANGDB1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb1)
    )
  )

YANGDB2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb2)
    )
  )
YANGDB3 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb3)
    )
  )
RAC =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.250.7.200)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = rac)
    )
  )
 监听文件中的配置信息:
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>cat listener.ora 
# listener.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
      (SID_LIST =
         (SID_DESC =
           (SID_NAME = PLSExtProc)
             (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
           (PROGRAM = extproc)
         )
        (SID_DESC =
          (GLOBAL_DBNAME = yangdb)
           (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
          (SID_NAME = yangdb)
         )
     ) 
yangdb =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    )
  )
ADR_BASE_LISTENER = /opt/oracle
参数service_names是静态的,必须重启机器才能够生效。
oracle@rac3:/home/oracle>sqlplus "/as sysdba"
SYS@yangdb-rac3> alter system set service_names='yangdb,yangdb1,yangdb2,yangdb3' scope=both;
System altered.
SYS@yangdb-rac3> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@yangdb-rac3> startup 
ORACLE instance started.
Database mounted.
Database opened.
SYS@yangdb-rac3> show parameter service_names
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3
进行连接测试:
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@yangdb1
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:12:13 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
YANG@yangdb-rac3> show parameter service_ 

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3
YANG@yangdb-rac3> exit
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@yangdb2
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:57:26 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.

YANG@yangdb-rac3> show parameter service_

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3
YANG@yangdb-rac3> exit

oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@yangdb3
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:57:38 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:
YANG@yangdb-rac3> show parameter service_

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3
YANG@yangdb-rac3> exit
查看监听服务状态,可以知道共有6个监听服务名
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>lsnrctl service
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 20-OCT-2011 11:59:11
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0
         LOCAL SERVER
Service "yangdb" has 2 instance(s).
  Instance "yangdb", status UNKNOWN, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0
         LOCAL SERVER
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb1" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb2" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb3" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdbXDB" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "D000" established:0 refused:0 current:0 max:1022 state:ready
         DISPATCHER
         (ADDRESS=(PROTOCOL=tcp)(HOST=rac3)(PORT=45357))
The command completed successfully
tnsnames.ora文件中的service_name 和 参数文件中的service_name不一样的情况:
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>vi tnsnames.ora 
# tnsnames.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
lily =   ----连接标识,可以为任意值
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = lily)
    )
  )
YANGDB1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb1)
    )
  )
YANGDB2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb2)
    )
  )
YANGDB3 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb3)
    )
  )
"tnsnames.ora" 60L, 1198C 已写入                            
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>vi listener.ora 
# listener.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
      (SID_LIST =
         (SID_DESC =
           (SID_NAME = PLSExtProc)
             (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
           (PROGRAM = extproc)
         )
        (SID_DESC =
          (GLOBAL_DBNAME = lily)
           (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
          (SID_NAME = yangdb)
         )
     )
yangdb =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    )
  )
ADR_BASE_LISTENER = /opt/oracle
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>lsnrctl status
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 20-OCT-2011 10:58:30
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                27-SEP-2011 21:47:16
Uptime                    22 days 13 hr. 11 min. 14 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
Listener Log File         /opt/oracle/diag/tnslsnr/rac3/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rac3)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "yangdb" has 2 instance(s).
  Instance "yangdb", status UNKNOWN, has 1 handler(s) for this service...
  Instance "yangdb", status READY, has 1 handler(s) for this service...
Service "yangdb1" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
Service "yangdb2" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
Service "yangdb3" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
Service "yangdbXDB" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
The command completed successfully
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>lsnrctl service
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 20-OCT-2011 10:58:37

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

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0
         LOCAL SERVER
Service "yangdb" has 2 instance(s).
  Instance "yangdb", status UNKNOWN, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0
         LOCAL SERVER
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb1" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb2" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdb3" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:4 refused:0 state:ready
         LOCAL SERVER
Service "yangdbXDB" has 1 instance(s).
  Instance "yangdb", status READY, has 1 handler(s) for this service...
    Handler(s):
      "D000" established:0 refused:0 current:0 max:1022 state:ready
         DISPATCHER
         (ADDRESS=(PROTOCOL=tcp)(HOST=rac3)(PORT=45357))
The command completed successfully
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>lsnrctl reload
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 20-OCT-2011 10:58:45
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
The command completed successfully
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>
更改tnsnames.ora文件中的service_name的值 yangdblily,并相应的修改listener.ora文件中的global_dbname的值为lily ,也可以成功连接数据库。
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>vi tnsnames.ora 
# tnsnames.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
lily =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = lily)
    )
  )
YANGDB1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb1)
    )
  )
YANGDB2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb2)
    )
  )

YANGDB3 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangdb3)
    )
  )
"tnsnames.ora" 60L, 1196C 已写入
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@lily 
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:00:11 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
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
YANG@yangdb-rac3> show parameter service_        
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3  --动态监听里面没有lily 这样的服务
YANG@yangdb-rac3> exit
说明:如果 参数文件中的service_names没有lily这个值,但是listener.ora文件中有与之对应的GLOBAL_DBNAME,并且已经启动了静态监听服务,则sqlplus通过@lily也可以连接数据库。
如果没有启动静态监听,即没有lily 这样的service,则不可以连接数据库,(当然修改过之后没有执行reload 或没有重启监听,则还可以连接,因为之前存在,这样的例子不讨论)
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>vi listener.ora     
# listener.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
# Generated by Oracle configuration tools.
# SID_LIST_LISTENER =
#      (SID_LIST =
#         (SID_DESC =
#           (SID_NAME = PLSExtProc)
#             (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
#           (PROGRAM = extproc)
#         )
#        (SID_DESC =
#          (GLOBAL_DBNAME = lily)
#           (ORACLE_HOME = /opt/oracle/11.2.0/alifpre)
#          (SID_NAME = yangdb)
#         )
#     ) 
yangdb =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac3)(PORT = 1521))
    )
  )
ADR_BASE_LISTENER = /opt/oracle    
编辑过之后重新启动数据库(可选),重启监听,目的重新注册服务,去掉静态监听的服务。
  SYS@yangdb-rac3> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@yangdb-rac3>   startup 
Database mounted.
Database opened.  
SYS@yangdb-rac3> 
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>lsnrctl start
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 20-OCT-2011 11:11:24
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
Starting /opt/oracle/11.2.0/alifpre/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.1.0 - Production
System parameter file is /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
Log messages written to /opt/oracle/diag/tnslsnr/rac3/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rac3)(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                20-OCT-2011 11:11:24
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /opt/oracle/11.2.0/alifpre/network/admin/listener.ora
Listener Log File         /opt/oracle/diag/tnslsnr/rac3/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rac3)(PORT=1521)))
The listener supports no services  --没有静态监听服务,这时只能通过动态监听的服务来连接到数据库。
The command completed successfully
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@lily
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:11:35 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect 
descriptor --报错,监听没有lily这样的服务

Enter user-name: ^[[ 
--通过pmon动态监听的服务名来连接数据库。
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>sqlplus yang/yang@yangdb1
SQL*Plus: Release 11.2.0.1.0 Production on Thu Oct 20 11:12:13 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
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
YANG@yangdb-rac3> show parameter service_ 
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      yangdb,yangdb1,yangdb2,yangdb3
相关文章
|
9月前
|
Oracle 关系型数据库 Java
Oracle sid_name 和service_name的区别
Oracle sid_name 和service_name的区别
139 1
|
Oracle 关系型数据库 Java
ORACLE中SID和SERVICE_NAME的区别
       先来讲一个小故事,2015年6月份,有个客户迁移了数据库,由单实例数据库变成了RAC。
1149 0
|
14天前
|
SQL Oracle 关系型数据库
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
56 7
|
1月前
|
Oracle 关系型数据库 数据库
|
2月前
|
开发框架 Oracle 关系型数据库
ASP.NET实验室LIS系统源码 Oracle数据库
LIS是HIS的一个组成部分,通过与HIS的无缝连接可以共享HIS中的信息资源,使检验科能与门诊部、住院部、财务科和临床科室等全院各部门之间协同工作。 
37 4
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
1月前
|
Oracle 关系型数据库 数据库
Oracle数据库基本概念理解(3)
Oracle数据库基本概念理解(3)
18 2
|
1月前
|
Oracle 关系型数据库 数据库
如何利用 Docker 安装 Oracle 数据库
【2月更文挑战第14天】
94 0
|
14天前
|
SQL Oracle 关系型数据库
【Oracle】玩转Oracle数据库(七):RMAN恢复管理器
【Oracle】玩转Oracle数据库(七):RMAN恢复管理器
41 5
|
1月前
|
Oracle 关系型数据库 数据库
Oracle数据库基本概念理解(2)
Oracle数据库基本概念理解(2)
13 1

推荐镜像

更多