利用可传输表空间技术实现数据的高效迁移

通过这个文章演示一下Oracle的表空间迁移流程以及需要注意的诸多事项。

实验目标:将oracle 10g数据库实例上的表空间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上,但是其他的其他的分区在另外的表空间上;

相关推荐