【原創】使用nid命令修改資料庫名稱

木頭一個發表於2008-05-22

nid是Oracle用來更改資料庫名稱的自帶工具.它可以直接修改資料庫名稱,而無需通過以前需要重建控制檔案的方法來改變.

nid命令的使用方法如下:

C:\WINDOWS>nid

DBNEWID: Release 10.2.0.1.0 - Production on Thu May 22 21:56:48 2008

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Keyword     Description                    (Default)
----------------------------------------------------
TARGET      Username/Password              (NONE)
DBNAME      New database name              (NONE)
LOGFILE     Output Log                     (NONE)
REVERT      Revert failed change           NO
SETNAME     Set a new database name only   NO
APPEND      Append to output log           NO
HELP        Displays these messages        NO

下面是使用nid修改資料名稱的庫實驗:

實驗環境:WinXP SP2

資料庫版本:10.2.0.1

注:其中有些步驟可能是不需要的,主要是為了說明nid執行的兩個條件:
1、資料庫必須處於mount狀態
2、所有的資料檔案不能處於disabled狀態

具體實驗步驟:

1.使用nid將資料庫名稱由test改為t,但是提示錯誤:資料庫不能處於open的狀態

C:\WINDOWS>nid target=sys/test@test dbname=t

DBNEWID: Release 10.2.0.1.0 - Production on Thu May 22 21:58:33 2008

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to database TEST (DBID=1946053558)

NID-00121: Database should not be open


Change of database name failed during validation - database is intact.
DBNEWID - Completed with validation errors.

2.關閉資料庫,並啟動到mount狀態

sys@TEST>shut immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@TEST>startup mount
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1248504 bytes
Variable Size             134218504 bytes
Database Buffers          125829120 bytes
Redo Buffers                7139328 bytes
Database mounted.

3.再次執行nid命令,又報了錯,說有資料檔案處於disabled的狀態

C:\WINDOWS>nid target=sys/test@test dbname=t

DBNEWID: Release 10.2.0.1.0 - Production on Thu May 22 22:01:29 2008

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to database TEST (DBID=1946053558)

Connected to server version 10.2.0

Control Files in database:
    +TEST/test/control01.ctl
    +TEST/test/control02.ctl


The following datafiles are disabled:
    F:\ORACLE\PRODUCT\ORADATA\TEST\TEST01.DBF (8)

NID-00125: Database should have no disabled datafiles


Change of database name failed during validation - database is intact.
DBNEWID - Completed with validation errors.


4.檢視資料檔案的狀態,果然有檔案處於disabled的狀態

sys@TEST>select file#,enabled from v$datafile;

     FILE# ENABLED
---------- ----------
         1 READ WRITE
         2 READ WRITE
         3 READ WRITE
         4 READ WRITE
         5 READ WRITE
         6 READ WRITE
         7 READ WRITE
         8 DISABLED
         9 DISABLED
        10 READ WRITE
        11 READ WRITE
        12 READ WRITE

12 rows selected.

5.將資料庫open

sys@TEST>alter database open;

Database altered.

6.發現原來是有兩個表空間offline了

sys@TEST>select tablespace_name,status from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
UNDOTBS1                       ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE
TEST_BIG                       ONLINE
MGMT_TABLESPACE                ONLINE
MGMT_ECM_DEPOT_TS              ONLINE
TEST                           OFFLINE
TEST1                          OFFLINE
UNDO01                         ONLINE
UNDO02                         ONLINE
TEST03                         ONLINE

13 rows selected.

7.將這兩個表空間online

sys@TEST>alter tablespace test1 online;

Tablespace altered.

sys@TEST>alter tablespace test online;

Tablespace altered.

8.再次檢視,所有資料檔案的狀態都是READ WRITE的

sys@TEST>select file#,enabled from v$datafile;

     FILE# ENABLED
---------- ----------
         1 READ WRITE
         2 READ WRITE
         3 READ WRITE
         4 READ WRITE
         5 READ WRITE
         6 READ WRITE
         7 READ WRITE
         8 READ WRITE
         9 READ WRITE
        10 READ WRITE
        11 READ WRITE
        12 READ WRITE

12 rows selected.

9.再次關閉資料庫,並啟動到mount狀態

sys@TEST>shut immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
sys@TEST>startup mount
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1248504 bytes
Variable Size             134218504 bytes
Database Buffers          125829120 bytes
Redo Buffers                7139328 bytes
Database mounted.

10.執行nid命令,這次執行成功了,可以看到資料庫id改變了,由1946053558變成544433466,這就意味著原來的備份和歸檔日誌都不能再使用了

C:\WINDOWS>nid target=sys/test@test dbname=t

DBNEWID: Release 10.2.0.1.0 - Production on Thu May 22 22:14:16 2008

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to database TEST (DBID=1946053558)

Connected to server version 10.2.0

Control Files in database:
    +TEST/test/control01.ctl
    +TEST/test/control02.ctl

Change database ID and database name TEST to T? (Y/[N]) => y

Proceeding with operation
Changing database ID from 1946053558 to 544433466
Changing database name from TEST to T
    Control File +TEST/test/control01.ctl - modified
    Control File +TEST/test/control02.ctl - modified
    Datafile +TEST/test/datafile/system.258.650496175 - dbid changed, wrote new name
    Datafile +TEST/test/datafile/undotbs1.259.650496245 - dbid changed, wrote new name
    Datafile +TEST/test/datafile/sysaux.257.650496219 - dbid changed, wrote new name
    Datafile +TEST/test/datafile/users.260.650496247 - dbid changed, wrote new name
    Datafile +TEST/test/datafile/test_big.262.650496255 - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\MGMT.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\MGMT_ECM_DEPOT1.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\TEST01.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\TEST02.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\UNDO01.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\UNDO02.DBF - dbid changed, wrote new name
    Datafile F:\ORACLE\PRODUCT\ORADATA\TEST\TEST03.DBF - dbid changed, wrote new name
    Datafile +TEST/temp01.dbf - dbid changed, wrote new name
    Control File +TEST/test/control01.ctl - dbid changed, wrote new name
    Control File +TEST/test/control02.ctl - dbid changed, wrote new name
    Instance shut down

Database name changed to T.
Modify parameter file and generate a new password file before restarting.
Database ID for database T changed to 544433466.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.

11.關閉資料庫

sys@TEST>shut immediate
ORA-01507: database not mounted


ORACLE instance shut down.

12.重新啟動資料庫,在從nomount狀態啟動至mount狀態時報錯,這是因為控制檔案中的資料庫名稱已經改成t了,但引數檔案中的db_name仍然是test,所以報錯了

idle>startup
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1248504 bytes
Variable Size             125829896 bytes
Database Buffers          134217728 bytes
Redo Buffers                7139328 bytes
ORA-01103: database name 'T' in control file is not 'TEST'

13.檢視引數檔案中的db_name設定

idle>show parameter name

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

14.修改引數檔案中的db_name引數,並重啟資料庫

idle>alter system set db_name='t' scope=spfile;

System altered.

idle>shut immediate
ORA-01507: database not mounted


ORACLE instance shut down.
idle>startup
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1248504 bytes
Variable Size             125829896 bytes
Database Buffers          134217728 bytes
Redo Buffers                7139328 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open

15.由於資料庫的id變化了,需要以resetlogs的方式啟動資料庫

idle>alter database open resetlogs;

Database altered.

16.到此資料庫的名稱修改完畢

idle>show parameter name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert                 string
db_name                              string      t
db_unique_name                       string      test
global_names                         boolean     FALSE
instance_name                        string      test
lock_name_space                      string
log_file_name_convert                string
service_names                        string      t

17.重建密碼檔案,進行資料庫備份,這裡就不詳述了

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/498744/viewspace-293561/,如需轉載,請註明出處,否則將追究法律責任。

相關文章