1 说明
在Oracle 12c 的CDB 架构中,可以对PDB 进行unplug 和 plug。 就是把一个库从CDB 的架构拔出,然后在附加到CDB中。 那么可以利用这个特性,在2个不同的数据库之间进行数据迁移。
源库:
SQL> select * from v$version; BANNER CON_ID -------------------------------------------------------------------------------- ---------- Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0 PL/SQL Release 12.1.0.2.0 - Production 0 CORE 12.1.0.2.0 Production 0 TNS for Linux: Version 12.1.0.2.0 - Production 0 NLSRTL Version 12.1.0.2.0 - Production 0 SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 READ WRITE NO 4 CNDBA READ WRITE NO SQL>
目标库:
SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED
现在的操作,就是把CNDBA 这个PDB 从源库unplug 下来,然后plug 到目标库上。
2 在源库进行unplug 操作
先查看CNDBA 这个PDB数据文件的信息:http://www.cndba.cn/cndba/dave/article/223http://www.cndba.cn/cndba/dave/article/223
SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 READ WRITE NO 4 CNDBA READ WRITE NO SQL> alter session set container=CNDBA; Session altered. SQL> select file_name from dba_data_files; FILE_NAME ------------------------------------------------------------------------------------------------------------------------ /u01/app/oracle/oradata/dave/cndba/users.dbf /u01/app/oracle/oradata/dave/cndba/ado1.dbf /u01/app/oracle/oradata/dave/cndba/sysaux.dbf /u01/app/oracle/oradata/dave/cndba/ado2.dbf /u01/app/oracle/oradata/dave/cndba/system.dbf
关闭PDB,并unplug:
SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 4 CNDBA READ WRITE NO SQL> alter pluggable database cndba close; Pluggable database altered. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 4 CNDBA MOUNTED SQL> alter pluggable database cndba unplug into '/tmp/cndba.xml'; alter pluggable database cndba unplug into '/tmp/cndba.xml' * ERROR at line 1: ORA-65040: operation not allowed from within a pluggable database SQL> alter session set container=CDB$ROOT; Session altered. SQL> alter pluggable database cndba unplug into '/tmp/cndba.xml'; Pluggable database altered. SQL> drop pluggable database cndba keep datafiles; Pluggable database dropped. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 READ WRITE NO SQL>
3 在目标库进行plug 操作
在目标库创建与源库相同的目录结构,当然也可以不同。
3.1 源库与目标库目录结构相同的情况
在目标库上创建目录:http://www.cndba.cn/cndba/dave/article/223
[oracle@Ora12c cndba]$ pwd /u01/app/oracle/oradata/dave/cndba[oracle@Ora12c cndba]$ ls[oracle@Ora12c cndba]$
把源库上CNDBA 对应的所有数据文件,和含有元数据的XML 文件全部cp过去:
[oracle@Ora12c cndba]$ pwd /u01/app/oracle/oradata/dave/cndba[oracle@Ora12c cndba]$ ls ado1.dbf ado2.dbf sysaux.dbf system.dbf users.dbf[oracle@Ora12c cndba]$ scp * 192.168.3.40:`pwd` oracle@192.168.3.40's password: ado1.dbf 100% 50MB 50.0MB/s 00:01 ado2.dbf 100% 50MB 50.0MB/s 00:01 sysaux.dbf 100% 710MB 54.6MB/s 00:13 system.dbf 100% 810MB 57.9MB/s 00:14 users.dbf 100% 5128KB 5.0MB/s 00:00 [oracle@Ora12c cndba]$[oracle@Ora12c cndba]$ scp /tmp/cndba.xml 192.168.3.40:/tmp oracle@192.168.3.40's password: cndba.xml 100% 6884 6.7KB/s 00:00 [oracle@Ora12c cndba]$
在目标库上plug:
SET SERVEROUTPUT ON DECLARE l_result BOOLEAN; BEGIN l_result := DBMS_PDB.check_plug_compatibility( pdb_descr_file => '/tmp/cndba.xml', pdb_name => 'cndba'); IF l_result THEN DBMS_OUTPUT.PUT_LINE('compatible'); ELSE DBMS_OUTPUT.PUT_LINE('incompatible'); END IF; END; / compatible SQL> create pluggable database cndba using '/tmp/cndba.xml' nocopy; Pluggable database created. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 CNDBA MOUNTED SQL> alter pluggable database cndba open; Pluggable database altered. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 CNDBA READ WRITE NO SQL>
查看数据文件位置,并测试:
SQL> select file_name from dba_data_files; FILE_NAME -------------------------------------------------------------------------------- /u01/app/oracle/oradata/dave/cndba/ado2.dbf /u01/app/oracle/oradata/dave/cndba/ado1.dbf /u01/app/oracle/oradata/dave/cndba/users.dbf /u01/app/oracle/oradata/dave/cndba/sysaux.dbf /u01/app/oracle/oradata/dave/cndba/system.dbf SQL> create table cndba as select * from dba_data_files; Table created. SQL> select count(1) from cndba; COUNT(1) ---------- 5
3.2 源库与目标库目录结构不同的情况
先把刚plug上去的cndba 这个PDB drop 掉:
SQL> alter session set container=CDB$ROOT; Session altered. SQL> alter pluggable database cndba close; Pluggable database altered. SQL> drop pluggable database cndba including datafiles; Pluggable database dropped. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED SQL>
新建个目录,然后把XML 文件和数据文件复制过来:
[oracle@Ora12c cndba]# mkdir -p /u01/app/oracle/oradata/dave/cndba2[oracle@Ora12c cndba]$ scp * 192.168.3.40:/u01/app/oracle/oradata/dave/cndba2 oracle@192.168.3.40's password: ado1.dbf 100% 50MB 50.0MB/s 00:01 ado2.dbf 100% 50MB 50.0MB/s 00:00 sysaux.dbf 100% 710MB 44.4MB/s 00:16 system.dbf 100% 810MB 45.0MB/s 00:18 users.dbf 100% 5128KB 5.0MB/s 00:00 [oracle@Ora12c cndba]$
因为路径不一样,需要使用source_file_name_convert 来plug。source_file_name_convert 就是用来更改XML 文件记录的文件的位置。
官方文档对source_file_name_convert的说明:
Specify this clause only if the contents of the XML file do not accurately describe the locations of the source files. If the files that must be used to plug in the source database are no longer in the location specified in the XML file, then use this clause to map the specified file names to the actual file names.http://www.cndba.cn/cndba/dave/article/223
SQL> create pluggable database cndba using '/tmp/cndba.xml' source_file_name_convert=('/u01/app/oracle/oradata/dave/cndba','/u01/app/oracle/oradata/dave/cndba2') nocopy tempfile reuse; Pluggable database created. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 CNDBA MOUNTED SQL> alter pluggable database cndba open; Pluggable database altered. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 CNDBA READ WRITE NO SQL> alter session set container=CNDBA; Session altered. SQL> select file_name from dba_data_files; FILE_NAME -------------------------------------------------------------------------------- /u01/app/oracle/oradata/dave/cndba2/ado2.dbf /u01/app/oracle/oradata/dave/cndba2/ado1.dbf /u01/app/oracle/oradata/dave/cndba2/users.dbf /u01/app/oracle/oradata/dave/cndba2/sysaux.dbf /u01/app/oracle/oradata/dave/cndba2/system.dbf
切换成功。
4 扩展说明
上面演示的是使用Oracle 12c 的unplug 和plug 进行数据迁移,在12c 之前的版本里,我们迁移数据,可以使用数据泵,RMAN,传输表空间,而在12c 的plug的功能,明显进一步简化了迁移的成本。
如果在库大的情况,我们可以考虑另外一种方法:
1. Unplug db。
2. 启动plug db,到只读状态,保证部分业务。
3. 迁移到新服务器,进行plug。
4. 切换业务。
这个过程,基本是一个平滑的操作,而影响这个操作的主要因素就是带宽和IO。
在Infiniband 和 PCIE 闪存卡的硬件加速情况下,IB 每秒最高到40G,闪存卡可以支持2G的传输。 对于我们大数据量的迁移,结合12c 的plug 功能,真的是一个非常高效的方法。
之前一直提到XML 的元数据文件,其主要就是记录我们PDB的元数据,类似与传输表空间。 这样元数据我们用XML 导入,CDB 中就不用关系我们数据文件,只要XML 是正确的就可以完成迁移操作。
附录一个完成的XML 元数据文档:
[root@Ora12c cndba]# cat /tmp/cndba.xml <?xml version="1.0" encoding="UTF-8"?> <PDB> <xmlversion>1</xmlversion> <pdbname>CNDBA</pdbname> <cid>4</cid> <byteorder>1</byteorder> <vsn>202375680</vsn> <vsns> <vsnnum>12.1.0.2.0</vsnnum> <cdbcompt>12.1.0.2.0</cdbcompt> <pdbcompt>12.1.0.2.0</pdbcompt> <vsnlibnum>0.0.0.0.22</vsnlibnum> <vsnsql>22</vsnsql> <vsnbsv>8.0.0.0.0</vsnbsv> </vsns> <dbid>142169581</dbid> <ncdb2pdb>0</ncdb2pdb> <cdbid>925913465</cdbid> <guid>144C163289BF0781E0531E02A8C0AFE3</guid> <uscnbas>2558320</uscnbas> <uscnwrp>0</uscnwrp> <rdba>4194824</rdba> <tablespace> <name>SYSTEM</name> <type>0</type> <tsn>0</tsn> <status>1</status> <issft>0</issft> <file> <path>/u01/app/oracle/oradata/dave/cndba/system.dbf</path> <afn>22</afn> <rfn>1</rfn> <createscnbas>2088055</createscnbas> <createscnwrp>0</createscnwrp> <status>1</status> <fileblocks>103680</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <fdbid>142169581</fdbid> <fcpsw>0</fcpsw> <fcpsb>2558316</fcpsb> <frlsw>0</frlsw> <frlsb>1594143</frlsb> <frlt>863031228</frlt> </file> </tablespace> <tablespace> <name>SYSAUX</name> <type>0</type> <tsn>1</tsn> <status>1</status> <issft>0</issft> <file> <path>/u01/app/oracle/oradata/dave/cndba/sysaux.dbf</path> <afn>23</afn> <rfn>3</rfn> <createscnbas>2088058</createscnbas> <createscnwrp>0</createscnwrp> <status>1</status> <fileblocks>90880</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <fdbid>142169581</fdbid> <fcpsw>0</fcpsw> <fcpsb>2558316</fcpsb> <frlsw>0</frlsw> <frlsb>1594143</frlsb> <frlt>863031228</frlt> </file> </tablespace> <tablespace> <name>TEMP</name> <type>1</type> <tsn>3</tsn> <status>1</status> <issft>0</issft> <bmunitsize>128</bmunitsize> <file> <path>/u01/app/oracle/oradata/dave/cndba/temp.dbf</path> <afn>4</afn> <rfn>1</rfn> <createscnbas>2088056</createscnbas> <createscnwrp>0</createscnwrp> <status>0</status> <fileblocks>7680</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <autoext>1</autoext> <maxsize>4194302</maxsize> <incsize>80</incsize> </file> </tablespace> <tablespace> <name>USERS</name> <type>0</type> <tsn>4</tsn> <status>1</status> <issft>0</issft> <file> <path>/u01/app/oracle/oradata/dave/cndba/users.dbf</path> <afn>24</afn> <rfn>6</rfn> <createscnbas>2088061</createscnbas> <createscnwrp>0</createscnwrp> <status>1</status> <fileblocks>640</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <fdbid>142169581</fdbid> <fcpsw>0</fcpsw> <fcpsb>2558316</fcpsb> <frlsw>0</frlsw> <frlsb>1594143</frlsb> <frlt>863031228</frlt> </file> </tablespace> <tablespace> <name>ADO1</name> <type>0</type> <tsn>6</tsn> <status>1</status> <issft>0</issft> <file> <path>/u01/app/oracle/oradata/dave/cndba/ado1.dbf</path> <afn>25</afn> <rfn>5</rfn> <createscnbas>2088063</createscnbas> <createscnwrp>0</createscnwrp> <status>1</status> <fileblocks>6400</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <fdbid>142169581</fdbid> <fcpsw>0</fcpsw> <fcpsb>2558316</fcpsb> <frlsw>0</frlsw> <frlsb>1594143</frlsb> <frlt>863031228</frlt> </file> </tablespace> <tablespace> <name>ADO2</name> <type>0</type> <tsn>7</tsn> <status>1</status> <issft>0</issft> <file> <path>/u01/app/oracle/oradata/dave/cndba/ado2.dbf</path> <afn>26</afn> <rfn>2</rfn> <createscnbas>2088065</createscnbas> <createscnwrp>0</createscnwrp> <status>1</status> <fileblocks>6400</fileblocks> <blocksize>8192</blocksize> <vsn>202375680</vsn> <fdbid>142169581</fdbid> <fcpsw>0</fcpsw> <fcpsb>2558316</fcpsb> <frlsw>0</frlsw> <frlsb>1594143</frlsb> <frlt>863031228</frlt> </file> </tablespace> <optional> <ncdb2pdb>0</ncdb2pdb> <csid>852</csid> <ncsid>2000</ncsid> <options> <option>APS=12.1.0.2.0</option> <option>CATALOG=12.1.0.2.0</option> <option>CATJAVA=12.1.0.2.0</option> <option>CATPROC=12.1.0.2.0</option> <option>CONTEXT=12.1.0.2.0</option> <option>DV=12.1.0.2.0</option> <option>JAVAVM=12.1.0.2.0</option> <option>OLS=12.1.0.2.0</option> <option>ORDIM=12.1.0.2.0</option> <option>OWM=12.1.0.2.0</option> <option>SDO=12.1.0.2.0</option> <option>XDB=12.1.0.2.0</option> <option>XML=12.1.0.2.0</option> <option>XOQ=12.1.0.2.0</option> </options> <olsoid>0</olsoid> <dv>0</dv> <APEX>4.2.5.00.08:1</APEX> <parameters> <parameter>processes=300</parameter> <parameter>memory_target=1627389952</parameter> <parameter>db_block_size=8192</parameter> <parameter>compatible='12.1.0.2.0'</parameter> <parameter>enable_pluggable_database=TRUE</parameter> <spfile>*.heat_map='ON'</spfile> <spfile>*.open_cursors=300</spfile> </parameters> <sqlpatches> <sqlpatch>PSU bundle patch 4 (Database Patch Set Update : 12.1.0.2.4 (20831110)): APPLY SUCCESS</sqlpatch> </sqlpatches> <tzvers> <tzver>primary version:18</tzver> <tzver>secondary version:0</tzver> </tzvers> <walletkey>0</walletkey> <services> <service>cndbaXDB,cndbaXDB</service> </services> <opatches> <opatch>19769480</opatch> <opatch>20299023</opatch> <opatch>20831110</opatch> </opatches> <hasclob>1</hasclob> <awr> <loadprofile>CPU Usage Per Sec=0.000000</loadprofile> <loadprofile>DB Block Changes Per Sec=0.000000</loadprofile> <loadprofile>Database Time Per Sec=0.000000</loadprofile> <loadprofile>Executions Per Sec=0.000000</loadprofile> <loadprofile>Hard Parse Count Per Sec=0.000000</loadprofile> <loadprofile>Logical Reads Per Sec=0.000000</loadprofile> <loadprofile>Logons Per Sec=0.000000</loadprofile> <loadprofile>Physical Reads Per Sec=0.000000</loadprofile> <loadprofile>Physical Writes Per Sec=0.000000</loadprofile> <loadprofile>Redo Generated Per Sec=0.000000</loadprofile> <loadprofile>Total Parse Count Per Sec=0.000000</loadprofile> <loadprofile>User Calls Per Sec=0.000000</loadprofile> <loadprofile>User Rollbacks Per Sec=0.000000</loadprofile> <loadprofile>User Transaction Per Sec=0.000000</loadprofile> </awr> <hardvsnchk>0</hardvsnchk> </optional> </PDB>[root@Ora12c cndba]#
版权声明:本文为博主原创文章,未经博主允许不得转载。