1. 程式人生 > >Change the Name of Oracle Database

Change the Name of Oracle Database

因新專案開發需要,現需建立新的測試資料庫。但目前測試伺服器沒有到位,故暫且使用以 前的測試機代替,其中安裝的Oracle資料庫僅作學習測試使用,資料庫名、例項名均為 dbtest。現在將此資料庫的資料庫名、例項名從dbtest改為CRMTEST。

以下是更改記錄,以作備忘。

測試環境

  • 作業系統:CentOS 5.5
  • 資料庫: Oracle Database 10.2.0.1.0

主要步驟

1. 先更改dbname

修改oracle資料庫的dbid和dbname,主要步驟如下:

  • 1)將資料庫啟動到mount狀態:startup mount;
  • 2)使用nid命令修改:nid target=/ DBNAME=CRMTEST
  • 3)更改初始化引數檔案中的db_namedb_name="CRMTEST"
  • 4)啟動到mount:startup mount
  • 5)以resetlogs開啟資料庫:alter database open resetlogs;

1)重啟資料庫到mount

檢視當前資料庫names設定:

SQL> show parameter name
NAME                       TYPE      VALUE
-------------------------- --------- ------------------------------
db_file_name_convert       string
db_name                    string    dbtest
db_unique_name             string    dbtest
global_names               boolean   FALSE
instance_name              string    dbtest
lock_name_space            string
log_file_name_convert      string
service_names              string    dbtest

檢視當前資料庫啟動引數型別:

SQL> show parameter spfile
NAME                       TYPE      VALUE
-------------------------- --------- ------------------------------
spfile                     string     /db/oracle/product/10.2.0/db_1/
                                      dbs/spfiledbtest.ora

從spfile建立pfile,便於直接編輯引數檔案:

SQL> create pfile from spfile;
File created.

關閉資料庫:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

啟動資料庫到mount狀態:

SQL> startup mount;
ORACLE instance started.
Total System Global Area 603979776 bytes
Fixed Size 1220796 bytes
Variable Size 163581764 bytes
Database Buffers 432013312 bytes
Redo Buffers 7163904 bytes
Database mounted.

2)執行nid,更改dbnamedbid

nid即new database id,根據dbname生成新的dbid

[[email protected] dbs]$ nid target=/ dbname=CRMTEST
DBNEWID: Release 10.2.0.1.0 - Production on Tue Oct 9 20:02:52 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to database DBTEST (DBID=1176995883)
Connected to server version 10.2.0
Control Files in database:
/db/oracle/oradata/dbtest/control01.ctl
/db/oracle/oradata/dbtest/control02.ctl
/db/oracle/oradata/dbtest/control03.ctl
Change database ID and database name DBTEST to CRMTEST? (Y/[N]) => y
Proceeding with operation
Changing database ID from 1176995883 to 889633516
Changing database name from DBTEST to CRMTEST
Control File /db/oracle/oradata/dbtest/control01.ctl - modified
Control File /db/oracle/oradata/dbtest/control02.ctl - modified
Control File /db/oracle/oradata/dbtest/control03.ctl - modified
Datafile /db/oracle/oradata/dbtest/system01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/undotbs01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/sysaux01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/users01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/crm_data01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/crm_index01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/temp01.dbf - dbid changed, wrote new name
Datafile /db/oracle/oradata/dbtest/crm_temp01.dbf - dbid changed, wrote new name
Control File /db/oracle/oradata/dbtest/control01.ctl - dbid changed, wrote new name
Control File /db/oracle/oradata/dbtest/control02.ctl - dbid changed, wrote new name
Control File /db/oracle/oradata/dbtest/control03.ctl - dbid changed, wrote new name
Instance shut down
Database name changed to CRMTEST.
Modify parameter file and generate a new password file before restarting.
Database ID for database CRMTEST changed to 889633516.
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.

3)更改initdbtest.ora,將db_name設定為CRMTEST

進入到$ORACLE_HOME/dbs目錄:

[[email protected] dbs]$ pwd
/db/oracle/product/10.2.0/db_1/dbs

更改db_name引數,改為CRMTEST:

[[email protected] dbs]$ vim initdbtest.ora
... ...
*.db_name='CRMTEST'
... ...

4)以initdbtest.ora啟動資料庫到mount狀態

SQL> startup mount pfile='/db/oracle/product/10.2.0/db_1/dbs/initdbtest.ora';
ORACLE instance started.
Total System Global Area 603979776 bytes
Fixed Size 1220796 bytes
Variable Size 163581764 bytes
Database Buffers 432013312 bytes
Redo Buffers 7163904 bytes
Database mounted.

5)以resetlogs開啟資料庫

SQL> alter database open resetlogs;
Database altered.

檢視當前資料庫的names:

SQL> show parameter name
NAME                       TYPE      VALUE
-------------------------- --------- --------------------
db_file_name_convert       string
db_name                    string    CRMTEST
db_unique_name             string    dbtest
global_names               boolean 	 FALSE
instance_name              string    dbtest
lock_name_space            string
log_file_name_convert      string
service_names              string    dbtest

到這裡,已經將dbname從dbtest更改為CRMTEST,但instance_name還是dbtest,接下來 將intance_name更改為CRMTEST。 其他的如db_unique_nameservice_names同樣更改為CRMTEST。

2. 再更改instance_name

更改資料庫的instance_name,主要步驟如下:

  • 1)建立新的密碼檔案:orapwd
  • 2)建立新的初始化引數檔案:cp init<SID>.ora to init<NEW_SID>.ora,並作修改
  • 3)以新的初始化引數檔案啟動資料庫:startup pfile='?/dbs/init<NEW_SID>.ora
  • 4)更改資料庫以spfile啟動:create spfile from pfile
  • 5)修改監聽:vim $ORACLE_HOME/network/admin/listener.ora

1)建立新的密碼檔案orapwCRMTEST

[[email protected] dbs]$ pwd
/db/oracle/product/10.2.0/db_1/dbs
[[email protected] dbs]$ orapwd file=orapwCRMTEST password=oracle entries=5
[[email protected] dbs]$ ll
total 7252
-rw-r----- 1 oracle oinstall 1544 Oct 9 20:20 hc_dbtest.dat
-rw-r--r-- 1 oracle oinstall 1436 Oct 9 20:04 initdbtest.ora
-r--r--r-- 1 oracle oinstall 982 May 24 15:20 initdbtest.ora.dist
-rw-r----- 1 oracle oinstall 12920 May 3 2001 initdw.ora
-rw-r----- 1 oracle oinstall 8385 Sep 11 1998 init.ora
-rw-r----- 1 oracle oinstall 24 May 24 14:43 lkDBTEST
-rw-r----- 1 oracle oinstall 2048 Oct 9 20:23 orapwCRMTEST
-rw-r----- 1 oracle oinstall 1536 Sep 21 19:31 orapwdbtest
-rw-r----- 1 oracle oinstall 7356416 Oct 9 20:06 snapcf_dbtest.f
-rw-r----- 1 oracle oinstall 3584 Oct 9 20:17 spfiledbtest.ora

2)建立initCRMTEST.ora檔案,設定相關引數

拷貝initdbtest.ora到initCRMTEST.ora:

[[email protected] dbs]$ cp initdbtest.ora initCRMTEST.ora

更改initCRMTEST.ora檔案,主要是instance_name,db_unique_name,dispatcher等:

[[email protected] dbs]$ vim initCRMTEST.ora 
CRMTEST.__db_cache_size=432013312
CRMTEST.__java_pool_size=4194304
CRMTEST.__large_pool_size=4194304
CRMTEST.__shared_pool_size=155189248
CRMTEST.__streams_pool_size=0
... ...
*.db_name='CRMTEST'
*.db_unique_name='CRMTEST'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=CRMTESTXDB)'
... ...

3)以initCRMTEST.ora啟動資料庫

因例項名已經從dbtest改為CRMTEST,需重新設定ORACLE_SID

[[email protected] dbs]$ export ORACLE_SID=CRMTEST

以initCRMTEST.ora重啟資料庫:

SQL> conn /as sysdba
Connected.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='/db/oracle/product/10.2.0/db_1/dbs/initCRMTEST.ora';
ORACLE instance started.
Total System Global Area 603979776 bytes
Fixed Size 1220796 bytes
Variable Size 163581764 bytes
Database Buffers 432013312 bytes
Redo Buffers 7163904 bytes
Database mounted.
Database opened.

檢視當前資料庫的names

SQL> show parameter name
NAME                        TYPE      VALUE
--------------------------- --------- ------------------
db_file_name_convert        string
db_name                     string    CRMTEST
db_unique_name              string    CRMTEST
global_names                boolean   FALSE
instance_name               string    CRMTEST
lock_name_space             string
log_file_name_convert       string
service_names               string    dbtest

4)建立spfile,並以spfile重啟資料庫

從pfile建立spfile:

SQL> create spfile from pfile;
File created.

關閉資料庫:

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

啟動資料庫:

SQL> startup;
ORACLE instance started.
Total System Global Area 603979776 bytes
Fixed Size 1220796 bytes
Variable Size 163581764 bytes
Database Buffers 432013312 bytes
Redo Buffers 7163904 bytes
Database mounted.
Database opened.

確認當前資料庫使用的引數檔案型別:

SQL> show parameter spfile;
NAME                       TYPE     VALUE
-------------------------- -------- ------------------------------
spfile                     string   /db/oracle/product/10.2.0/db_1/
									dbs/spfileCRMTEST.ora

更改service_names:

SQL> alter system set service_names=CRMTEST;

System altered.

再次檢視當前資料庫的names:

SQL> show parameter name
NAME                        TYPE      VALUE
--------------------------- --------- ------------------
db_file_name_convert        string
db_name                     string    CRMTEST
db_unique_name              string    CRMTEST
global_names                boolean   FALSE
instance_name               string    CRMTEST
lock_name_space             string
log_file_name_convert       string
service_names               string    CRMTEST

檢視當前資料庫的歸檔模式:

SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 1
Next log sequence to archive 1
Current log sequence 1

5)修改並重啟監聽

修改監聽中的SID_NAMEGLOBAL_DBNAME

[[email protected] admin]$ vim listener.ora
# listener.ora Network Configuration File: /db/oracle/product/10.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
    (SID_LIST =
        (SID_DESC =
            (SID_NAME = PLSExtProc)
            (ORACLE_HOME = /db/oracle/product/10.2.0/db_1)
            (PROGRAM = extproc)
        )
        (SID_DESC =
            (GLOBAL_DBNAME= CRMTEST)
            (ORACLE_HOME = /db/oracle/product/10.2.0/db_1)
            (SID_NAME = CRMTEST)
        )
    )

LISTENER =
    (DESCRIPTION_LIST =
        (DESCRIPTION =
            (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
            (ADDRESS = (PROTOCOL = TCP)(HOST = shoptest.egolife.com)(PORT = 1521))
        )
)

重啟監聽:

[[email protected] admin]$ lsnrctl start
LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 10-OCT-2012 19:52:12
Copyright (c) 1991, 2005, Oracle. All rights reserved.
Starting /db/oracle/product/10.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.1.0 - Production
System parameter file is /db/oracle/product/10.2.0/db_1/network/admin/listener.ora
Log messages written to /db/oracle/product/10.2.0/db_1/network/log/listener.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=shoptest.egolife.com)(PORT=1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date 10-OCT-2012 19:52:13
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /db/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File /db/oracle/product/10.2.0/db_1/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=shoptest.egolife.com)(PORT=1521)))
Services Summary...
Service "CRMTEST" has 1 instance(s).
Instance "CRMTEST", status UNKNOWN, has 1 handler(s) for this service...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

最後,注意更改bash環境變數,設定ORACLE_SID=CRMTEST,之前使用 export ORACLE_SID=CRMTEST僅對當前的使用者有效

[[email protected] admin]$ vim ~/.bash_profile
... ...
export ORACLE_SID=CRMTEST
... ...

測試和確認

這樣資料庫的instance_name就更改完成,下面進行一些簡單的測試,確認更改成功,主 要測試一下幾個方面:

  • 監聽正常,可以接收連線請求:tnsping crmtest
  • 密碼檔案生效:conn [email protected] /as sysdba

1. 監聽測試

伺服器端連線測試

配置tnsnames.ora:

[[email protected] admin]$ vim tnsnames.ora
CRMTEST =
    (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = shoptest.egolife.com)(PORT = 1521))
        (CONNECT_DATA =
            (SERVER = DEDICATED)
            (SERVICE_NAME = CRMTEST)
        )
    )

EXTPROC_CONNECTION_DATA =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
        )
        (CONNECT_DATA =
            (SID = PLSExtProc)
           (PRESENTATION = RO)
        )
    )

使用tnsping,測試crmtest連線是否正常:

[[email protected] admin]$ tnsping crmtest
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 10-OCT-2012 19:54:31
Copyright (c) 1997, 2005, Oracle. All rights reserved.

Used parameter files:
/db/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = shoptest.egolife.com)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = CRMTEST)))
OK (0 msec)

Windows客戶端連線測試

同樣,先配置tnsnames.ora,然後執行tnsping crmtest,確認是否可以連線:

C:\> tnsping crmtest
TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 10-10月-2012 20:09:57
Copyright (c) 1997, 2010, Oracle. All rights reserved.

已使用的引數檔案:
D:\Oracle\11g\product\11.2.0\db_1\network\admin\sqlnet.ora

已使用 TNSNAMES 介面卡來解析別名
嘗試連線 (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = shoptest.egolife.com)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = CRMTEST)))
OK (0 毫秒)

2. orapwCRMTEST密碼檔案生效測試

再使用sqlplus以sysdba身份登入,檢查密碼檔案是否生效:

C:\>sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 10月 10 20:11:38 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
SQL> conn [email protected]/as sysdba
輸入口令:
已連線。
SQL>

參考

blog comments powered by Disqus