Oracle Dataguard(主库为单节点)配置详解(5):使用 rman 复制技术(DUPLICATE)同步主库到备库
目录
- Oracle Dataguard(主库为单节点)配置详解(5):使用 rman 复制技术(DUPLICATE)同步主库到备库
- 一、为备库配置监听
- 二、修改备库的配置文件
- 1、把主库的初始化参数文件和密码文件复制到备库相应目录下
- 2、修改备库的初始化参数文件
- 3、使用静态初始化参数文件生成 spfile 文件
- 三、创建参数文件中涉及到的目录并启动数据库到nomount
- 1、创建目录
- 2、启动备库的数据库到 nomount
- 四、在主库使用 rman 复制(DUPLICATE)同步主库到备库
- 1、使用 rman 连接主库和备库,并设置好主备关系(target-主:auxiliary-备)
- 2、同步主库数据到备库
- 五、启动备库
- 1、查看备库的状态
- 2、正常启动备库
- 3、开启实时同步
- 4、查看备库状态
- 5、查看备库进程
- 6、查看主库状态
- 7、查看主库进程
- 8、实时同步验证
一、为备库配置监听
修改文件listener.ora
,内容如下:
[oracle@oradg admin]$ cd $ORACLE_HOME/network/admin
[oracle@oradg admin]$ vi listener.ora
# listener.ora Network Configuration File: /usr/local/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = oradg)(PORT = 1521))
)
)
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC =
(GLOBAL_DBNAME = hisdb)
(ORACLE_HOME = /usr/local/oracle/product/11.2.0/db_1)
(SID_NAME = ora)
)
(SID_DESC=
(GLOBAL_DBNAME=hisdbdg)
(SID_NAME=oradg)
(ORACLE_HOME=/usr/local/oracle/product/11.2.0/db_1)
)
)
ADR_BASE_LISTENER = /usr/local/oracle
修改文件tnsnames.ora
,内容如下:
[oracle@oradg admin]$ vi tnsnames.ora
HISDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ora)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = hisdb)
)
)
HISDBDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oradg)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = hisdbdg)
)
)
查看备库的监听信息:
[oracle@oradg admin]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 06-JAN-2025 21:10:11
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 06-JAN-2025 21:09:17
Uptime 0 days 0 hr. 0 min. 55 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /usr/local/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File /usr/local/oracle/diag/tnslsnr/oradg/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oradg)(PORT=1521)))
Services Summary...
Service "hisdb" has 1 instance(s).
Instance "ora", status UNKNOWN, has 1 handler(s) for this service...
Service "hisdbdg" has 1 instance(s).
Instance "oradg", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
二、修改备库的配置文件
1、把主库的初始化参数文件和密码文件复制到备库相应目录下
(1)在主库上执行如下操作
[oracle@ora admin]$ cd $ORACLE_HOME/dbs
# 复制静态初始化参数文件到备库
[oracle@ora dbs]$ scp initora.ora oracle@oradg:$ORACLE_HOME/dbs/initoradg.ora
oracle@oradg's password:
initora.ora 100% 1384 1.4KB/s 00:00
# 复制密码文件到备库
[oracle@ora dbs]$ scp orapwora oracle@oradg:$ORACLE_HOME/dbs/orapworadg
oracle@oradg's password:
orapwora 100% 1536 1.5KB/s 00:00
(2)查看备库的初始化参数文件信息
[oracle@oradg admin]$ cd $ORACLE_HOME/dbs/
[oracle@oradg dbs]$ ll
总用量 12
-rw-r--r-- 1 oracle oinstall 2851 5月 15 2009 init.ora
-rw-r--r-- 1 oracle oinstall 1384 1月 6 21:12 initoradg.ora
-rw-r----- 1 oracle oinstall 1536 1月 6 21:13 orapworadg
2、修改备库的初始化参数文件
修改后的初始化参数文件内容如下:
ora.__db_cache_size=792723456
ora.__java_pool_size=4194304
ora.__large_pool_size=8388608
ora.__oracle_base='/usr/local/oracle'#ORACLE_BASE set from environment
ora.__pga_aggregate_target=499122176
ora.__sga_target=1073741824
ora.__shared_io_pool_size=0
ora.__shared_pool_size=255852544
ora.__streams_pool_size=0
*.audit_file_dest='/usr/local/oracle/admin/hisdbdg/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/usr/local/oradata/hisdbdg/control01.ctl','/usr/local/oracle/fast_recovery_area/hisdbdg/control02.ctl'
*.db_file_name_convert='/usr/local/oradata/hisdb/','/usr/local/oradata/hisdbdg/'
*.log_file_name_convert='/usr/local/oradata/hisdb/','/usr/local/oradata/hisdbdg/'
*.db_block_size=8192
*.db_domain=''
*.db_name='hisdb'
*.db_recovery_file_dest_size=4385144832
*.db_recovery_file_dest='/usr/local/oracle/fast_recovery_area'
*.db_unique_name='hisdbdg'
*.diagnostic_dest='/usr/local/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=oraXDB)'
*.fal_client='hisdb'
*.fal_server='hisdbdg'
*.log_archive_config='dg_config=(hisdb,hisdbdg)'
*.log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST valid_for=(all_logfiles,all_roles) db_unique_name=hisdbdg'
*.log_archive_dest_2='service=hisdb lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=hisdb'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.open_cursors=300
*.pga_aggregate_target=499122176
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=1073741824
*.standby_file_management='auto'
*.undo_tablespace='UNDOTBS1'
3、使用静态初始化参数文件生成 spfile 文件
[oracle@oradg dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Mon Jan 6 21:18:23 2025
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL> create spfile from pfile;
File created.
三、创建参数文件中涉及到的目录并启动数据库到nomount
1、创建目录
[oracle@oradg ~]$ mkdir -p /usr/local/oracle/fast_recovery_area
[oracle@oradg ~]$ mkdir -p /usr/local/oradata/hisdbdg
[oracle@oradg ~]$ mkdir -p /usr/local/oracle/admin/hisdbdg/adump
[oracle@oradg ~]$ mkdir -p /usr/local/oradata/hisdbdg
[oracle@oradg ~]$ mkdir -p /usr/local/oracle/fast_recovery_area/hisdbdg
2、启动备库的数据库到 nomount
SQL> startup nomount
ORACLE instance started.
Total System Global Area 1068937216 bytes
Fixed Size 2260088 bytes
Variable Size 331350920 bytes
Database Buffers 729808896 bytes
Redo Buffers 5517312 bytes
四、在主库使用 rman 复制(DUPLICATE)同步主库到备库
1、使用 rman 连接主库和备库,并设置好主备关系(target-主:auxiliary-备)
[oracle@ora dbs]$ rman target sys/oracle@hisdb auxiliary sys/oracle@hisdbdg
Recovery Manager: Release 11.2.0.4.0 - Production on Mon Jan 6 21:30:04 2025
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: HISDB (DBID=2101731232)
connected to auxiliary database: HISDB (not mounted)
2、同步主库数据到备库
RMAN> DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE;
Starting Duplicate Db at 06-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
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: SID=21 device type=DISK
allocated channel: ORA_AUX_DISK_3
channel ORA_AUX_DISK_3: SID=22 device type=DISK
allocated channel: ORA_AUX_DISK_4
channel ORA_AUX_DISK_4: SID=23 device type=DISK
allocated channel: ORA_AUX_DISK_5
channel ORA_AUX_DISK_5: SID=24 device type=DISK
allocated channel: ORA_AUX_DISK_6
channel ORA_AUX_DISK_6: SID=25 device type=DISK
contents of Memory Script:
{
backup as copy reuse
targetfile '/usr/local/oracle/product/11.2.0/db_1/dbs/orapwora' auxiliary format
'/usr/local/oracle/product/11.2.0/db_1/dbs/orapworadg' ;
}
executing Memory Script
Starting backup at 06-JAN-25
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=45 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=37 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=46 device type=DISK
allocated channel: ORA_DISK_4
channel ORA_DISK_4: SID=47 device type=DISK
allocated channel: ORA_DISK_5
channel ORA_DISK_5: SID=48 device type=DISK
allocated channel: ORA_DISK_6
channel ORA_DISK_6: SID=49 device type=DISK
Finished backup at 06-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 06-JAN-25
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4
using channel ORA_DISK_5
using channel ORA_DISK_6
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/usr/local/oracle/product/11.2.0/db_1/dbs/snapcf_ora.f tag=TAG20250106T213217 RECID=1 STAMP=1189719138
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 06-JAN-25
Starting restore at 06-JAN-25
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3
using channel ORA_AUX_DISK_4
using channel ORA_AUX_DISK_5
using channel ORA_AUX_DISK_6
channel ORA_AUX_DISK_2: skipped, AUTOBACKUP already found
channel ORA_AUX_DISK_3: skipped, AUTOBACKUP already found
channel ORA_AUX_DISK_4: skipped, AUTOBACKUP already found
channel ORA_AUX_DISK_5: skipped, AUTOBACKUP already found
channel ORA_AUX_DISK_6: skipped, AUTOBACKUP already found
channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 06-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/temp01.dbf";
switch clone tempfile all;
set newname for datafile 1 to
"/usr/local/oradata/hisdbdg/system01.dbf";
set newname for datafile 2 to
"/usr/local/oradata/hisdbdg/sysaux01.dbf";
set newname for datafile 3 to
"/usr/local/oradata/hisdbdg/undotbs01.dbf";
set newname for datafile 4 to
"/usr/local/oradata/hisdbdg/users01.dbf";
backup as copy reuse
datafile 1 auxiliary format
"/usr/local/oradata/hisdbdg/system01.dbf" datafile
2 auxiliary format
"/usr/local/oradata/hisdbdg/sysaux01.dbf" datafile
3 auxiliary format
"/usr/local/oradata/hisdbdg/undotbs01.dbf" datafile
4 auxiliary format
"/usr/local/oradata/hisdbdg/users01.dbf" ;
sql 'alter system archive log current';
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /usr/local/oradata/hisdbdg/temp01.dbf in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting backup at 06-JAN-25
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4
using channel ORA_DISK_5
using channel ORA_DISK_6
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/usr/local/oradata/hisdb/system01.dbf
channel ORA_DISK_2: starting datafile copy
input datafile file number=00002 name=/usr/local/oradata/hisdb/sysaux01.dbf
channel ORA_DISK_3: starting datafile copy
input datafile file number=00003 name=/usr/local/oradata/hisdb/undotbs01.dbf
channel ORA_DISK_4: starting datafile copy
input datafile file number=00004 name=/usr/local/oradata/hisdb/users01.dbf
output file name=/usr/local/oradata/hisdbdg/users01.dbf tag=TAG20250106T213234
channel ORA_DISK_4: datafile copy complete, elapsed time: 00:00:04
output file name=/usr/local/oradata/hisdbdg/undotbs01.dbf tag=TAG20250106T213234
channel ORA_DISK_3: datafile copy complete, elapsed time: 00:00:28
output file name=/usr/local/oradata/hisdbdg/sysaux01.dbf tag=TAG20250106T213234
channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:48
output file name=/usr/local/oradata/hisdbdg/system01.dbf tag=TAG20250106T213234
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:58
Finished backup at 06-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=1 STAMP=1189719214 file name=/usr/local/oradata/hisdbdg/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=2 STAMP=1189719214 file name=/usr/local/oradata/hisdbdg/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=3 STAMP=1189719214 file name=/usr/local/oradata/hisdbdg/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=4 STAMP=1189719214 file name=/usr/local/oradata/hisdbdg/users01.dbf
-----------------------------------------------------------------------
Finished Duplicate Db at 06-JAN-25
-- 退出 rman
RMAN> exit
Recovery Manager complete.
五、启动备库
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 CONNECTED 0 0 0 0
ARCH CLOSING 1 39 1 333
RFS IDLE 0 0 0 0
RFS IDLE 0 0 0 0
RFS IDLE 1 40 138 1
MRP0 APPLYING_LOG 1 40 138 102400
8 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 39 1 333
ARCH CLOSING 1 37 28672 799
ARCH OPENING 1 38 1 13535
ARCH CLOSING 1 39 1 333
LNS WRITING 1 40 218 1
8、实时同步验证
(1)在主库的 t01 表插入以下数据
SQL>
insert into t01 values(22,'mark');
1 row created.
insert into t01 values(33,'black');
1 row created.
commit;
Commit complete.
SQL> select * from t01;
ID NAME
---------- --------------------
1 jack
22 mark
33 black
(2)在被查询 t01 表中的数据
SQL> select * from t01;
ID NAME
---------- --------------------
1 jack
22 mark
33 black