Oracle-DataGuard-Rman-auxiliary搭建备库

1.环境介绍

百货–数据库服务器

实例名称 IP 账户 密码 db_name db_unique_name service_name
zbds-baihuo-DB01 172.16.35.141 zyadmin ** zbdsora zbdsora zbdsora
zbds-baihuo-DB02 172.16.35.140 zyadmin ** zbdsora zbdsora_std zbdsora_std

2.DG部署

2.1.建立主库zbdsora

DBCA建立主库,修改online redo 大小为500M

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
SQL> set linesize 160
set pagesize 2000
col GROUP# for 999
col status for a10
col TYPE for a10
col MEMBER for a15
col IS_RECOVERY_DEST_FILE for a10

select * from v$log;
select * from v$logfile;


alter database add logfile thread 1 group 1 '/alidata/oradata/zbdsora/redo01.log' size 500M;
alter database add logfile thread 1 group 2 '/alidata/oradata/zbdsora/redo02.log' size 500M;
alter database add logfile thread 1 group 3 '/alidata/oradata/zbdsora/redo03.log' size 500M;
alter database add logfile thread 1 group 4 '/alidata/oradata/zbdsora/redo04.log' size 500M;
alter database add logfile thread 1 group 5 '/alidata/oradata/zbdsora/redo05.log' size 500M;

alter database drop logfile group 1;
alter database drop logfile group 2;
alter database drop logfile group 3;

alter system switch logfile;
alter system checkpoint;
select group#,bytes,status from v$log;

2.2.主库开启归档

主库开启归档并设置强制日志 force logging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
--干净的关闭数据库
SQL> shutdown immediate
--以mount模式启动
SQL> startup mount
--切换到归档模式
SQL> alter database archivelog;
--变更归档路径
alter system set log_archive_dest_1='location=/alidata/arch';
--开启强制日志
SQL> alter database force logging;
--打开数据库
SQL> alter database open;
--查看归档
SQL> archive log list;
--查看是否为强制日志
SQL> select force_logging from v$database;

2.3.主库添加Standby Redo Log

1
2
3
4
5
6
7
8
9
10
11
--查看Redo和Standby Redo
SQL> select * from v$logfile;
--仅仅显示Online Redo,不显示Standby Redo
SQL> select * from v$log;
--新增一组大小为500M的Standby Redo,这里的group号不得与Online redo重复
SQL> alter database add standby logfile group 21 '/alidata/oradata/zbdsora/standby21.log' size 500M;
alter database add standby logfile group 22 '/alidata/oradata/zbdsora/standby22.log' size 500M;
alter database add standby logfile group 23 '/alidata/oradata/zbdsora/standby23.log' size 500M;
alter database add standby logfile group 24 '/alidata/oradata/zbdsora/standby24.log' size 500M;
alter database add standby logfile group 25 '/alidata/oradata/zbdsora/standby25.log' size 500M;
alter database add standby logfile group 26 '/alidata/oradata/zbdsora/standby26.log' size 500M;

2.4.从主库创建pfile文件

创建pfile文件, 默认路径为$ORACLE_HOME/dbs,此处为/alidata/oracle/product/11.2.0/db_1/dbs/,在sqlplus里执行以下命令

1
SQL> create pfile from spfile;

将主库的pfile复制到备库/alidata/oracle/product/11.2.0/db_1/dbs/下

1
2
3
4
5
6
7
cd /alidata/oracle/product/11.2.0/db_1/dbs/
scp -P 40022 initzbdsora.ora zyadmin@172.16.35.140:/software/dumpfile/
zyadmin/5Eb/5Al!7Uy@6Oh!

cp /software/dumpfile/initzbdsora.ora /alidata/oracle/product/11.2.0/db_1/dbs/

chown oracle. /alidata/oracle/product/11.2.0/db_1/dbs/initzbdsora.ora

2.5.设置主库初始化参数

编辑/alidata/oracle/product/11.2.0/db_1/dbs/initzbdsora.ora文件,追加

1
2
3
4
5
6
7
8
9
10
11
12
cat >> /alidata/oracle/product/11.2.0/db_1/dbs/initzbdsora.ora << "EOF"
*.db_unique_name='zbdsora'
*.fal_server='zbdsora_std'
*.log_archive_config='dg_config=(zbdsora,zbdsora_std)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=zbdsora'
*.log_archive_dest_2='service=zbdsora_std lgwr async valid_for=(online_logfile,primary_role) db_unique_name=zbdsora_std'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='/alidata/oradata/zbdsora','/alidata/oradata/zbdsora'
*.log_file_name_convert='/alidata/oradata/zbdsora','/alidata/oradata/zbdsora'
EOF

创建新的主库spfile文件,并重新启动主库

1
2
3
SQL> shutdown immediate
SQL> create spfile from pfile;
SQL> startup

2.6.设置备库初始化参数

编辑/alidata/oracle/product/11.2.0/db_1/dbs/initzbdsora.ora文件,修改备库初始化参数

1
2
3
4
5
6
7
8
9
10
11
12
cat >> /alidata/oracle/product/11.2.0/db_1/dbs/initzbdsora.ora << "EOF"
*.db_unique_name='zbdsora_std'
*.fal_server='zbdsora'
*.log_archive_config='dg_config=(zbdsora,zbdsora_std)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=zbdsora_std'
*.log_archive_dest_2='service=zbdsora_pri lgwr async valid_for=(online_logfile,primary_role) db_unique_name=zbdsora'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='/alidata/oradata/zbdsora','/alidata/oradata/zbdsora'
*.log_file_name_convert='/alidata/oradata/zbdsora','/alidata/oradata/zbdsora'
EOF

alter system set log_archive_dest_2=’service=zbdsora_pri lgwr async valid_for=(online_logfile,primary_role) db_unique_name=zbdsora’;

2.7.复制主库的密码文件到备库

将密码文件orapwzbdsora复制到备库的/alidata/oracle/product/11.2.0/db_1/dbs/下

1
2
3
4
5
6
7
8
9
scp -P 40022 /alidata/oracle/product/11.2.0/db_1/dbs/orapwzbdsora zyadmin@172.16.35.140:/software/dumpfile/
zyadmin/5Eb/5Al!7Uy@6Oh!

cd /alidata/oracle/product/11.2.0/db_1/dbs/


cp /software/dumpfile/orapwzbdsora /alidata/oracle/product/11.2.0/db_1/dbs/

chown oracle. /alidata/oracle/product/11.2.0/db_1/dbs/orapwzbdsora

2.8.创建备库相应的目录结构

使用oracle用户创建以下目录,避免权限问题

1
2
3
mkdir -p /alidata/oradata/zbdsora/
mkdir -p /alidata/oracle/admin/zbdsora/adump/
mkdir -p /alidata/oracle/fast_recovery_area/zbdsora/

2.9.配置主库和备库的监听###

使用图形界面配置,采用静态监听,注意一定要配置静态监听

vim /alidata/oracle/product/11.2.0/db_1/network/admin/listener.ora

1
netmgr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
手动配置:
主库:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = zbdsora_pri)
(ORACLE_HOME = /alidata/oracle/product/11.2.0/db_1)
(SID_NAME = zbdsora)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.141)(PORT = 1521))
)
)

ADR_BASE_LISTENER = /alidata/oracle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
备库:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = zbdsora_std)
(ORACLE_HOME = /alidata/oracle/product/11.2.0/db_1)
(SID_NAME = zbdsora)
)
)

LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.140)(PORT = 1521))
)
)

ADR_BASE_LISTENER = /alidata/oracle

2.10.配置主库和备库的网络服务名

使用图形界面配置,在主备库上均需配置zbdsora、zbdsora_std两个服务名

1
netmgr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
zbdsora_pri =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.141)(PORT = 1521))
)
(CONNECT_DATA =
(sid = zbdsora)
)
)

zbdsora_std =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.140)(PORT = 1521))
)
(CONNECT_DATA =
(sid = zbdsora)
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ZBDSORA_PRI =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.141)(PORT = 1521))
)
(CONNECT_DATA =
(SID = zbdsora)
)
)

ZBDSORA_STD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.35.140)(PORT = 1521))
)
(CONNECT_DATA =
(SID = zbdsora)
)
)

2.11.创建备库并启动

创建备库的spfile文件,启动备库到nomount模式

1
2
SQL> create spfile from pfile;
SQL> startup nomount

2.12.RMAN复制主库到备库

首先RMAN连接到目标数据库和辅助数据库

1
rman target sys/oracle@zbdsora_pri auxiliary sys/oracle@zbdsora_std

使用RMAN的duplicate命令进行复制,两边目录结构相同,需要添加nofilenamecheck参数

1
RMAN> duplicate target database for standby from active database nofilenamecheck;

复制成功后,备库自动被加载为mount模式,进入sqlplus查看

1
SQL> select status from v$instance;

2.13.在备库开启实时日志应用

1
SQL> alter database recover managed standby database using current logfile disconnect from session;

2.14.主备库角色状态查询

1
2
3
SQL> select switchover_status,database_role from v$database;
--主库显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭活动的会话再检查
--备库显示:NOT ALLOWED/PHYSICAL STANDBY

3.测试DG

3.1.执行日志切换测试

在主库端切换归档,在备库检查是否也发生了切换

主库上执行日志切换

1
2
3
SQL> archive log list;
SQL> alter system switch logfile;
archive log list;

备库上查看,日志的sequence号也跟着变了

1
SQL> archive log list;

3.2.查看备库启动的DG进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SQL> select process,client_process,sequence#,status from v$managed_standby;
PROCESS CLIENT_P SEQUENCE# STATUS

------

ARCH ARCH 23 CLOSING
ARCH ARCH 0 CONNECTED //归档进程
ARCH ARCH 21 CLOSING
ARCH ARCH 0 CONNECTED
RFS ARCH 0 IDLE
RFS UNKNOWN 0 IDLE
RFS LGWR 24 IDLE //归档传输进程
RFS UNKNOWN 0 IDLE
MRP0 N/A 24 APPLYING_LOG //日志应用进程

9 rows selected.

3.3.查看数据库的保护模式

1
2
3
4
5
6
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE PROTECTION_LEVEL OPEN_MODE

------

PRIMARY MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE READ WRITE

3.4.查看DG的日志信息

1
SQL> select * from v$dataguard_status;

3.5.Open Read Only Standby数据库

以只读方式打开备库,并开启实时日志应用

1
2
3
4
5
SQL> shutdown immediate
SQL> startup
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
SQL> select process,client_process,sequence#,status from v$managed_standby;
SQL> alter database recover managed standby database using current logfile disconnect from session;

3.6.解锁scott用户并添加数据,验证是否同步

在主库解锁scott用户并创建测试表,插入10000行数据。

操作如下

1
2
3
4
5
6
7
8
9
10
11
12
13
SQL> set line 200
SQL> select username,default_tablespace,account_status from dba_users where username='SCOTT';
SQL> alter user scott account unlock;
SQL> conn scott/tiger;
SQL> show user
SQL> create table test001 (id number(10),name varchar2(20));
SQL> begin
for i in 1..10000 loop
insert into test001 values (1,'ww');
end loop;
end;
/
SQL> commit;

standby端查询scott用户是否解锁,以及test001表是否创建并且插入了10000行数据

1
2
3
SQL> conn scott/tiger;
SQL> select * from tab;
SQL> select count(*) from scott.test001;

4.DG三种模式

4.1.最大性能模式max performance-默认

这种保护模式(默认)提供了可能的最高级别的数据保护,而不影响主数据库的性能。这是通过允许事务在恢复该事务所需重做数据在写到本地联机重做日志后立即提交而实现的。主数据库的重做数据流也写到至少一个备数据库,但是那个重做流相对于创建重做数据的事务是异步写的。

当所用的网络连接有足够的带宽,这种模式提供了近似于最大可用性模式的数据保护级别,并且对主数据库性能的影响最小。

4.2.最大可用性模式max availability

这种保护模式提供了可能的最高级别的数据保护,而不用与主数据库的可用性相折衷。与最大保护模式相同,在恢复事务所需的重做写到本地联机重做日志和至少一个事务一致性备数据库上的备重做日志之前,事务将不会提交。与最大保护模式不同的是,如果故障导致主数据库无法写重做流到异地备重做日志时,主数据库不会关闭。替代地,主数据库以最大性能模式运行直到故障消除,并且解决所有重做日志文件中的中断。当所有中断解决之后,主数据库自动继续以最大可用性模式运行。

这种模式确保如果主数据库故障,但是只有当第二次故障没有阻止完整的重做数据集从主数据库发送到至少一个备数据库时,不发生数据丢失。

4.3.最大保护模式max protection

这种保护模式确保如果主数据库故障不会发生数据丢失。要提供这种级别的保护,恢复每个事务所需的重做数据必须在事务提交之前同时写到本地联机重做日志和至少一个备数据库上的备重做日志。要确保不发生数据丢失,如果故障导致主数据库无法写重做流到至少一个事务一致性备数据库的备重做日志时,主数据库会关闭。

4.4.查询当前模式

1
SQL> select protection_mode,protection_level from v$database;

5.DG切换测试

5.1.DG switchover 切换测试

db11-zbdsora:主库——>备库

db12-zbdsora:备库——>主库

主备库角色状态查询

1
2
3
SQL> select switchover_status,database_role,open_mode from v$database;
--db11-zbdsora显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭会话再检查
--db12-zbdsora显示:NOT ALLOWED/PHYSICAL STANDBY

主-zbdsora切换到备库

1
2
3
4
5
SQL> alter database commit to switchover to physical standby;
SQL> alter database commit to switchover to physical standby with session shutdown;
--如果状态显示SESSION ACTIVE,在切换的时候可以指定with session shutdown 子句强制关闭活动的会话。
SQL> shutdown immediate
SQL> startup mount

从-zbdsora切换到主库

1
2
SQL> alter database commit to switchover to primary;
SQL> alter database open;

db11-zbdsora执行APPLY LOG命令

1
2
3
4
5
6
7
--启用mount状态下的APPLY LOG
SQL> alter database recover managed standby database disconnect from session;
--启用open状态(READ ONLY WITH APPLY)下的APPLY LOG
SQL> alter database recover managed standby database cancel;
SQL> alter database open;
SQL> alter database recover managed standby database disconnect from session;
SQL> select switchover_status,database_role,open_mode from v$database;

5.2.DG failover 切换测试

db11-zbdsora:主库——>崩溃

db12-zbdsora:备库——>主库

主备库角色状态查询

1
SQL> select switchover_status,database_role,open_mode from v$database;

db11-zbdsora通过shutdown abort方式人工模拟主库崩溃,直接关闭

1
2
3
4
SQL> select open_mode from v$database;
SQL> shutdown abort
SQL> startup mount
SQL> alter system flush redo to 'zbdsora_std';

db12-zbdsora执行如下操作切换为主库

1
2
3
4
5
6
7
SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;
--如果没有发现明显的gap现象,说明此次的failover不会有数据损失情况。在备库,要进行关闭apply和结束应用动作。
SQL> alter database recover managed standby database cancel;
SQL> alter database recover managed standby database finish;
SQL> alter database commit to switchover to primary;
SQL> alter database open;
SQL> select open_mode, switchover_status from v$database;

5.3.DG failover后重建DG

db11-zbdsora:崩溃——>备库

db12-zbdsora:主库——>主库(保持主库状态不变)

db12-zbdsora主库角色状态查询

1
SQL> select switchover_status,database_role,open_mode from v$database;

db12-zbdsora主库创建备库控制文件

1
mkdir -p /u01/bak/

SQL> alter database create standby controlfile as ‘/u01/bak/control01.ctl’;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
1
将备库控制文件拷贝至db11
scp ezdb12:/u01/bak/control01.ctl /alidata/oradata/zbdsora/control01.ctl
cp /alidata/oradata/zbdsora/control01.ctl /u01/app/oracle/fast_recovery_area/zbdsora/
mv /u01/app/oracle/fast_recovery_area/zbdsora/control01.ctl /u01/app/oracle/fast_recovery_area/zbdsora/control02.ctl
1
2
3
恢复对应数据文件至db11,并启动db11-zbdsora到mount状态,应用APPLY模式

SQL> startup mount;
SQL> alter database recover managed standby database disconnect from session;
1
2
启动db11-zbdsora至OPEN状态,并应用APPLY REDO模式

SQL> alter database recover managed standby database cancel;
SQL> alter database open;
SQL> alter database recover managed standby database disconnect from session;
1
2
3
6.开启和关闭DataGuard的流程
6.1.关闭
关闭主库

SQL> shutdown immediate
1
关闭主库监听

lsnrctl stop
1
查询备库是否正在执行重做应用或实时应用。如果MRP0或MRP进程存在,则备库正在应用重做。

SQL> select process, status from v$managed_standby;
1
如果重做应用程序正在运行,停止备库的Redo日志的应用

SQL> alter database recover managed standby database cancel;
1
关闭备库

SQL> shutdown immediate
1
关闭备库监听

lsnrctl stop
1
6.2.开启
启动备库监听

lsnrctl start
1
启动备库到mount状态

SQL> startup mount;
1
开启备库的Redo日志应用

SQL> alter database recover managed standby database using current logfile disconnect from session;
1
启动主库监听

lsnrctl start
1
启动主库

SQL> startup;
1
检查日志情况的SQL语句

select al.thrd "Thread", almax "Last Seq Received", lhmax "Last Seq Applied"
from (select thread# thrd, max(sequence#) almax
from v$archived_log
where resetlogs_change#=(select resetlogs_change# from v$database)
group by thread#) al,
(select thread# thrd, max(sequence#) lhmax
from v$log_history
where first_time=(select max(first_time) from v$log_history)
group by thread#) lh
where al.thrd = lh.thrd;