經過這個文章演示一下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 --數據庫
來自《oracle數據庫表空間的導出》服務器