我的实验环境:
- 源数据库A机:
RHEL6.4 + Oracle 11.2.0.4
IP地址:192.168.99.159
db_name=oradb 数据库已正常运行
- 复制数据库B机:
RHEL6.4 + Oracle 11.2.0.4
IP地址:192.168.99.191
db_name=testdb 仅安装了数据库软件
1.为复制数据库做准备
登录到B机,
1.1 配置环境变量
ORACLE_SID=testdb
ORACLE_BASE=按实际配置
ORACLE_HOME=按实际配置
PATH=按实际配置
1.2 创建密码文件
orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=20
1.3 创建初始化参数文件
vi init$ORACLE_SID.ora
db_name=testdb
compatible='11.2.0.4.0'
db_create_file_dest='/u01/oradata2'
注意:
CONTROL_FILES参数,如果未使用OMF,则需要指定。我这里使用OMF。指定 db_create_file_dest即可。
DB_BLOCK_SIZE参数,大小必须与源库一致,如果源库设置,则也需要指定。我这里不需要。
1.4 监听网络配置
因为后续辅助实例启动到nomount模式远程连接,需要配置静态监听。
cd $ORACLE_HOME/network/admin
配置监听listener.ora
sid_list_listener =
(sid_list =
(sid_desc =
(global_dbname=testdb)
(sid_name=testdb)
(oracle_home=/opt/app/oracle/product/11.2.0/dbhome_1)
)
)
配置tnsnames.ora
A =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.99.159)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = oradb)
)
)
B =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.99.191)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = testdb)
)
)
2.启动辅助实例到nomount模式
登录到B机,
SQL> startup nomount
测试连接:sqlplus sys/oracle@B as sysdba
$ sqlplus sys/oracle@B as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Nov 5 14:33:45 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
SQL>
3.启动源数据库到mount或open
登录到A机,
SQL> startup
SQL> select status, instance_name from v$instance;
STATUS INSTANCE_NAME
------------ ----------------
OPEN oradb
SQL> select dbid, open_mode from v$database;
DBID OPEN_MODE
---------- --------------------
2647366668 READ WRITE
4.运行RMAN DUPLICATE命令
登录到A机:
$ rman target sys/oracle@A auxiliary sys/oracle@B
RMAN> duplicate target database to testdb from active database;
具体过程如下:
[oracle@JY-DB01 admin]$ rman target sys/oracle@A auxiliary sys/oracle@B
Recovery Manager: Release 11.2.0.4.0 - Production on Thu Nov 5 15:44:57 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORADB (DBID=2647366668)
connected to auxiliary database: TESTDB (not mounted)
RMAN> duplicate target database to testdb from active database;
Starting Duplicate Db at 2015-11-05 15:46:44
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=1 device type=DISK
contents of Memory Script:
{
sql clone "create spfile from memory";
}
executing Memory Script
sql statement: create spfile from memory
contents of Memory Script:
{
shutdown clone immediate;
startup clone nomount;
}
executing Memory Script
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 238034944 bytes
Fixed Size 2252016 bytes
Variable Size 180355856 bytes
Database Buffers 50331648 bytes
Redo Buffers 5095424 bytes
contents of Memory Script:
{
sql clone "alter system set control_files =
''/u01/oradata2/TESTDB/controlfile/o1_mf_c3p2gqj2_.ctl'' comment=
''Set by RMAN'' scope=spfile";
sql clone "alter system set db_name =
''ORADB'' comment=
''Modified by RMAN duplicate'' scope=spfile";
sql clone "alter system set db_unique_name =
''TESTDB'' comment=
''Modified by RMAN duplicate'' scope=spfile";
shutdown clone immediate;
startup clone force nomount
backup as copy current controlfile auxiliary format '/u01/oradata2/TESTDB/controlfile/o1_mf_c3p2gqjc_.ctl';
sql clone "alter system set control_files =
''/u01/oradata2/TESTDB/controlfile/o1_mf_c3p2gqjc_.ctl'' comment=
''Set by RMAN'' scope=spfile";
shutdown clone immediate;
startup clone nomount;
alter clone database mount;
}
executing Memory Script
sql statement: alter system set control_files = ''/u01/oradata2/TESTDB/controlfile/o1_mf_c3p2gqj2_.ctl'' comment= ''Set by RMAN'' scope=spfile
sql statement: alter system set db_name = ''ORADB'' comment= ''Modified by RMAN duplicate'' scope=spfile
sql statement: alter system set db_unique_name = ''TESTDB'' comment= ''Modified by RMAN duplicate'' scope=spfile
Oracle instance shut down
Oracle instance started
Total System Global Area 238034944 bytes
Fixed Size 2252016 bytes
Variable Size 180355856 bytes
Database Buffers 50331648 bytes
Redo Buffers 5095424 bytes
Starting backup at 2015-11-05 15:47:29
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=46 device type=DISK
channel ORA_DISK_1: starting datafile copy
copying current control file
output file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/snapcf_oradb.f tag=TAG20151105T154729 RECID=27 STAMP=894988050
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-11-05 15:47:31
sql statement: alter system set control_files = ''/u01/oradata2/TESTDB/controlfile/o1_mf_c3p2gqjc_.ctl'' comment= ''Set by RMAN'' scope=spfile
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 238034944 bytes
Fixed Size 2252016 bytes
Variable Size 180355856 bytes
Database Buffers 50331648 bytes
Redo Buffers 5095424 bytes
database mounted
contents of Memory Script:
{
set newname for clone datafile 1 to new;
set newname for clone datafile 2 to new;
set newname for clone datafile 3 to new;
set newname for clone datafile 4 to new;
backup as copy reuse
datafile 1 auxiliary format new
datafile 2 auxiliary format new
datafile 3 auxiliary format new
datafile 4 auxiliary format new
;
sql 'alter system archive log current';
}
executing Memory Script
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting backup at 2015-11-05 15:47:57
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/oradata/ORADB/datafile/o1_mf_system_c2v0xxm5_.dbf
output file name=/u01/oradata2/TESTDB/datafile/o1_mf_system_4uqlgrpe_.dbf tag=TAG20151105T154757
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u01/oradata/ORADB/datafile/o1_mf_sysaux_c2v0xxp1_.dbf
output file name=/u01/oradata2/TESTDB/datafile/o1_mf_sysaux_4vqlgrq7_.dbf tag=TAG20151105T154757
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/oradata/ORADB/datafile/o1_mf_undotbs1_c2v0xxpb_.dbf
output file name=/u01/oradata2/TESTDB/datafile/o1_mf_undotbs1_50qlgrqm_.dbf tag=TAG20151105T154757
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/oradata/ORADB/datafile/users01.dbf
output file name=/u01/oradata2/TESTDB/datafile/o1_mf_users_51qlgrqp_.dbf tag=TAG20151105T154757
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-11-05 15:48:43
sql statement: alter system archive log current
contents of Memory Script:
{
backup as copy reuse
archivelog like "/u01/oradb_arch1/1_146_894101903.dbf" auxiliary format
"/opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf" ;
catalog clone archivelog "/opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf";
switch clone datafile all;
}
executing Memory Script
Starting backup at 2015-11-05 15:48:44
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log copy
input archived log thread=1 sequence=146 RECID=252 STAMP=894988123
output file name=/opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf RECID=0 STAMP=0
channel ORA_DISK_1: archived log copy complete, elapsed time: 00:00:01
Finished backup at 2015-11-05 15:48:45
cataloged archived log
archived log file name=/opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf RECID=252 STAMP=894988217
datafile 1 switched to datafile copy
input datafile copy RECID=27 STAMP=894988217 file name=/u01/oradata2/TESTDB/datafile/o1_mf_system_4uqlgrpe_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=28 STAMP=894988217 file name=/u01/oradata2/TESTDB/datafile/o1_mf_sysaux_4vqlgrq7_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=29 STAMP=894988217 file name=/u01/oradata2/TESTDB/datafile/o1_mf_undotbs1_50qlgrqm_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=30 STAMP=894988217 file name=/u01/oradata2/TESTDB/datafile/o1_mf_users_51qlgrqp_.dbf
contents of Memory Script:
{
set until scn 2237265;
recover
clone database
delete archivelog
;
}
executing Memory Script
executing command: SET until clause
Starting recover at 2015-11-05 15:48:46
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=24 device type=DISK
starting media recovery
archived log for thread 1 with sequence 146 is already on disk as file /opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf
archived log file name=/opt/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_146_894101903.dbf thread=1 sequence=146
media recovery complete, elapsed time: 00:00:00
Finished recover at 2015-11-05 15:48:49
Oracle instance started
Total System Global Area 238034944 bytes
Fixed Size 2252016 bytes
Variable Size 180355856 bytes
Database Buffers 50331648 bytes
Redo Buffers 5095424 bytes
contents of Memory Script:
{
sql clone "alter system set db_name =
''TESTDB'' comment=
''Reset to original value by RMAN'' scope=spfile";
sql clone "alter system reset db_unique_name scope=spfile";
shutdown clone immediate;
startup clone nomount;
}
executing Memory Script
sql statement: alter system set db_name = ''TESTDB'' comment= ''Reset to original value by RMAN'' scope=spfile
sql statement: alter system reset db_unique_name scope=spfile
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 238034944 bytes
Fixed Size 2252016 bytes
Variable Size 180355856 bytes
Database Buffers 50331648 bytes
Redo Buffers 5095424 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "TESTDB" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 SIZE 50 M ,
GROUP 2 SIZE 50 M ,
GROUP 3 SIZE 50 M
DATAFILE
'/u01/oradata2/TESTDB/datafile/o1_mf_system_4uqlgrpe_.dbf'
CHARACTER SET ZHS16GBK
contents of Memory Script:
{
set newname for clone tempfile 1 to new;
switch clone tempfile all;
catalog clone datafilecopy "/u01/oradata2/TESTDB/datafile/o1_mf_sysaux_4vqlgrq7_.dbf",
"/u01/oradata2/TESTDB/datafile/o1_mf_undotbs1_50qlgrqm_.dbf",
"/u01/oradata2/TESTDB/datafile/o1_mf_users_51qlgrqp_.dbf";
switch clone datafile all;
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /u01/oradata2/TESTDB/datafile/o1_mf_temp_%u_.tmp in control file
cataloged datafile copy
datafile copy file name=/u01/oradata2/TESTDB/datafile/o1_mf_sysaux_4vqlgrq7_.dbf RECID=1 STAMP=894988259
cataloged datafile copy
datafile copy file name=/u01/oradata2/TESTDB/datafile/o1_mf_undotbs1_50qlgrqm_.dbf RECID=2 STAMP=894988259
cataloged datafile copy
datafile copy file name=/u01/oradata2/TESTDB/datafile/o1_mf_users_51qlgrqp_.dbf RECID=3 STAMP=894988259
datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=894988259 file name=/u01/oradata2/TESTDB/datafile/o1_mf_sysaux_4vqlgrq7_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=894988259 file name=/u01/oradata2/TESTDB/datafile/o1_mf_undotbs1_50qlgrqm_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=894988259 file name=/u01/oradata2/TESTDB/datafile/o1_mf_users_51qlgrqp_.dbf
Reenabling controlfile options for auxiliary database
Executing: alter database enable block change tracking
contents of Memory Script:
{
Alter clone database open resetlogs;
}
executing Memory Script
database opened
Finished Duplicate Db at 2015-11-05 15:49:38
RMAN>
5.打开辅助实例
登录到B机,
$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Nov 5 16:06:29 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
SQL> select status, instance_name from v$instance;
STATUS INSTANCE_NAME
------------ ----------------
OPEN testdb
SQL> select dbid, open_mode from v$database;
DBID OPEN_MODE
---------- --------------------
2678316385 READ WRITE
至此,成功duplicate数据库TESTDB。可以发现使用RMAN DUPLICATE复制的数据库DBID是不同的。