db_name,instance_name,service_names,db_domain,dbid,oracle_sid等区别与联系

最近整理了一篇文章:oracle listener 有网友对数据库是否显式设置了instance_name和service_names提出疑问。

由此引发出db_name,instance_name,oracle_sid等等这些常见的参数都代表什么意思,怎么取值的,有什么区别?

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE    11.2.0.3.0      Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

SQL> show parameter name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert             string
db_name                               string       ORCL
db_unique_name                    string       ORCL
global_names                         boolean    FALSE
instance_name                       string       ORCL
lock_name_space                   string
log_file_name_convert              string
processor_group_name            string
service_names                        string       ORCL

看到这么多参数,但是服务器参数(spfile)中仅仅设置了db_name那么其它的name比如db_unique_name,instance_name,service_names的值是怎么出来的?

官方是这样说的:When an Oracle instance starts, it reads initialization parameters from an initialization parameter file. This file must at a minimum specify the DB_NAME parameter. All otherparameters have default values.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

SQL> create pfile='/u01/pfile.ora' from spfile;

File created.

[oracle@resoft u01]$ vi pfile.ora

ORCL.__db_cache_size=243269632
ORCL.__java_pool_size=4194304
ORCL.__large_pool_size=4194304
ORCL.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
ORCL.__pga_aggregate_target=293601280
ORCL.__sga_target=553648128
ORCL.__shared_io_pool_size=0
ORCL.__shared_pool_size=289406976
ORCL.__streams_pool_size=4194304
*.audit_file_dest='/u01/app/oracle/admin/ORCL/adump'
*.audit_trail='DB','EXTENDED'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/ORCL/control01.ctl','/u01/app/oracle/fast_recovery_area/ORCL/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='ORCL'
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4322230272
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=ORCLXDB)'
*.memory_target=847249408
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'

各种name或者id的解释:--括弧内中文名称是我们一致认为比较合理的翻译,但是如果想准确的表达,请直接说英文名称,不要去翻译,以免造成误解。

db_name(数据库名)

Property                                                Description
Parameter type                                      String
Syntax                                                  DB_NAME = database_name
Default value                                          There is no default value.
Modifiable                                              No
Basic                                                    Yes
Oracle RAC                                           You must set this parameter for every instance. Multiple instances must have the same value, or the same value must be

                                                             specified in the STARTUP OPEN SQL*Plus command or the ALTER DATABASE MOUNT SQL statement.

DB_NAME must be set to a text string of no more than eight characters. During database creation, the name provided for DB_NAME is recorded in the datafiles, redo log files, and control file of the database. If during database instance startup the value of the DB_NAME parameter (in the parameter file) and the database name in the control file are different, the database does not start.

db_name必须是一个不超过8个字符的文本串。在数据库创建过程中,db_name被记录在数据文件,日志文件和控制文件中。如果数据库实例启动过程中参数文件中的db_name和控制文件中的db_name名称不一样,则数据库不能启动。db_name是最具有稳定意义的参数,官网说不能修改,修改后会导致数据库不能启动。

DB_NAME 也就是数据库的名字标示。这里,数据库里可能有多个实例,比如RAC里的多节点,这多个节点是不同的实例,但是却有相同的名字,他们的 DB_NAME是相同的但是Instance_name是不同的。DB_NAME会保持在数据文件头里,所以更改DB_NAME不能仅仅修改parameter,还需要用nid 来进行更改,并且更改后还需要手工做些工作,是其生效。

db_unique_name(数据库唯一名)

Property                                                Description
Parameter type                                      String
Syntax                                                  DB_UNIQUE_NAME = database_unique_name
Default value                                          Database instances: the value of DB_NAME
                                                            Automatic Storage Management instances: +ASM
Modifiable                                              No
Basic                                                    Yes
Oracle RAC                                           Multiple instances must have the same value.

DB_UNIQUE_NAME specifies a globally unique name for the database. Databases with the same DB_NAME within the same DB_DOMAIN (for example, copies of a database created for reporting or a physical standby) must have a unique DB_UNIQUE_NAME. Every database's DB_UNIQUE_NAME must be unique within the enterprise. The value of DB_UNIQUE_NAME can be up to 30 characters and is case insensitive. The following characters are valid in a database name: alphanumeric characters, underscore (_), number sign (#), and dollar sign ($)   --E文不好,翻译还不如看原版。

DB_UNIQUE_NAME这在另一个HA的应用,Dataguard会经常提及的,和DB_NAME不一样的作用,在DG里,要求物理DG,主从库都有一样的DB_NAME,虽然他们和RAC不一样,并不是同一个库。这里是数据库的唯一名字。但是他们的DB_UNIQUE_NAME是不一样的,用以进行不同的标示。DB_UNQUIE_NAME的会影响到Service_names,也会影响到动态监听的时候的service_name

比如如下片段
Service "zxdbdg1" has 1 instance(s).
Instance "zxdb", status BLOCKED, has 1 handler(s) for this service...
Service "zxdbdg1_XPT" has 1 instance(s).
Instance "zxdb", status BLOCKED, has 1 handler(s) for this service...
The command completed successfully
这里的zxdbdg1就是dg中的从库。从库的db_name和主库保持一样为zxdb,DB_UNIQUE_NAME不同。在动态监听后,注册为
zxdbdg1的service,启动的instance_name还是zxdb
Instance_name简单讲就是ORACLE_SID,oracle里通过ORSCLE_SID来管理不同的数据库实例。
另,
上面的动态监听信息里出现了Instance "zxdb", status BLOCKED
这里是因为我的从库数据库不是open状态。

instance_name (数据库实例名)

Property                                                                  Description
Parameter type                                                        String
Syntax                                                                    INSTANCE_NAME = instance_id
Default value                                                            The instance's SID
                                                                              Note: The SID identifies the instance's shared memory on a host, but
                                                                              may not uniquely distinguish this instance from other instances.
Modifiable                                                                No
Range of values                                                       Any alphanumeric characters and the underscore (_) character
Basic                                                                      No

In a Real Application Clusters environment, multiple instances can be associated with a single database service. Clients can override Oracle's connection load balancing by

In a single-instance database system, the instance name is usually the same as the database namespecifying a particular instance by which to connect to the database. INSTANCE_NAME specifies the unique name of this instance.

用于和操作系统之间的联系,用于对外部连接时使用。在操作系统中要取得与数据库之间的交互,必须使用数据库实例名。该值允许修改,但一般设置与数据库名相同即可

INSTANCE_NAME 的默认值就是oracle SID。 一般跟数据库库名称相同,也可以不相同。

服务器参数spfile中没有设置instance_name,所以取默认值oracle_sid,我这里sid 为ORCL 所以instance_name=ORCL

oracle_sid(数据库实例名)

SID的全称为site identifier,Oracle_SID则为Oracle site identifier.

Unix/Linux查看oralce_sid: export $oracle_sid  或者直接查看环境变量文件 more .bash_profile

下面引用Tom(Thomas Kyte)的一段话来解释Oracle_SID

If you’re unfamiliar with the term SID or ORACLE_SID, a full definition is called for. The SID is a site identifier. It and ORACLE_HOME (where the Oracle software is installed) are hashed together in UNIX to create a unique key name for attaching an SGA. If your ORACLE_SID or ORACLE_HOME is not set correctly, you’ll get the ORACLE NOT AVAILABLE error, since you can’t attach to a shared memory segment that is identified by this unique key. On Windows, shared memory isn’t used in the same fashion as UNIX, but the SID is still important. You can have more than one database on the same ORACLE_HOME, so you need a way to uniquely identify each one, along with their configuration files.

即:在Unix/Linux系统中,SID和ORACLE_HOME在一起哈希后得到一个唯一的值作为SGA的key。

当oracle实例启动时,在操作系统上的fork进程则根据Oracle_SID来创建相关后台进程。

Oracle 11g 支持Oracle_SID的长度为12位,db_name的长度为8位

作用:

用于区别同一台主机上不同的Oracle实例

决定实例所启动后台进程的名称。(实例由SGA和后台进程组成)

决定了参数文件的名称。如spfile<ORACLE_SID>.ora,init<ORACLE_SID>.ora

决定后台进程产生的相关跟踪文件、日志文件等。

如alert_<ORACLE_SID>.log,<ORACLE_SID>_arc1_<spid>.trc,<SID>_ora_<SPID>.trc

同一主机上不同的$ORACLE_HOME,可以创建相同的ORACLE_SID

由此可以得出

ORACLE_HOME相同时,可以使用不同的ORACLE_SID

ORACLE_HOME不同时,可以使用相同的ORACLE_SID

[oracle@RESOFT~]$ export ORACLE_SID=orcl --设定ORACLE_SID为orcl

SQL> ho ps -ef | grep oracle         --ORACLE_SID参与了后台进程命名

oracle 3272 1 0 09:46 ? 00:00:00 ora_pmon_orcl

oracle 3274 1 0 09:46 ? 00:00:00 ora_psp0_orcl

oracle 3276 1 1 09:46 ? 00:00:00 ora_mman_orcl

oracle 3278 1 0 09:46 ? 00:00:00 ora_dbw0_orcl

oracle 3280 1 0 09:46 ? 00:00:00 ora_lgwr_orcl

oracle 3282 1 0 09:46 ? 00:00:00 ora_ckpt_orcl

oracle 3284 1 0 09:46 ? 00:00:00 ora_smon_orcl

---------............部分结果省略.............. --------------

dbid(数据库id)

An internal, uniquely generated number that differentiates databases. Oracle creates this number automatically when you create the database.

DBID 可以看成是db_name在数据库内部的表示。 DBID是在创建数据库时,用db_name 结合一种算法来创建的。 具体用什么算法,不太清楚。它存在与数据文件和控制文件,用于表示数据文件的归属。 所以这个DBID 是唯一的。 对于不同的数据库,DBID 是不同的,但是db_name 有可能相同。

用身份证打个比方: 可以有同名的人,但是它的省份证号码肯定是不同的。

查看DBID:

SQL> select dbid from v$database;

DBID

----------

1318255748

global_names (全局数据库名)

Property                                                                   Description
Parameter type                                                         Boolean
Default value                                                             false
Modifiable                                                                 ALTER SESSION, ALTER SYSTEM
Range of values                                                         true | false
Basic                                                                        No

GLOBAL_NAMES specifies whether a database link is required to have the same name as the database to which it connect.

If the value of GLOBAL_NAMES is false, then no check is performed. If you use or plan to use distributed processing, then Oracle recommends that you set this parameter to true to ensure the use of consistent naming conventions for databases and links in a networked environment.

Global_names 是一个布尔值,global_names的作用是创建db link时是否强制使用远程数据库的global_name,如果global_names=true,则db link name必须要求是remote database的global_name,否则创建之后db link 不能连同,缺省值是false。多用于分布式系统。

global_name命名规则= db_name+[db_domain]

也就是由db_name.db_domain构成。

查看Global_name:

SQL> SELECT * FROM GLOBAL_NAME;

GLOBAL_NAME

--------------------------------------------------------------------------------

RACDB

我们可以修改GLOBAL_NAME. 如:

ALTER DATABASE RENAME GLOBAL_NAME TO sales.us.example.com;

db_domain(数据库域名)

Property                                                                            Description
Parameter type                                                                  String
Syntax                                                                              DB_DOMAIN = domain_name
Default value                                                                      There is no default value.
Modifiable                                                                          No
Range of values                                                                 Any legal string of name components, separated by periods and up to

                                                                                        128 characters long (including the periods). This value cannot be NULL.
Basic                                                                                Yes
Oracle RAC                                                                       You must set this parameter for every instance, and multiple instances must have the same value.

In a distributed database system, DB_DOMAIN specifies the logical location of the database within the network structure. You should set this parameter if this database is or ever will be part of a distributed system. The value consists of the extension components of a global database name, consisting of valid identifiers (any alphanumeric ASCII characters), separated by periods. Oracle recommends that you specify DB_DOMAIN as a unique string for all databases in a domain.

This parameter allows one department to create a database without worrying that it might have the same name as a database created by another department. If one sales department's DB_DOMAIN is JAPAN.ACME.COM, then their SALES database (SALES.JAPAN.ACME.COM) is uniquely distinguished from another database with DB_NAME = SALES but with DB_DOMAIN = US.ACME.COM

If you omit the domains from the name of a database link, Oracle expands the name by qualifying the database with the domain of your local database as it currently exists in the data dictionary, and then stores the link name in the data dictionary. The characters valid in a database domain name are: alphanumeric characters, underscore (_), and number sign (#)

通常用于分布式数据库系统中标识一个数据库的逻辑位置。为数据定义一个域,该参数作为Global_names的一部分,即在不同的域中可以使用相同的数据库名称,该参数缺省情况下位空,在RAC环境中需要为每一个实例指定该值,且多实例具有相同的值

SQL> show parameter db_domain

NAME TYPE VALUE

------------------------------------ ----------- ------------------------------

db_domain string

service_names(数据库服务名)

Property                                                                        Description
Parameter type                                                              String
Syntax                                                                          SERVICE_NAMES =db_service_name [, db_service_name [ ... ] ]
Default value                                                                  DB_UNIQUE_NAME.DB_DOMAIN if defined
Modifiable                                                                      ALTER SYSTEM
Range of values                                                              Any ASCII string or comma-separated list of string names
Basic                                                                             No
Oracle RAC                                                                    Do not set the SERVICE_NAMES parameter for Oracle RAC
                                                                                     environments. Instead, define services using Oracle Enterprise 
                                                                                     Manager and manage those services using Server Control (SRVCTL) utility

SERVICE_NAMES specifies one or more names by which clients can connect to the instance. The instance registers its service names with the listener. When a client requests a service, the listener determines which instances offer the requested service and routes the client to the appropriate instance.

You can specify multiple service names in order to distinguish among different uses of the same database. For example:
SERVICE_NAMES = sales.acme.com, widgetsales.acme.com

You can also use service names to identify a single service that is available from two different databases through the use of replication.

If you do not qualify the names in this parameter with a domain, Oracle qualifies them with the value of the DB_DOMAIN parameter. If DB_DOMAIN is not specified, then no domain will be applied to the non-qualified SERVICE_NAMES values.

service_names 在tnsnames.ora中用的最多。

service_names=DB_UNIQUE_NAME.DB_DOMAIN

我的环境中db_unique_name,db_domanin都没有设置,而db_unique_name在单实例下默认值为:db_name = ORCL

所以service_names=ORCL

这也就解释了刚开始提出的问题:没有在spfile中设置instance_name,service_names 但是动态注册时怎么发生的?

总结:

DB 相关的: DBID, ORCLE_SID

PFILE中的参数:DB_NAME,DB_DOMAIN, INSTANCE_NAME,

DB_UNIQUE_NAME,SERVICE_NAMES,

GLOBAL_NAME,GLOBAL_NAMES

Listener.ora中参数:

SID_NAME,GLOBAL_DBNAME

Tnsnames.ora中参数:

SERVICE_NAME,SID

posted @ 2013-04-05 16:56  AlbertCQY  阅读(869)  评论(1编辑  收藏  举报