2011年5月22日 星期日

(轉)oracle中sid及有關name的解釋

本文轉載自

這篇文章中要講的幾個參數:
      DB相關的:DBID, SID
      PFILE中的參數:DB_NAMEDB_DOMAIN, INSTANCE_NAME
                           DB_UNIQUE_NAME,SERVICE_NAMES,
                           GLOBAL_NAMEGLOBAL_NAMES
      Listener.ora中參數:
                         SID_NAMEGLOBAL_DBNAME
      Tnsnames.ora中參數:
                        SERVICE_NAMESID          

.DB相關的2個參數
1.1 DBIDDataBase IDentifier
Oracle官網對DBID的解釋如下:
      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
----------
1262006473


我們可以用命令來修改DBID,這個官網有說明:
      Changing the DBID and DBNAME of a Database

修改的步驟如下:
1)將數據庫啟動到mount狀態
2)用nid命令修改:nid TARGET=/ DBNAME=tianlesoftware
3)修改之後,在啟動到mountstartup mount
4resetlogs打開數據庫:ALTER DATABASE OPEN RESETLOGS;

要注意一點:
      修改DBID之後,之前的備份和歸檔都將無效。具體參考官網內容:

Ramifications of Changing the DBID and DBNAME
      Changing the DBID of a database is a serious procedure.When the DBID of a database is changed, all previous backups and archived logs of the database become unusable.This is similar to creating a database except that the data is already in the datafiles. After you change the DBID, backups and archive logs that were created prior to the change can no longer be used because they still have the original DBID, which does not match the current DBID.You must open the database with the RESETLOGS option, which re-creates the online redo logs and resets their sequence to 1(see theOracle Database Administrator's Guide). Consequently,you should make a backup of the whole database immediately after changing the DBID.
      Changing the DBNAME without changing the DBID does not require you to open with the RESETLOGS option, so database backups and archived logs are not invalidated. However,changing the DBNAME does have consequences. You must change the DB_NAME initialization parameter after a database name change to reflect the new name. Also,you may have to re-create the Oracle password file.If you restore an old backup of the control file (before the name change), then you should use the initialization parameter file and password file from before the database name change.

From
Ramifications of Changing the DBID and DBNAME

1.2. SIDsystem identifier
官網的說明如下:
      The system identifier (SID) is a unique name for an Oracle database instance on a specific host.On UNIX and Linux, Oracle Database uses the SID andOracle homevalues to create a key to shared memory.Also, the SID is used by default to locate the parameter file, which is used to locate relevant files such as the database control files.
On most platforms,the ORACLE_SID environment variable sets the SID,whereas the ORACLE_HOME variable sets the Oracle home. When connecting to an instance, clients can specify the SID in an Oracle Net connection or use a net service name. Oracle Database converts a service name into an ORACLE_HOME and ORACLE_SID.

查看SID:
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
orcl

      儘管v$instance中字段instance_name看起來是實例名,但是實際上存儲的是sid  win,不管oracle_home是否相同,sid不能重複。
      unix/linux下只要不同版本的oracle安裝在不同的oracle_home下就可以創建相同sid的實例,但是win下不可以,主要是受到windows服務的限制,在服務中不能存在服務名相同的oracle服務,服務名是由如下格式組成的:OracleServiceSID,因為服務名中包括了sid,所以sid如果相同了,服務名就相同了,這是windows所不允許的。因此在win下無法創建相同sid的不同實例。


. Pfile中的參數
 2.1. DB_NAME
      官網的說明如下:
      DB_NAME specifies a database identifier of up to 8 characters.This parameter must be specified and must correspond to the name specified in the CREATE DATABASE statement.
      If you have multiple databases, the value of this parameter should match the Oracle instance identifier of each one to avoid confusion with other databases running on the system.The value of DB_NAME should be the same in both the standby and production initialization parameter files.
      The database name specified in either the STARTUP command or the ALTER DATABASE ... MOUNT statement for each instance of the cluster database must correspond to the DB_NAME initialization parameter setting.
      The following characters are valid in a database name: alphanumeric characters, underscore (_), number sign (#), and dollar sign ($).No other characters are valid. Oracle removes double quotation marks before processing the database name. Therefore you cannot use double quotation marks to embed other characters in the name. The database name is case insensitive.
      
      DB_NAME must be set to a text string ofno 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 not the same, the database does not start.

From:


      DB_NAME是數據庫名,它的長度不能超過8個字節,超過8個會被截斷。對於RAC環境,各個節點之間的DB_NAME都是相同的,INSTANCE_NAME不同。對於Data Guard環境,DB_NAME相同,DB_UNIQUE_NAME不同。
      DB_NAME記錄在datafileredo logcontrol file中。這個記錄方式就是通過DBID因為DBID就是用DB_NAME加算法來生成的。
      還是借用身份證的比喻:DB_NAME就是姓名,DBID就是身份證。
      
      所以如果要修改DB_NAME,只能用nid命令來修改DBID,然後修改相關pfile參數。

查看DB_NAME
SQL>show parameter db_name
NAME                  TYPE       VALUE
------------------------------------ ----------- ------------------------------
db_name                string     orcl
SQL> select dbid,name from v$database;
DBID NAME
---------- ---------
1262006473 ORCL

      db_name還有一個非常重要的作用就是動態註冊監聽. 關於動態註冊和靜態註冊,參考Blog
      Oracle Listener動態註冊靜態註冊

      不管是否指定了service_name,或者說service_name的值是什麼,pmon都會使用db_name動態註冊監聽的。

      註冊到監聽器中的服務值從init.ora文件中的參數service_names取得。如果該參數沒有設定值,數據庫將拼接init.ora文件中的db_namedb_domain的值來註冊自己。如果選擇提供service_names值,您可以使用完全限定的名稱(比如orcl.oracle.com)或縮寫的名稱(比如orcl)。如果選擇縮寫的名稱並設置了db_domain參數,註冊到監聽器中的服務將是service_name值和db_domain值的拼接。

2.2. 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 andup 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 (#).


2.3. 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.
--對於DB_NAME系統的的數據庫必須要有不同的DB_UNIQUE_NAMEData Guard就是這麼回事。
      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 ($).

      DB_UNQUIE_NAME的會影響到Service_names,也會影響到動態監聽的時候的service_name如在Data Guard環境下,如果採用動態註冊,那麼註冊的Service就是DB_UNIQUE_NAMEinstance還是instance_name,SID.

如:
Service "orcl_st" has 1 instance(s).
!q o/v7@3} f @ c0Instance "orcl", status BLOCKED, has 1 handler(s) for this service...
'l d'H7H'p'[ a7O0Service "orcl_st_XPT" has 1 instance(s).
$s t Q X w5x0k0Instance "orcl", status BLOCKED, has 1 handler(s) for this service...ITPUB個人空間 u%e*]1H D |*J#a I
The command completed successfully


2.4. 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 specifying a particular instance by which to connect to the database.INSTANCE_NAME specifies the unique name of this instance.
      In a single-instance database system, the instance name is usually the same as the database name.

INSTANCE_NAME  的默認值就是SID一般跟數據庫庫名稱相同,也可以不相同。

查看:
SQL>show parameter db_name
NAME                  TYPE       VALUE
------------------------------------ ----------- ------------------------------
db_name                 string     racdb
SQL>show parameter instance_name
NAME                   TYPE       VALUE
------------------------------------ ----------- ------------------------------
instance_name            string     racdb1


INSTANCE_NAME會影響進程的命名:
[root@racnode1 ~]# ps -ef|grep pmon
grid     7474    1 0 Dec16 ?       00:00:05 asm_pmon_+ASM1
oracle   8077    1 0 Dec16 ?       00:00:07 ora_pmon_racdb1
root    20204 20176 0 00:13 pts/1   00:00:00 grep pmon

initSID.oraorapwSID文件要與INSTANCE_NAME保持一致:
[oracle@racnode1 ~]$ cd $ORACLE_HOME/dbs
[oracle@racnode1 dbs]$ ls
hc_DBUA0.dat hc_racdb1.dat init.ora initracdb1.ora orapwracdb1 snapcf_racdb1.f

2.5 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可以是多個值。
       Data Guard中,如果採用動態註冊,建議在primarystandby上使用相同的service_names,這樣可能便於盡可能的實現透明切換。
      如果配置了靜態註冊的監聽在primarystandby上也務必保持在listener中要求輸入的服務名相同,盡可能的實現透明切換。

查看service_names
SQL> show parameter service_names

NAME                  TYPE       VALUE
------------------------------------ ----------- ------------------------------
service_names           string     orcl


2.6 GLOBAL_NAME
global_name是由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;

具體參考:
Changing the Domain in a Global Database Name

2.8 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 connects.
      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 databaseglobal_name,否則創建之後db link不能連同,缺省值是false。多用於分佈式系統。

SQL>show parameter global_names
NAME                 TYPE       VALUE
------------------------------------ ----------- -------
global_names            boolean    FALSE


. Listener.ora文件中的參數
先看一個listener.ora文件:
[oracle@dg2 admin]$ cat listener.ora
SID_LIST_LISTENER =
 (SID_LIST =
   (SID_DESC =
     (SID_NAME = PLSExtProc)
     (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
     (PROGRAM = extproc)
   )
   (SID_DESC =
     (SID_NAME = orcl)
     (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
     (GLOBAL_DBNAME = orcl)
   ) 
)


3.1 SID_NAME
      SID_NAME指數據庫的運行的實例名,和instance_name保持一致。

3.2 GLOBAL_DBNAME
      配置靜態監聽註冊時,需要輸入SIDGLOBAL_NAME我上面貼出來的內容,就是靜態監聽配置的內容。SID已經說過,和Instance_name保持一致就可以了。
      GLOBAL_DBNAMElistener配置的對外網絡連接名稱,我們可以寫成任意值。在客戶端配置監聽的tnsnames.ora文件中的service_name與這個GLOBAL_DBNAME保持一致就可以了。因為客戶端訪問數據庫是通過監聽來實現的。

      如果採用動態註冊的話,PMON進程會根據初始化參數initSID.ora中的instance_name,service_names兩個參數將實例和服務動態註冊到listener中,這時自動註冊的對外網絡連接名稱就是initSID.ora文件中service_names.
    因為service_names可以有多個值,如果有多個值,就會註冊多個。但是他們對應都是同一個instance_name這樣,我們在客戶端配置tnsnames.ora時,在service_name寫其中任意一個都可以正常連上數據庫。


. Tnsnames.ora文件

先看一個tnsnames.ora文件:
ORCL_PD =
 (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.2)(PORT = 1521))
   )
   (CONNECT_DATA =
     (SERVICE_NAME = orcl)
   )
 )

4.1 SERVICE_NAME
      如果服務器采監聽採用了靜態註冊,那麼這個參數就等於Listener.ora文件中的GLOBAL_DBNAME的值。
      如果是動態註冊,那麼這個值就是initSID.ora文件中service_names中的值。

4.2 SID
      tnsnames.ora文件中還可以使用SID如果使用該參數,只需要把該參數指定為instance_name就可以了。