Oracle中Service Name、Database name、Instance Name的区别

转自:http://blog.csdn.net/kkdelta/article/details/4708592

1,数据库名是Oracle数据库的内部标识,一般在安装完后不应该改变,数据库的很多物理存储目录都用到了数据库名。

2,Instance_Name,ORACLE_SID,数据库实例名,是数据库和操作系统交互时用到的名称。

如果在一台机器上创建了多个数据库,通过Sqlplus想连接到其中的一个数据库,就需要指明ORACLE_SID:

set ORACLE_SID=SIDNAME

sqlplus / as sysdba(这种连法只能在本机用,会根据ORACLE_SID连接到对应的实例)

通常碰到的12560错误一般就是因为实例名被错误修改或者服务没有被启动。

Instance_Name则是数据库的一个参数.

3,Service Name:服务名,如果数据库有域名则等同于Global DB Name、没有的话则等同于数据库名,这是因为数据库启动后会自动把数据库名注册到监听成为服务名,同时数据库参数service_names也会注册为服务名,所以一个数据库可以有多个服务名.

当在Oracle8i,9i,10g的客户端连接Oracle8i,9i,10g服务器的时候,主机字符串应该使用服务名。

4,Net Service Name:网络服务名,也有人叫TNS别名、网络连接串(connect string),在tnsnames.ora中配置的名称,如下面例子的DBTNS.
sqlplus sys/sys@orcl as sysdba(这种连法会根据tnsnames.ora中配置的网络服务名连接到本机或者远程的oracle,走的是网络通信)

看看数据库参数设置:
SQL> show parameter instance_name
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------
instance_name                        string      orcl
SQL> show parameter service_names
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------
service_names                        string      orcla,orclb
SQL> show parameter db_name
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------
db_name                              string      orcl
service_names也可以通过alter system set service_names=orclA,orclB;修改为对应多个值。

listener启动时候根据listener.ora配置的信息静态注册可用的服务,同时数据库实例启动以后(PMON)会把

service_names,db_name的值动态的注册到Listener。

例如,如果service_names的值为orcla,orclb,db_name的值为orcl,在listener.ora里有如下配置:

    (SID_DESC =
      (GLOBAL_DBNAME = orclst) 
      (ORACLE_HOME = D:\oracle\product\10.2.0\db_1)
      (SID_NAME = orcl)
    )

则在Tnsname.ora中可以作为SERVICE_NAME的值为orcla,orclb,orcl,orclst

通过lsnrct status 可以看到这些服务的信息: status READY的为动态注册的(因为是在实例启动之后才注册到listener的,所以状态为ready),status UNKNOWN的为静态注册的(因为是listener启动的时候根据配置启动的,这是实例是否启动是不知道的,所以为unknown)。

通过连接后看v$session中的service_name也可以判断用的服务名是静态注册的还是动态注册的.SYS$USERS表示为静态的.

其中orclst为静态的注册,查看session可以看到如下

SQL> select service_name from v$session where sid=(select distinct(sid) from v$mystat);
SERVICE_NAME
----------------------------------------------------------------
SYS$USERS

其中通过orclsa为动态的注册,查看session可以看到如下

SQL> select service_name from v$session where sid=(select distinct(sid) from v$mystat);
SERVICE_NAME
----------------------------------------------------------------
orcla


Services Summary...
Service "ORCLA" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "ORCLB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
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...
Service "orclst" has 1 instance(s).
  Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

动态注册默认只能注册1521的listener,如果监听端口不是1521,需要:

1,在服务器端的Tnsname.ora中配置一个tns指明端口号,只有ADDRESS配置没有CONNECT_DATA配置项:

lclsn=
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = PC-6753184)(PORT = 1522))
  )

2,将数据库的参数 local_listener的值改为服务器端的Tnsname.ora配置的lclsn:

 alter system set local_listener=lclsn;


Tnsname.ora中配置: 通过lsnrct status得到可以用的SERVICE_NAME ,如orcla,orclb,orcl...

DBTNS =
(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.19.11.28)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = teldb)
    )
)

上面例子中的SERVICE_NAME = teldb也可以换成SID=orcl.

一个数据库集群的例子:

DBCLUSTER=
  (DESCRIPTION_LIST =

    (LOAD_BALANCE = OFF)

    (FAILOVER = ON)

    (DESCRIPTION =

      (ADDRESS_LIST =

        (LOAD_BALANCE = ON)

        (FAILOVER = ON)

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d001-oravip.test.com)(PORT = 6191))

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d002-oravip.test.com)(PORT = 6191))

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d003-oravip.test.com)(PORT = 6191))

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d004-oravip.test.com)(PORT = 6191))

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d005-oravip.test.com)(PORT = 6191))

        (ADDRESS = (PROTOCOL = TCP)(HOST = rdl701d006-oravip.test.com)(PORT = 6191))

      )

      (CONNECT_DATA =

        (SERVICE_NAME = opofs_rd)

        (FAILOVER_MODE =

          (TYPE = SESSION)

          (METHOD = BASIC)

          (RETRIES = 120)

          (DELAY = 5)

        )

      )

    )

  )

你可能感兴趣的:(Oracle中Service Name、Database name、Instance Name的区别)