【實驗】利用可傳輸表空間技術實現資料的高效遷移

secooler發表於2009-08-25
透過這個文章演示一下Oracle的表空間遷移流程以及需要注意的諸多事項。

實驗目標:將ora10g資料庫例項上的表空間TBS_SEC_D遷移到secooler資料庫例項上
作業系統:Redhat 5.3
資料庫:Oracle 10.2.0.3

【實驗BEGIN】
【注意事項一】:匯入之前,目標資料庫中使用者必須已經存在存在。
【注意事項二】:匯入之前,目標資料庫中不能存在同名的表空間,如遷移同名的表空間,需要對遷移之前的源資料庫或待遷入資料庫中的表空間改名。

1.檢查源資料庫的表空間是否是“自包含”的
1)以sys使用者登入資料庫
sec@ora10g> conn / as sysdba
Connected.

2)使用dbms_tts.transport_set_check對待遷移表空間進行檢查,這裡待表空間的名字是TBS_SEC_D
sys@ora10g> exec dbms_tts.transport_set_check('TBS_SEC_D',true);

PL/SQL procedure successfully completed.

3)透過transport_set_violations檢視檢視是否有違反“自包含”的內容,這裡顯示結果是沒有,所以可以對完成TBS_SEC_D表空間的遷移
sys@ora10g> select * from transport_set_violations;

no rows selected

簡單列一下“非自包含”的四種可能情況以及應對方法:
--假設待遷移的表空間名字只是:TBS_SEC_D
(1)【索引】表空間TBS_SEC_D上存在索引,但是這個索引的基表在另外一個表空間上(後面的實驗將會演示違反這種約束的情況);
(2)【LOB】表儲存在表空間TBS_SEC_D上,但是表上的LOB欄位儲存在其他表空間上;
(3)【約束】表的約束有的在表空間TBS_SEC_D上,但是其他的約束在另外的表空間上;
(4)【分割槽表】分割槽表的一些分割槽在表空間TBS_SEC_D上,但是其他的其他的分割槽在另外的表空間上。

如果違反上述的條件,單獨想要匯出表空間TBS_SEC_D是不行的,處理方法:
第一種處理方法:連帶相關的表空間一起匯出
第二種處理方法:預處理那些不在一起的表空間資料到TBS_SEC_D上,然後就可以匯出表空間TBS_SEC_D了

2.將待匯出的表空間TBS_SEC_D修改為“只讀”——————這一步很關鍵
sys@ora10g> alter tablespace TBS_SEC_D read only;

Tablespace altered.

3.以SYSDBA許可權匯出表空間
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:54:22 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace TBS_SEC_D ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

OK,匯出成功。
表空間匯出主要是transport_tablespace=y這個引數在起作用,看提示資訊,這裡匯出的exp_TBS.dmp檔案中是不包含物件資料的,僅包含表空間的“後設資料”,真正的資料還在表空間對應的物理資料檔案上,因此使用表空間傳輸技術完成匯入時需要的不僅僅是這個
exp_TBS.dmp匯出檔案,還需要表空間對應的資料檔案。

4.不要著急將表空間TBS_SEC_D恢復為“讀寫”狀態,需要先將匯出的exp_TBS.dmp檔案和組成表空間的物理資料檔案傳送到需要匯入的secooler資料庫伺服器上
這裡需要注意的是:要以二進位制(bin)的模式傳輸資料。
我習慣於使用scp命令完成資料檔案的傳輸。
最好將資料檔案放置到目標資料庫資料檔案存放的目錄,以便統一進行管理。

5.OK,傳輸完成後,現在可以將表空間TBS_SEC_D恢復為“讀寫”狀態了
sys@ora10g> alter tablespace TBS_SEC_D read write;

Tablespace altered.

6.在目標資料庫(secooler資料庫例項)中匯入表空間
secooler@dbserver /imp$ imp "'"/ as sysdba"'" file='/imp/exp_TBS.dmp' transport_tablespace=y datafiles='/imp/tbs_sec_d01.dbf' tablespaces=TBS_SEC_D tts_owners=sec fromuser=sec touser=sec

Import: Release 10.2.0.3.0 - Production on Tue Aug 25 21:27:37 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing SEC's objects into SEC
. . importing table                         "TEST"
. importing SYS's objects into SYS
Import terminated successfully without warnings.
secooler@dbserver /imp$


7.透過登陸到sec使用者中查詢資料庫物件,驗證資料已經成功匯入。


8.將表空間置為可讀寫狀態,完成整個表空間的遷移任務。
sec@secooler> select TABLESPACE_NAME,STATUS from dba_tablespaces where TABLESPACE_NAME='TBS_SEC_D';

TABLESPACE_NAME                STATUS
------------------------------ ---------
TBS_SEC_D                      READ ONLY

sec@secooler> alter tablespace SEC_D read write;

Tablespace altered.

sec@secooler> select TABLESPACE_NAME,STATUS from dba_tablespaces where TABLESPACE_NAME='TBS_SEC_D';

TABLESPACE_NAME                STATUS
------------------------------ ---------
TBS_SEC_D                      ONLINE


【實驗補充ing】
【模擬違反“自包含”第一條原則過程】

sec@ora10g> create table t (x number) tablespace USERS;

Table created.

sec@ora10g> create index t_idx on t(x) tablespace TBS_SEC_D;

Index created.

sec@ora10g> conn / as sysdba
Connected.
sys@ora10g> exec dbms_tts.transport_set_check('USERS',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

no rows selected

sys@ora10g> exec dbms_tts.transport_set_check('USERS',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

no rows selected

sys@ora10g> exec dbms_tts.transport_set_check('TBS_SEC_D',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

VIOLATIONS
------------------------------------------------
Index SEC.T_IDX in tablespace TBS_SEC_D points to table SEC.T in tablespace USERS

將TBS_SEC_D,USERS兩個表空間同時匯出不會有問題:
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D,USERS triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:09 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace TBS_SEC_D ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

單獨將USERS表空間同時匯出也不會有問題:
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=USERS triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:19 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

但是,單獨將TBS_SEC_D表空間同時就會報錯,因為違反了一下原則:
【索引】表空間TBS_SEC_D上存在索引,但是這個索引的基表在另外一個表空間上(後面的實驗將會演示違反這種約束的情況)
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:25 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
EXP-00008: ORACLE error 29341 encountered
ORA-29341: The transportable set is not self-contained
ORA-06512: at "SYS.DBMS_PLUGTS", line 1387
ORA-06512: at line 1
EXP-00000: Export terminated unsuccessfully

======================================================================
【注意】不相同的資料庫字符集和國家字符集是不能完成表空間遷移的!報錯如下,要多加註意。
bomsdb1@testdb183 /imp$ imp "'"/ as sysdba"'" file='/imp/exp_TBS.dmp' transport_tablespace=y datafiles='/imp/tbs_sec_d01.dbf' tablespaces=TBS_SEC_D tts_owners=sec fromuser=sec touser=sec

Import: Release 10.2.0.3.0 - Production on Tue Aug 25 20:18:10 2009

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
import server uses WE8ISO8859P1 character set (possible charset conversion)
export server uses UTF8 NCHAR character set (possible ncharset conversion)
IMP-00017: following statement failed with ORACLE error 29345:
 "BEGIN   sys.dbms_plugts.beginImport ('10.2.0.3.0',873,'871',13,'Linux 64-bi"
 "t for AMD',12006,39801,1,0,0,0); END;"
IMP-00003: ORACLE error 29345 encountered
ORA-29345: cannot plug a tablespace into a database using an incompatible character set
ORA-06512: at "SYS.DBMS_PLUGTS", line 2386
ORA-06512: at "SYS.DBMS_PLUGTS", line 1946
ORA-06512: at line 1
IMP-00000: Import terminated unsuccessfully


【最後小結】
表空間遷移技術可以非常高效的完成資料的遷移任務,所用時間基本等於物理複製資料檔案的時間。不過有一些具體環境的限制,在真正使用之前,需要
進行嚴格的測試。

將完成表空間遷移過程中需要注意的事項列一下,如果不全,請大家補充。
【注意事項一】:匯入之前,目標資料庫中使用者必須已經存在存在。
【注意事項二】:匯入之前,目標資料庫中不能存在同名的表空間,如遷移同名的表空間,需要對遷移之前的源資料庫或待遷入資料庫中的表空間改名。
【注意事項三】:匯出前需要將表空間置為“只讀狀態”
【注意事項四】:需要以SYSDBA許可權完成表空間遷移
【注意事項五】:表空間需要“自包含”,不符合“自包含”的情況如下
(1)【索引】表空間TBS_SEC_D上存在索引,但是這個索引的基表在另外一個表空間上(後面的實驗將會演示違反這種約束的情況);
(2)【LOB】表儲存在表空間TBS_SEC_D上,但是表上的LOB欄位儲存在其他表空間上;
(3)【約束】表的約束有的在表空間TBS_SEC_D上,但是其他的約束在另外的表空間上;
(4)【分割槽表】分割槽表的一些分割槽在表空間TBS_SEC_D上,但是其他的其他的分割槽在另外的表空間上;

Goodluck.

-- The End --

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

相關文章