OracleDB入门5:可传输表空间Transportable Tablespace

写在前面

         本文转载自:聊聊Oracle可传输表空间(Transportable Tablespace)(上)

         因为实用,所以转载,感谢原创者

        我们在Oracle环境中,有很多进行数据备份和移植手段,如exp/imp、expdp/impdp和rman等。在这些方法中,可传输表空间(Transportable Tablespace)一直是传统意义上最快数据移植的技术手段。理想情况下,Transportable Tablespace可以实现近似网络直传的速率特点。本篇中,我们来介绍一下传输表空间技术的一些使用细节。

1、Transportable Tablespace概述

      其他传统意义上的备份迁移手段,大都是遵循“抽取-传输-还原”的模式。以expdp为例,Oracle使用专门的内部调度作业,将需要导出的数据(Used Block)转化为dmp格式文件进行存储保存。之后,通过网络进行传输到Target Envionment,最后再还原到新环境上。这种模式的加速优化,主要体现在抽取和还原上,如使用并行等手段。

      而Transportable Tablespace完全不是这样的概念。如果比喻的话,它类似一种Portable/Plugin的理念。相同平台、字符集的表空间,完全可以将数据文件直接拷贝到Target Environment。Oracle层面只需要让数据字典知道这些数据的metadata就可以了。

      相对于其他手段,Transportable Tablespace最大的好处就在于不需要进行繁复的抽取和还原过程,而且对中间环境的空间要求很小。下面通过一系列的实验来进行演示。

2、环境准备和前提条件

Oracle Transportable Tablespace(以下简称为TTS)出现的很早。传统的TTS有三个层面基础限制,分别为:

  • 表空间内容self contained。我们一次性导出的表空间(一个或者多个),不能在其他表空间中存在依赖对象。比如,我们常常将一个数据表数据和索引分布在不同的表空间上,这样如果我们使用Transportable Tablespace,就要求必须将这些表空间一次性全部导出;
  • Source和Target DB的Character Set、National Character Set必须完全相同;
  • 操作系统Source Target DB要求兼容。注意:在10g以上版本,这个限制已经取消;

       我们会在下面更加直观的介绍这些约束和检查方法。由于笔者环境的限制,一些实验只能在一台服务器上进行。具体实验选择Oracle 11gR2。

SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE        11.2.0.1.0         Production

--构造两个实验表空间

SQL> create tablespace ttstbl datafile size 10m autoextend on
  2  extent management local uniform. size 1m
  3  segment space management auto;
Tablespace created

SQL> create tablespace ttsind datafile size 10m autoextend on
  2  extent management local uniform. size 1m
  3  segment space management auto;
Tablespace created

--测试用户

SQL> create user test identified by test default tablespace ttstbl;
User created

SQL> grant resource, connect to test;
Grant succeeded

SQL> grant select_catalog_role to test;
Grant succeeded

使用test用户在表空间中创建一些对象。

SQL> conn test/test@ora11gp;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as test

SQL> create table t tablespace ttstbl as select * from dba_objects;
Table created

SQL> create index idx_t_id on t(object_id) tablespace ttsind;
Index created

3、Pre-Condition Check

作为先决条件,首选确定Source数据库的字符集信息。

SQL> select value from nls_database_parameters where parameter='NLS_CHARACTERSET';
VALUE
--------------------
AL32UTF8

SQL> select value from nls_database_parameters where parameter='NLS_NCHAR_CHARACTERSET';
VALUE
--------------------
AL16UTF16

希望导出新创建的表空间ttsind和ttstbl--查看datafile。

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name like 'TTS%';

FILE_NAME                                                                        TABLESPACE_NAME
-------------------------------------------------------------------------------- ------------------------------
/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf                      TTSTBL
/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf                      TTSIND
[oracle@bsplinux datafile]$ ls -l
total 2095500
(篇幅原因,省略部分内容)
-rw-r----- 1 oracle oinstall  10493952 Nov 19 18:04 o1_mf_ttsind_8bmyjz69_.dbf
-rw-r----- 1 oracle oinstall  20979712 Nov 19 17:59 o1_mf_ttstbl_8bmyjf3w_.dbf
-rw-r----- 1 oracle oinstall 267395072 Nov 19 18:13 o1_mf_undotbs1_7vpyc2py_.dbf
-rw-r----- 1 oracle oinstall  11804672 Nov 19 17:29 o1_mf_users_7vpyc2xd_.dbf

       传统的约束条件中,操作系统平台OS是使用TTS不能回避的因素。Oracle将支持平台划分为两个大类型BigLittle(CPU的大小端),同平台之间可以进行自由表空间移植,异平台之间不允许进行移植。注意:这个限制在Oracle 10g之后被打破。具体我们后面详细介绍。

我们可以通过查询v$transportable_platform来确定系统之间是否兼容。

SQL> col platform_name for a30;
SQL> select platform_name, ENDIAN_FORMAT from v$transportable_platform;
PLATFORM_NAME                  ENDIAN_FORMAT

------------------------------ --------------
Solaris[tm] OE (32-bit)        Big
Solaris[tm] OE (64-bit)        Big
Microsoft Windows IA (32-bit)  Little
Linux IA (32-bit)              Little
AIX-Based Systems (64-bit)     Big
HP-UX (64-bit)                 Big
HP Tru64 UNIX                  Little
HP-UX IA (64-bit)              Big
Linux IA (64-bit)              Little
HP Open VMS                    Little
Microsoft Windows IA (64-bit)  Little
IBM zSeries Based Linux        Big
Linux x86 64-bit               Little
Apple Mac OS                   Big
Microsoft Windows x86 64-bit   Little
Solaris Operating System (x86) Little
IBM Power Based Linux          Big
HP IA Open VMS                 Little
Solaris Operating System (x86- Little64)                            
Apple Mac OS (x86-64)          Little

20 rows selected

对于自己的数据库,v$database视图中可以查询自己的平台情况信息。

SQL> select PLATFORM_ID , PLATFORM_NAME from v$database;

PLATFORM_ID PLATFORM_NAME
----------- ------------------------------
         10 Linux IA (32-bit)

确定平台兼容之后,我们需要确定一次移植的表空间之间是否“Self Contained”。Oracle提供的dbms_tts包方法来进行验证。

SQL> exec dbms_tts.transport_set_check('ttstbl, ttsind',true);
PL/SQL procedure successfully completed

SQL> select * from transport_set_violations;

VIOLATIONS
--------------------------------------------------------------------------------

      如果验证出现错误,我们可以在transport_set_violations中查询到提示信息。只有解决了self contained问题,才能继续下面的步骤。

4、环境数据导出

   为了控制原库的变化,我们需要将表空间设置为只读。具体语句为:alter tablespace xxx read only

SQL> select tablespace_name, status from dba_tablespaces where tablespace_name like 'TTS%';
TABLESPACE_NAME                STATUS

------------------------------ ---------
TTSIND                         READ ONLY
TTSTBL                         READ ONLY

Oracle TTS需要使用exp/expdp将表空间的元数据Metadata信息导出为dmp文件,用于描述表空间信息。注意,这个过程时间很短,而且生成的dmp文件通常很小。
 

[root@bsplinux ~]# cd /
[root@bsplinux /]# mkdir transtts
[root@bsplinux /]# chown -R oracle:oinstall transtts/
[root@bsplinux /]# ls -l | grep transtts
drwxr-xr-x   2 oracle   oinstall  4096 Nov 19 18:19 transtts
[root@bsplinux /]#

Exp支持了TTS工作方法。

[oracle@bsplinux transtts]$ exp userid=\"/ as sysdba\" transport_tablespace=y tablespaces=ttstbl,ttsind file=ttsdmp.dmp log=res.log statistics=none


Export: Release 11.2.0.1.0 - Production on Mon Nov 19 19:32:14 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace TTSTBL ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
For tablespace TTSIND ...
. exporting cluster definitions
. exporting table definitions
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

之后,我们只需要直接将数据文件拷贝出来(cp 、scp等)

SQL> select 'cp '||file_name ||' /transtts' from dba_data_files where tablespace_name like 'TTS%';

'CP'||FILE_NAME||'/TRANSTTS'
--------------------------------------------------------------------------------
cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf /transtts
cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts

[oracle@bsplinux transtts]$ cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf /transtts
cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts
[oracle@bsplinux transtts]$ cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts
[oracle@bsplinux transtts]$

5、数据环境恢复

我们需要将数据文件通过FTP/SFTP传输到目标数据库服务器上。笔者先将数据环境还原,因为使用的是相同的数据库。

SQL> drop tablespace ttstbl including contents and datafiles;
Tablespace dropped

SQL> drop tablespace ttsind including contents and datafiles;
Tablespace dropped

SQL> select file_name from dba_data_files where tablespace_name like 'TTS%';
FILE_NAME
--------------------------------------------------------------------------------

Source数据库已经没有TTS表空间了。下面,我们将数据文件拷贝到新位置,并且导入Metadata信息。

[oracle@bsplinux transtts]$ cp o1_mf_ttstbl_8bmyjf3w_.dbf /u01/app/oradata/ORA11G/datafile/
[oracle@bsplinux transtts]$ cp o1_mf_ttsind_8bmyjz69_.dbf /u01/app/oradata/ORA11G/datafile/

[oracle@bsplinux datafile]$ ls -l
total 2095500
-rw-r----- 1 oracle oinstall  10493952 Jul  3 03:48 mytesttbl01.dbf
-rw-r----- 1 oracle oinstall  10493952 Jul  3 03:48 mytesttbl02.dbf
-rw-r----- 1 oracle oinstall 104865792 Nov 19 17:29 o1_mf_rman_ts_87bx5kcg_.dbf
-rw-r----- 1 oracle oinstall 838868992 Nov 19 19:46 o1_mf_sysaux_7vpyc2hb_.dbf
-rw-r----- 1 oracle oinstall 807411712 Nov 19 19:43 o1_mf_system_7vpyc1x7_.dbf
-rw-r----- 1 oracle oinstall  60825600 Nov 19 18:02 o1_mf_temp_7vpz05do_.tmp
-rw-r----- 1 oracle oinstall  10493952 Nov 19 19:47 o1_mf_ttsind_8bmyjz69_.dbf
-rw-r----- 1 oracle oinstall  20979712 Nov 19 19:46 o1_mf_ttstbl_8bmyjf3w_.dbf
-rw-r----- 1 oracle oinstall 267395072 Nov 19 19:47 o1_mf_undotbs1_7vpyc2py_.dbf
-rw-r----- 1 oracle oinstall  11804672 Nov 19 17:29 o1_mf_users_7vpyc2xd_.dbf

Imp命令导入

[oracle@bsplinux transtts]$ imp userid=\'/ as sysdba\' file=ttsdmp.dmp transport_tablespace=y tablespaces=ttsind,ttstbl datafiles=/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf,/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf

导入元数据后,Oracle中可以确定导入的表空间了。

SQL> select file_name,tablespace_name from dba_data_files where tablespace_name like 'TTS%';

FILE_NAME                                                                        TABLESPACE_NAME
-------------------------------------------------------------------------------- ------------------------------
/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf                      TTSIND
/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf                      TTSTBL

SQL> conn test/test@ora11gp
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as test

SQL> select count(*) from t;

  COUNT(*)
----------
     72348

注意,导入后的表空间还是read only状态,需要开启

SQL> alter tablespace ttsind read write;
Tablespace altered

SQL> alter tablespace ttstbl read write;
Tablespace altered

 上面我们给出了一个简单的TTS例子。对TTS来说,还有很多细节和复杂应用场景,留待后面继续。

其它补充

  •      在实际应用中,为了能方便的使用transportable tablespace特性,最好在创建schema时指定新的tablespace,而不是使用默认的USER表空间,这样就方便的满足self contained要求;
  • 在笔者实际使用环境中,OS是vmware虚拟级,企业版redhat,采用云平台基于标准的模板自动构建,因而轻易满足Character Set、National Character Set等要求

猜你喜欢

转载自blog.csdn.net/zhaogang1993/article/details/92721083
今日推荐