Bootstrap

Oracle Dataguard(主库为双节点集群)配置详解(5):将主库复制到备库并启动同步

Oracle Dataguard(主库为双节点集群)配置详解(5):将主库复制到备库并启动同步

一、使用 rman 连接主库和备库,并设置好主备关系

target——主库:auxiliary——备库。

在主库的节点1执行如下命令:

[oracle@rac01 ~]$ rman target sys/oracle@hisdb1 auxiliary sys/oracle@hisdbdg

Recovery Manager: Release 11.2.0.4.0 - Production on Sat Jan 11 11:45:08 2025

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: HISDB (DBID=2102569523)
connected to auxiliary database: HISDB (not mounted)

二、执行复制命令

该命令可以直接恢复数据文件、standby日志文件和控制文件。

RMAN> DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE;  

Starting Duplicate Db at 11-JAN-25
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=20 device type=DISK

contents of Memory Script:
{
   backup as copy reuse
   targetfile  '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwhis1' auxiliary format 
 '/usr/local/oracle/product/11.2.0/db_1/dbs/orapwhisdg'   ;
}
executing Memory Script

Starting backup at 11-JAN-25
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=1 instance=his1 device type=DISK
Finished backup at 11-JAN-25

contents of Memory Script:
{
   backup as copy current controlfile for standby auxiliary format  '/usr/local/oradata/hisdbdg/control01.ctl';
   restore clone controlfile to  '/usr/local/oracle/fast_recovery_area/hisdbdg/control02.ctl' from 
 '/usr/local/oradata/hisdbdg/control01.ctl';
}
executing Memory Script

Starting backup at 11-JAN-25
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_his1.f tag=TAG20250111T124752 RECID=3 STAMP=1190119677
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
Finished backup at 11-JAN-25

Starting restore at 11-JAN-25
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 11-JAN-25

contents of Memory Script:
{
   sql clone 'alter database mount standby database';
}
executing Memory Script

sql statement: alter database mount standby database

contents of Memory Script:
{
   set newname for tempfile  1 to 
 "/usr/local/oradata/hisdbdg/temp.263.1189953499";
   switch clone tempfile all;
   set newname for datafile  1 to 
 "/usr/local/oradata/hisdbdg/system.256.1189953387";
   set newname for datafile  2 to 
 "/usr/local/oradata/hisdbdg/sysaux.257.1189953387";
   set newname for datafile  3 to 
 "/usr/local/oradata/hisdbdg/undotbs1.258.1189953387";
   set newname for datafile  4 to 
 "/usr/local/oradata/hisdbdg/users.259.1189953387";
   set newname for datafile  5 to 
 "/usr/local/oradata/hisdbdg/undotbs2.264.1189953701";
   backup as copy reuse
   datafile  1 auxiliary format 
 "/usr/local/oradata/hisdbdg/system.256.1189953387"   datafile 
 2 auxiliary format 
 "/usr/local/oradata/hisdbdg/sysaux.257.1189953387"   datafile 
 3 auxiliary format 
 "/usr/local/oradata/hisdbdg/undotbs1.258.1189953387"   datafile 
 4 auxiliary format 
 "/usr/local/oradata/hisdbdg/users.259.1189953387"   datafile 
 5 auxiliary format 
 "/usr/local/oradata/hisdbdg/undotbs2.264.1189953701"   ;
   sql 'alter system archive log current';
}
executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to /usr/local/oradata/hisdbdg/temp.263.1189953499 in control file

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting backup at 11-JAN-25
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=+DATA/hisdb/datafile/system.256.1189953387
output file name=/usr/local/oradata/hisdbdg/system.256.1189953387 tag=TAG20250111T124809
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=+DATA/hisdb/datafile/sysaux.257.1189953387
output file name=/usr/local/oradata/hisdbdg/sysaux.257.1189953387 tag=TAG20250111T124809
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:05
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=+DATA/hisdb/datafile/undotbs1.258.1189953387
output file name=/usr/local/oradata/hisdbdg/undotbs1.258.1189953387 tag=TAG20250111T124809
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=+DATA/hisdb/datafile/undotbs2.264.1189953701
output file name=/usr/local/oradata/hisdbdg/undotbs2.264.1189953701 tag=TAG20250111T124809
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=+DATA/hisdb/datafile/users.259.1189953387
output file name=/usr/local/oradata/hisdbdg/users.259.1189953387 tag=TAG20250111T124809
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 11-JAN-25

sql statement: alter system archive log current

contents of Memory Script:
{
   switch clone datafile all;
}
executing Memory Script

datafile 1 switched to datafile copy
input datafile copy RECID=3 STAMP=1190119881 file name=/usr/local/oradata/hisdbdg/system.256.1189953387
datafile 2 switched to datafile copy
input datafile copy RECID=4 STAMP=1190119881 file name=/usr/local/oradata/hisdbdg/sysaux.257.1189953387
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=1190119881 file name=/usr/local/oradata/hisdbdg/undotbs1.258.1189953387
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=1190119881 file name=/usr/local/oradata/hisdbdg/users.259.1189953387
datafile 5 switched to datafile copy
input datafile copy RECID=7 STAMP=1190119881 file name=/usr/local/oradata/hisdbdg/undotbs2.264.1189953701
Finished Duplicate Db at 11-JAN-25

三、启动备库

1、查看备库的状态

执行完同步后,数据库状态会从 nomount --> mount

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
MOUNTED
2、正常启动备库
SQL> alter database open;

Database altered.
3、开启实时同步
SQL> alter database recover managed standby database using current logfile disconnect from session;

Database altered.
4、查看备库状态
SQL> select open_mode,log_mode,open_mode ,database_role from v$database;

OPEN_MODE	     LOG_MODE	  OPEN_MODE	       DATABASE_ROLE
-------------------- ------------ -------------------- ----------------
READ ONLY WITH APPLY ARCHIVELOG   READ ONLY WITH APPLY PHYSICAL STANDBY
5、查看备库进程
SQL> select process, status, thread#,sequence#, block#, blocks from v$managed_standby;

PROCESS   STATUS	  THREAD#  SEQUENCE#	 BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH	  CONNECTED		0	   0	      0 	 0
ARCH	  CONNECTED		0	   0	      0 	 0
ARCH	  CLOSING		2	  20	  10240       1912
ARCH	  CLOSING		1	  24	  32768        265
RFS	  IDLE			0	   0	      0 	 0
RFS	  IDLE			0	   0	      0 	 0
RFS	  WRITING		2	  21	    144 	 1
RFS	  WRITING		1	  25	    103 	 1
RFS	  IDLE			0	   0	      0 	 0
RFS	  IDLE			0	   0	      0 	 0
MRP0	  APPLYING_LOG		2	  20	   2418      12151

11 rows selected.
6、查看主库状态
SQL> select open_mode,log_mode,open_mode ,database_role from v$database;

OPEN_MODE	     LOG_MODE	  OPEN_MODE	       DATABASE_ROLE
-------------------- ------------ -------------------- ----------------
READ WRITE	     ARCHIVELOG   READ WRITE	       PRIMARY
7、查看主库进程
SQL> select process, status, thread#,sequence#, block#, blocks from v$managed_standby;

PROCESS   STATUS	  THREAD#  SEQUENCE#	 BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH	  CLOSING		1	  24	  32768        265
ARCH	  CLOSING		1	  23	  47104       1418
ARCH	  CONNECTED		0	   0	      0 	 0
ARCH	  CLOSING		1	  24	  32769        264
LNS	  WRITING		1	  25	    151 	 1

四、实时同步验证

1、在主库创建 t1 表插入以下数据

SQL> 
create table t1(id int primary key,name varchar2(20));
insert into t1 values(1,'mark');
insert into t1 values(2,'jack');
commit;

Table created.

SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
Commit complete.

2、在备库查询 t1 表中的数据

SQL> select * from t1;

	ID NAME
---------- --------------------
	 1 mark
	 2 jack
;