转 DataGuard环境搭建 (一主一备一级联)
###sample 1 数据库全备的脚本
https://blog.csdn.net/gumengkai/article/details/53645339
cat rman_full2.sh
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
source /dd/dd/app/opdd/.bash_profile
rman target / log=/tmp/backupall_rman2.log<<EOF
run {
allocate channel ch1 device type DISK;
allocate channel ch2 device type DISK;
allocate channel ch3 device type DISK;
allocate channel ch4 device type DISK;
allocate channel ch5 device type DISK;
allocate channel ch6 device type DISK;
allocate channel ch7 device type DISK;
allocate channel ch8 device type DISK;
backup database format '/dba_bak/dd/full2/bk_%s_%p_%t.bak';
sql 'alter system archive log current';
BACKUP FORMAT '/dba_bak/dd/full2/al_%s_%p_%t.arc' ARCHIVELOG ALL NOT BACKED UP 1 TIMES;
release channel ch1;
release channel ch2;
release channel ch3;
release channel ch4;
release channel ch5;
release channel ch6;
release channel ch7;
release channel ch8;
}
EOF
##sample 3 使用dupliacte 搭建dg库,这种方式搭建dg 库步骤比较清晰,也方便懂,
###sample 3.2 上面介绍的是rman 搭建dg, 下文中写的是11g到19c 升级,借助TTS
http://blog.itpub.net/30130773/viewspace-2116985/
1.--------- primary_role / standby_role/ all_roles online_logfile/ standby_logfile/ all_logfiles
--------- 这几个参数的含义注意理解
DG搭建(一主一备一级联)
先说明一下dataguard一主一备一级联,意思是主库将日志传输给备库,然后备库在将日志传输给级联库,主库和级联库其实没有任何关系。另外,关于数据同步问题,后面也做了验证,主库上的操作一般情况下是可以实时同步到备库的,但是级联库必须等备库归档时,才能同步。如果主库切换日志,那么这时级联库也能及时同步。
节点 |
网络ip地址 |
数据库名 |
unique name |
数据库实例名 |
数据文件位置 |
zyx.test.com(主库) |
192.168.11.111 |
orcl |
orcl |
test |
/u01/app/oracle/oradata/orcl/ |
orcl.test.com(备库) |
192.168.11.22 |
orcl |
orclps |
orclps |
/u01/app/oracle/oradata/orcl/ |
dg2.orcl.com(级联库) |
192.168.11.23 |
orcl |
orclstd |
orclstd |
/u01/app/oracle/oradata/orcl/ |
1.主库设置
1.1 开归档
sys@ORCL> shutdown immediate
sys@ORCL> startup mount
sys@ORCL> alter database archivelog;
sys@ORCL> alter database open;
sys@ORCL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 4
Next log sequence to archive 6
Current log sequence 6
sys@ORCL> alter database force logging;
Database altered.
1.2 参数设置
sys@ORCL>alter system set log_archive_config='dg_config=(orcl,orclps)';
sys@ORCL>alter system set log_archive_dest_2='service=orclps sync affirm net_timeout=10 valid_for=(online_logfile,primary_role) db_unique_name=orclps';
#####下面参数是当主库切换为备库时需要的,这里先不设置
alter system set fal_server=orclps;
alter system set fal_client=orcl;
alter system set standby_file_management=auto;
sys@ORCL> create pfile='/home/oracle/pfile.ora' from spfile;
1.3 配置TNS
[oracle@zyx ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.111)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.test.com)
)
)
ORCLPS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.22)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCLPS)
)
)
2.备库orclps配置
----数据库软件安装好,数据库不用创建
2.1 环境变量
[oracle@orcl ~]$ vim .bash_profile
export ORACLE_SID=orclps
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export TNS_ADMIN=$ORACLE_HOME/network/admin
export ORACLE_UNQNAME=orclps
export ORACLE_HOSTNAME=orcl.test.com
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export NLS_LANG=american_america.ZHS16GBK
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
export EDITOR=vi
export LANG=C
umask 022
[oracle@orcl ~]$ . .bash_profile
2.2 创建必要目录
mkdir -p $ORACLE_BASE/fast_recovery_area/orcl
mkdir -p $ORACLE_BASE/admin/orcl/adump
mkdir -p $ORACLE_BASE/admin/orcl/dpdump
mkdir -p $ORACLE_BASE/admin/orcl/pfile
mkdir -p $ORACLE_BASE/oradata/orcl
2.3 静态监听
[oracle@orcl ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.22)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
SID_LIST_listener=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=orclps)
(SID_NAME=orclps)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1)))
[oracle@orcl ~]$ lsnrctl start
2.4 配置TNS
[oracle@orcl ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.111)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.test.com)
)
)
ORCLPS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.22)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCLPS)
)
)
2.5 参数文件设置
[oracle@zyx ~]$ scp pfile.ora 192.168.11.22:/home/oracle
idle>ho vim /home/oracle/pfile.ora
----可以删除的参数,或修改为下面格式(该参数是指当前数据库为主库时,传输在线日志给orcl)
*.log_archive_dest_2='service=orcl sync affirm net_timeout=10 valid_for=(online_logfile,primary_role) db_unique_name=orcl'
----追加参数
*.db_unique_name='orclps'
*.fal_client='orclps'
*.fal_server='orcl'
*.standby_file_management=auto
----创建spfile,启动到nomount
idle>create spfile from pfile='/home/oracle/pfile.ora';
idle>startup nomount
2.6 密钥文件创建
[oracle@orcl ~]$ cd $ORACLE_HOME/dbs
[oracle@orcl dbs]$ orapwd file=orapworclps password=sys
3.duplicate到备库
3.1 登录测试
[oracle@dg2 ~]$ tnsping orclps
[oracle@dg2 ~]$ tnsping orcl
[oracle@dg2 ~]$ sqlplus sys/sys@orclps as sysdba
[oracle@dg2 dbs]$ sqlplus sys/sys@orcl as sysdba
3.2 duplicate复制数据库到orclps
[oracle@orcl ~]$ rman target sys/sys@orcl auxiliary sys/sys@orclps
Recovery Manager: Release 11.2.0.4.0 - Production on Mon Apr 25 18:30:21 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1437652505)
connected to auxiliary database: ORCL (not mounted)
RMAN>
RMAN> duplicate target database for standby from active database dorecover nofilenamecheck;
--------如果复制数据库时,备库的路径和原库一致,就需要加nofilenamecheck,不然会报错
3.3 备库orclps配置
----查看当前状态
idle>select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
orclps MOUNTED
----如果需要手动启动备用数据库:
---- startup nomount
---- alter database mount standby database;
----创建srl日志(比主库redo多一组,大小一样)
idle>alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl01.log' size 50m;
idle>alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl02.log' size 50m;
idle>alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl03.log' size 50m;
idle>alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl04.log' size 50m;
----应用日志,开启redoapply
idle> alter database recover managed standby database using current logfile disconnect;
---------取消日志应用 recover managed standby database cancel;
# 主库添加srl日志,转为备库时需要
# alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl01.log' size 50m;
# alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl02.log' size 50m;
# alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl03.log' size 50m;
# alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl04.log' size 50m;
4.查看主/备应用日志情况
4.1主库切换日志
sys@ORCL>alter system switch logfile;
4.2备库出现新归档
----备库orclps出现新的归档
idle>select sequence#, applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------
7 YES
8 YES
9 IN-MEMORY
----备库orclps传输模式
idle> select protection_mode, protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE
----主库上最大性能改为最大可用
sys@ORCL>alter database set standby database to maximize availability;
sys@ORCL>show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_2 string service=orclps sync affirm net
_timeout=10 valid_for=(online_
logfile,primary_role) db_uniqu
e_name=orclps
sys@ORCL>select protection_mode,protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM AVAILABILITY MAXIMUM AVAILABILITY
4.3主库上查看备库应用情况(可以把name字段也加上)
sys@ORCL>select sequence#, applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------
6 NO
7 NO
8 NO
8 YES
9 YES
9 NO
10 YES
10 NO
11 YES
11 NO
12 YES
12 NO
13 YES
13 NO
14 NO
14 NO
16 rows selected.
------上面applied应用:NO的代表本地归档,是不需要应用的,YES的代表备库传输后已经应用。orclps备库是从8号归档开始应用
4.4 查看备库数据文件存放位置
idle>select name from v$controlfile;
idle>select name from v$datafile;
idle>select name from v$tempfile;
idle>select group#,member,type from v$logfile;
GROUP# MEMBER TYPE
---------- -------------------------------------------------------------------------------- ----------
3 /u01/app/oracle/fast_recovery_area/ORCLPS/onlinelog/o1_mf_3_ckvxlzto_.log ONLINE
2 /u01/app/oracle/fast_recovery_area/ORCLPS/onlinelog/o1_mf_2_ckvxlydr_.log ONLINE
1 /u01/app/oracle/fast_recovery_area/ORCLPS/onlinelog/o1_mf_1_ckvxlwvd_.log ONLINE
4 /u01/app/oracle/oradata/orcl/srl01.log STANDBY
5 /u01/app/oracle/oradata/orcl/srl02.log STANDBY
6 /u01/app/oracle/oradata/orcl/srl03.log STANDBY
7 /u01/app/oracle/oradata/orcl/srl04.log STANDBY
7 rows selected.
------后续转为主库后,redo log可以自己调整一下,上面是duplicate主库到备库时,自动生成的redo log
5.级联配置之备库设置
5.1 备库开启ADG模式
------备库不是一定要开启ADG,备库在mount下也是可以的完成级联库搭建的
idle>alter database recover managed standby database cancel;
idle>alter database open;
sys@ORCL>alter database recover managed standby database using current logfile disconnect;
sys@ORCL>select sequence#, applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------
12 YES
13 YES
14 YES
8 rows selected.
sys@ORCL>select database_role,switchover_status,db_unique_name from v$database;
DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
---------------- -------------------- ------------------------------
PHYSICAL STANDBY NOT ALLOWED orclps
5.2 备库参数设置
sys@ORCL>select log_mode,force_logging from v$database;
LOG_MODE FOR
------------ ---
ARCHIVELOG YES
sys@ORCL>alter system set log_archive_config='dg_config=(orcl,orclps,orclstd)';
sys@ORCL>alter system set log_archive_dest_3='service=orclstd sync affirm net_timeout=10 valid_for=(standby_logfile,standby_role) db_unique_name=orclstd';
--------- primary_role / standby_role/ all_roles online_logfile/ standby_logfile/ all_logfiles
--------- 这几个参数的含义注意理解
sys@ORCL>create pfile='/home/oracle/orclstd.ora' from spfile;
5.3 备库TNS配置追加
[oracle@orcl ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
ORCLSTD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.23)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orclstd)
)
)
6.级联库orclstd配置
6.1 环境变量
[oracle@dg2 ~]$ vim .bash_profile
export ORACLE_SID=orclstd
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export TNS_ADMIN=$ORACLE_HOME/network/admin
export ORACLE_UNQNAME=orclstd
export ORACLE_HOSTNAME=dg2.orcl.com
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export NLS_LANG=american_america.ZHS16GBK
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
export EDITOR=vi
export LANG=C
umask 022
[oracle@orcl ~]$ . .bash_profile
6.2 创建必要目录
mkdir -p $ORACLE_BASE/fast_recovery_area/orcl
mkdir -p $ORACLE_BASE/admin/orcl/adump
mkdir -p $ORACLE_BASE/admin/orcl/dpdump
mkdir -p $ORACLE_BASE/admin/orcl/pfile
mkdir -p $ORACLE_BASE/oradata/orcl
6.3 静态监听
[oracle@dg2 ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.23)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
SID_LIST_listener=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=orclstd)
(SID_NAME=orclstd)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1)))
[oracle@dg2 ~]$ lsnrctl start
6.4 配置TNS
[oracle@dg2 ~]$ vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
ORCLPS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.22)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCLPS)
)
)
ORCLSTD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.23)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orclstd)
)
)
6.5 参数文件设置
[oracle@orcl ~]$ scp orclstd.ora 192.168.11.23:/home/oracle/
----修改参数
SQL> !vim /home/oracle/orclstd.ora
*.db_name='orcl'
*.db_unique_name='orclstd'
*.fal_client='orclstd'
*.fal_server='orclps'
----暂时删除参数
*.log_archive_dest_3='service=orclstd sync affirm net_timeout=10 valid_for=(standby_logfile,standby_role) db_unique_name=orclstd'
----创建spfile,启动到nomount
idle>create spfile from pfile='/home/oracle/orclstd.ora';
idle>startup nomount
6.6 密钥文件创建
[oracle@dg2 ~]$cd $ORACLE_HOME/dbs
[oracle@dg2 dbs]$ orapwd file=orapworclstd password=sys
7.duplicate到级联库
7.1 登录测试
[oracle@dg2 ~]$ tnsping orclps
[oracle@dg2 ~]$ tnsping orclstd
[oracle@dg2 ~]$ sqlplus sys/sys@orclps as sysdba
[oracle@dg2 dbs]$ sqlplus sys/sys@orclstd as sysdba
7.2 duplicate复制数据库到orclstd
[oracle@orcl ~]$ rman target sys/sys@orclps auxiliary sys/sys@orclstd
Recovery Manager: Release 11.2.0.4.0 - Production on Mon Apr 25 19:37:45 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1437652505)
connected to auxiliary database: ORCL (not mounted)
RMAN>
RMAN> duplicate target database for standby from active database dorecover nofilenamecheck;
--------如果复制数据库时,备库的路径和原库一致,就需要加nofilenamecheck,不然会报错
7.3 级联库orclstd调整srl日志
----查看当前状态
SQL> select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
orclstd MOUNTED
----如果需要手动启动备用数据库:
---- startup nomount
---- alter database mount standby database;
----查看是否有srl日志
set linesize 200
set pagesize 999
col member for a80
select group#,type,member from v$logfile;
GROUP# TYPE MEMBER
---------- ------- --------------------------------------------------------------------------------
3 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_3_cl7h802v_.log
2 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_2_cl7h7z1l_.log
1 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_1_cl7h7y17_.log
4 STANDBY /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_4_cl7h8102_.log
5 STANDBY /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_5_cl7h8291_.log
6 STANDBY /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_6_cl7h83dn_.log
7 STANDBY /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_7_cl7h84r2_.log
7 rows selected.
------有redo和srl日志,不用再添加,当然这些日志都可以自己再手动调整位置
------redo日志需要转为主库才能调整,现在先调整srl日志
SQL> alter database drop standby logfile group 4;
SQL> alter database drop standby logfile group 5;
SQL> alter database drop standby logfile group 6;
SQL> alter database drop standby logfile group 7;
SQL> alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl01.log' size 50m;
SQL> alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl02.log' size 50m;
SQL> alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl03.log' size 50m;
SQL> alter database add standby logfile '/u01/app/oracle/oradata/orcl/srl04.log' size 50m;
SQL> select group#,type,member from v$logfile;
GROUP# TYPE MEMBER
---------- ------- --------------------------------------------------------------------------------
3 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_3_cl7hf6kq_.log
2 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_2_cl7hf4mo_.log
1 ONLINE /u01/app/oracle/fast_recovery_area/ORCLSTD/onlinelog/o1_mf_1_cl7hf33q_.log
4 STANDBY /u01/app/oracle/oradata/orcl/srl01.log
5 STANDBY /u01/app/oracle/oradata/orcl/srl02.log
6 STANDBY /u01/app/oracle/oradata/orcl/srl03.log
7 STANDBY /u01/app/oracle/oradata/orcl/srl04.log
7 rows selected.
7.4开启级联库redoapply
SQL> alter database recover managed standby database using current logfile disconnect;
---------取消日志应用 recover managed standby database cancel;
8.查看备库/级联库应用日志情况
8.1 级联库日志应用情况
----主库切换日志
sys@ORCL>alter system switch logfile;
----级联库出现新日志
SQL> select sequence#, applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------
15 IN-MEMORY
SQL> select protection_mode, protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM AVAILABILITY MAXIMUM AVAILABILITY
----备库orclps日志应用情况
sys@ORCL>select sequence#, applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------
12 YES
13 YES
14 YES
15 YES
15 YES
10 rows selected.
8.2 主库上创建表,级联库查看
----先开启级联库ADG
SQL> alter database recover managed standby database cancel;
SQL> alter database open;
SQL> alter database recover managed standby database using current logfile disconnect;
----主库orcl上创建表,并插入数据
sys@ORCL>create table shall(shall int);
begin
for i in 1..100000 loop
insert into shall values(i);
end loop;
commit;
end;
/
PL/SQL procedure successfully completed.
----备库orclps能实时查询到数据
sys@ORCL>select count(*) from shall;
COUNT(*)
----------
100000
----级联库orclstd不能查询到数据
SQL> select count(*) from shall;
select count(*) from shall
*
ERROR at line 1:
ORA-00942: table or view does not exist
----主库orcl切换日志
sys@ORCL>alter system switch logfile;
----此时级联库orclstd可以查询到数据
SQL> select count(*) from shall;
COUNT(*)
----------
0
SQL> select count(*) from shall;
COUNT(*)
----------
100000
----------备库是实时数据,而级联库需要等备库归档后才能同步
2.备份删除 策略
http://www.laoxiong.net/oracle-11g-data-guard-archived-log-managemen.html
幸运的是,在11g环境里面,上述的几点很容易就满足,那就是只需要做到以下几点。
- 使用快速恢复区(fast recovery area),在10g版本的文档中称为闪回恢复区(flash recovery area),老实说,一直不太明白为什么取名叫闪回恢复区,难道是因为10g有了数据库闪回功能?在RAC中,毫无疑问快速恢复区最好是置放在ASM上。
- 为快速恢复区指定合适的空间。首先我们需要预估一个合理的归档保留时间长。比如由于备份系统问题或Data Guard备库问题、维护等,需要归档保留的时间长度。假设是24小时,再评估一下在归档量最大的24小时之内,会有多少量的归档?一般来说是在批量数据处理的时候归档量最大,假设这24小时之内归档最大为200G。注意对于RAC来说是所有节点在这24小时的归档量之和。最后为快速恢复区指定需要的空间量,比通过参数db_recovery_file_dest_size指定快速恢复区的大小。这里同样假设快速恢复区们存放归档日志。
- 在备库上指定快速恢复区以及为快速恢复区指定合适的大小,在备库上指定快速恢复区的大小主要考虑的是:切换成为主库后归档日志容量;如果主库归档容量压力大,备库能否存储更多的归档日志以便可以通过备库来备份归档日志。
- 对主库和备份使用RMAN配置归档删除策略:CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
完成了上述几个步骤,那么归档管理的要求基本上就达到了。通过这样的设置,实现的效果如下:
- 归档日志如果没有应用到备库,那么在RMAN中使用backup .... delete inputs all和delete archivelog all不会将归档日志删除。但但是请注意如果是使用delete force命令则会删除掉归档,不管归档有没有被应用到备库。
- 如果归档日志已经应用到了备库,那么在RMAN中使用backup .... delete inputs all和delete archivelog all可以删除归档日志,在正常情况下,由于归档日志可能很快应用到Data Guard,所以在RMAN备份之后可以正常删除归档日志。RMAN也不需要使用特别的备份脚本,也不必担心人为不小心使用。delete archivelog all命令删除了归档。
- 备库的归档日志存储到快速恢复区中,备库的快速恢复区空间紧张时,会自动删除已经应用过的较早的归档日志以释放空间,这样便可以实现备库的归档日志完全自动管理。
- 如果由于备份异常或Data Guard异常,在快速恢复区空间紧张时,Oracle在切换日志时,会自动删除掉已经应用过的归档日志,以释放空间。但是如果归档日志没有应用到Data Guard,那么归档日志不会被删除。这种情况下,快速恢复区的归档可能会增加到空间耗尽,最后就会出现数据库不能归档,数据库挂起的问题。
Two queries you can run to look at space usage in the FRA are:
select * from V$FLASH_RECOVERY_AREA_USAGE;
SELECT substr(name, 1, 30) name
, space_limit/(1073741824) AS Quota_GB
, space_used/(1073741824) AS Used_GB
, space_reclaimable/(1073741824) AS Reclaimable_GB
, number_of_files AS files
FROM V$RECOVERY_FILE_DEST ;
#########
CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDB;
Files being deleted in the flash recovery area, messages in the alert log Deleted Oracle managed file <filename> (文档 ID 1369341.1) ,With Oracle 11.2 and onwards, Oracle will start to purge the files in the FRA when the SPACE_USED reaches 80% of SPACE_LIMIT.
####3
##############1
How is the space pressure managed in the Flash Recovery Area - An Example. (文档 ID 315098.1)
To make space reclaimable we have the following options:
1) The space occupied by archivelogs created in the Flash Recovery Area
would become reclaimable in 2 circumstances:
A) If the free space becomes less then 15% in the Flash Recovery Area then all
the archivelogs in the Flash Recovery Area which are not needed for recovery
by the current backups in the FRA will become obsolete and the space occupied
will be shown in the SPACE RECLAIMABLE column of V$RECOVERY_FILE_DEST.
RMAN> report obsolete;
B) If the archivelogs have been backed up (either to the Flash Recovery Area itself
or to another destination) and the archivelog not deleted. Then the space occupied
by that archivelog will be marked as reclaimable.
#############2
CAUSE
The log_archive_dest_1 parameter was set as:
log_archive_dest_1=LOCATION=+FRA_01/EAPRD/ARCHIVELOG_2
which points to flash recovery area. Files will get generated but will not be considered by FRA algorithm for cleaning up when space pressure mounts up.
These logs will not participate in the FRA algorithms. They will just exist in the same diskgroup that houses the FRA. Reason for an "ORA-15041: diskgroup space exhausted" error is the disk group itself had run out of space.
If the FRA had been in use for archive logs (setting log_archive_dest_1='location=use_db_recovery_file_dest'), then Oracle will check check for files whose space is eligible for reclaiming.
Any archive logs that are not managed via the FRA (v$archived_log.IS_RECOVERY_DEST_FILE=NO) should be manually managed, i.e. back them up and remove them to free up space if necessary.
Archive log files managed by the FRA will be governed by the rules mentioned in Note 315098.1.
Use next query to verify this
select dest_id, IS_RECOVERY_DEST_FILE, count(*)
from v$archived_log
group by dest_id, IS_RECOVERY_DEST_FILE;
DEST_ID IS_ COUNT(*)
---------- --- ----------
1 NO 3477
1 YES 413
#########sample 3 使用dupliacte 搭建dg库,这种方式搭建dg 库步骤比较清晰,也方便懂,
参考文档:
https://oracledistilled.com/oracle-database/high-availability/data-guard/creating-a-physical-standby-using-rman-duplicate-from-active-database/
2. 11g duplicate an dataguard
https://oracledistilled.com/oracle-database/high-availability/data-guard/creating-a-physical-standby-using-rman-duplicate-from-active-database/
2.1 准备时间1小时
#shutdown immediate
#cp file to remote location:
Primary database information
Host: db767-0
DB_NAME = dbb
DB_UNIQUE_NAME=dbb
Standby database information
Host: db4-11
DB_NAME = dbb
DB_UNIQUE_NAME=Sdbb
SQL> select force_logging from v$database;
FOR
---
NO
SQL> alter database force logging;
Database altered.
SQL>
--add standby log
select group#, thread#, bytes/1024/1024
from v$log;
SQL> alter database add standby logfile '/ddd/oracle/data/dbb/stby_redo01.log' size 50M;
Database altered.
SQL> alter database add standby logfile '/ddd/oracle/data/dbb/stby_redo02.log' size 50M;
Database altered.
SQL> alter database add standby logfile '/ddd/oracle/data/dbb/stby_redo03.log' size 50M;
Database altered.
SQL> alter database add standby logfile '/ddd/oracle/data/dbb/stby_redo04.log' size 50M;
Database altered.
---DB_UNIQUE_NAME will be used in the LOG_ARCHIVE_CONFIG
show parameter LOG_ARCHIVE_CONFIG
alter system set log_archive_config='DG_CONFIG=(dbb,Sdbb)';
show parameter log_archive_dest
show parameter log_archive_dest_2
alter system set log_archive_dest_2='SERVICE=Sdbb LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=Sdbb';
--config parameter
--FAL_SERVER is the primary and FAL_CLIENT is the standby.
alter system set fal_server=dbb;
alter system set fal_client=Sdbb;
alter system set standby_file_management=auto;
alter system set remote_loging_passwordfile=exclusive;
---Add TNS Names Entries for both pritst and stbytst databases on both servers
--SERVICE_NAME 完全一样
dbb =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.241.25.176)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dbb)
)
)
Sdbb =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.241.24.56)(PORT = 1522))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dbb)
)
)
2.2 Create Standby Shell 准备时间1个小时
cd /ddd/oracle/data/
mkdir /ddd/oracle/data/dbb
mkdir /ddd/oracle/fra
mkdir -p /ddd/oracle/app/product/diag/rdbms/dbb/dbb
mkdir /ddd/oracle/app/product/diag/rdbms/dbb/dbb/trace
mkdir /ddd/oracle/app/product/diag/rdbms/dbb/dbb/alert
mkdir /ddd/oracle/app/product/diag/rdbms/dbb/dbb/incident
mkdir /ddd/oracle/app/product/diag/rdbms/dbb/dbb/cdump
mkdir /ddd/oracle/app/product/diag/rdbms/dbb/dbb/hm
mkdir /ddd/oracle/app/product/11g/dbs
mkdir -p /ddd/oracle/app/product/admin/dbb/adump
Add the following line to /etc/oratab on the standby server.
pritst:/u01/app/oracle/product/11.2.0/dbhome_1:N
--Create the listener and statically register the standby database.
--SID_NAME = dbb 完全一样
SID_LIST_LISTENER_DG =
(SID_LIST =
(SID_DESC =
(SID_NAME = dbb)
(ORACLE_HOME = /ddd/oracle/app/product/11g)
)
)
LISTENER_DG =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.241.24.56)(PORT = 1522))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
[oracle@ocm2 ~]$ lsnrctl start
--Also insure that the TNS entries for both the primary and standby are in the ORACLE_HOME/network/admin/tnsnames.ora file.
--SERVICE_NAME 完全一样
PRITST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ocm1.odlabs.net)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pritst)
)
)
STBYTST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ocm2.odlabs.net)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pritst)
)
)
--Create a basic PFILE in ORACLE_HOME/dbs with the following parameters.
[oracle@ocm2 dbs]$ cat initpritst.ora
DB_NAME=dbb
DB_UNIQUE_NAME=Sdbb
[oracle@ocm2 dbs]$
--如果没有密码,可以考虑手工创建一个密码
orapwd file=/ddd/oracle/app/product/11g/dbs/orapwdbb password=oracle entries=10
--Copy the password file from the primary server to the standby server
[oracle@ocm2 ~]$ scp oracle@ocm1:$ORACLE_HOME/dbs/orapwpritst $ORACLE_HOME/dbs
[oracle@ocm1 ~]$ . oraenv
ORACLE_SID = [oracle] ? pritst
The Oracle base for ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 is /u01/app/oracle
[oracle@ocm2 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Sep 28 10:40:29 2011
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 146472960 bytes
Fixed Size 1335080 bytes
Variable Size 92274904 bytes
Database Buffers 50331648 bytes
Redo Buffers 2531328 bytes
SQL> exit
[oracle@ocm2 ~]$
2.3 准备脚本 运行脚本 1小时
[oracle@ocm2 ~]$ cat dupstby.cmd
run {
allocate channel pri1 type disk;
allocate channel pri2 type disk;
allocate channel prmy4 type disk;
allocate auxiliary channel stby type disk;
duplicate target database
for standby
from active database
dorecover
spfile
set db_unique_name='Sdbb'
set control_files='/ddd/oracle/data/dbb/control01.ctl','/ddd/oracle/data/dbb/control02.ctl'
set fal_client='dbb'
set fal_server='Sdbb'
set standby_file_management='AUTO'
set log_archive_config='dg_config=(dbb,Sdbb)'
set log_archive_dest_1='LOCATION=/ddd/oracle/fra/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=Sdbb'
set log_archive_dest_2='service=dbb ASYNC valid_for=(ONLINE_LOGFILE,PRIMARY_ROLE) db_unique_name=dbb'
nofilenamecheck;
}
[oracle@ocm2 ~]$
---On standby connect to both the target (pritst) and the auxiliary (stbytst) in RMAN.
export ORACLE_SID=dbb
--确保可以登陆.密码文件权限是oracle
export ORACLE_SID=dbb
sqlplus sys/oracle@sdbb as sysdba
ls -ltr $ORACLE_HOME/dbs/orapw*
rman
connect target sys/oracle_1234@dbb
connect auxiliary sys/oracle_1234@Sdbb
rman
connect target sys/oracle@dbb
connect auxiliary sys/oracle@Sdbb
@dupstby.cmd
2.4 启动mrp,并且检查 (15分钟)
[oracle@ocm2 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jul 13 17:17:15 2012
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> alter database recover managed standby database disconnect from session;
Database altered.
select sequence#, first_time, next_time, applied
from v$archived_log
order by sequence#;
#########上面介绍的是rman 搭建dg, 下文中写的是11g到19c 升级,借助TTS
###### step 3 物理迁移数据文件 预计时间1-2小时
####本次目的是为了通过物理级别的复制,并进行逻辑级别impdp ,实现将11g 升级到19C的目的
2 准备阶段:
准备源库阶段:
--源库列出需要迁移的表空间
--源库列出需要迁移的表空间数据文件
select listagg(''''||tablespace_name||'''',',') within group(order by tablespace_name)
from dba_tablespaces
where CONTENTS='PERMANENT'
and TABLESPACE_NAME not in ('SYSTEM','SYSAUX')
order by tablespace_name;
select
TABLESPACE_NAME,
FILE_NAME,
FILE_ID
from dba_data_files
where tablespace_name in (
select tablespace_name
from dba_tablespaces
where CONTENTS='PERMANENT'
and TABLESPACE_NAME not in ('SYSTEM','SYSAUX'))
order by tablespace_name,file_id;
--2.3.目标库创建非默认temp表空间
--源库列出需要在目标库新建的非默认temp表空间
--col TABLESPACE_NAME for a30
--col FILE_NAME for a120
select TABLESPACE_NAME,FILE_NAME from dba_temp_files where TABLESPACE_NAME != 'TEMP';
--源库生成需要在目标库新建的非默认temp表空间及其文件。需要对生成路径中的文件路径进行调整
select 'create temporary tablespace ' || TABLESPACE_NAME || ' tempfile ''' || FILE_NAME || ''' size 500m autoextend on;' output from dba_temp_files where TABLESPACE_NAME != 'TEMP';
--2.6.源库确认需要传输的表空间为self-contained
--根据”2.1.确认需要迁移的表空间”的输出结果,源库确认需要传输的表空间为self-contained
EXECUTE DBMS_TTS.TRANSPORT_SET_CHECK('USERS',TRUE);
SELECT * FROM TRANSPORT_SET_VIOLATIONS;
--2.7.源库调整dbauser用户默认表空间
--如果需要迁移users表空间,则调整dbauser用户默认表空间为sysaux
alter user dbauser default tablespace sysaux;
--2.8.源库查询回收站对象并进行清理
--源库查询回收站对象并进行清理
select count(*) from dba_recyclebin;
purge dba_recyclebin;
select count(*) from dba_recyclebin;
准备目标库阶段:
--2.4.目标库删除users表空间
sqlplus / as sysdba
alter session set container=dbb;
alter database default tablespace sysaux;
drop tablespace users including contents and datafiles;
--2.5.目标库为dbauser用户授权
sqlplus / as sysdba
alter session set container=dbb;
grant sysbackup to dbauser;
--2.9.目标库环境参照源库环境安装11g数据库软件,并搭建Physical Standby
--Physical Standby数据文件目录与目标库数据文件目录存放于同一个存储卷上,以方便快速移动数据文件
--切换目标环境
. oraenv
--2.10.目标库环境检查11g Physical Standby同步情况
select sequence#, first_time, next_time, applied
from v$archived_log
order by sequence#;
--3.数据文件迁移
--停止业务应用程序并确认没有业务应用程序连接后执行后续步骤
--1.2.3.3.1.检查源库dataguard物理备库同步状态
select * from v$dataguard_stats;
实施阶段:
--3.2.源库停止监听程序
--源库停止监听程序
lsnrctl stop dbb
--3.3.源库重启数据库实例
--源库停止监听程序
sqlplus / as sysdba
shutdown immediate
startup
--3.4.源库手工归档日志
--源库手工归档日志
alter system checkpoint;
alter system archive log current;
--3.5.源库设置表空间为read only
--源库根据”2.1.确认需要迁移的表空间”的输出设置需要迁移的表空间为read only
select tablespace_name,status from dba_tablespaces where CONTENTS='PERMANENT' order by status,tablespace_name;
select 'alter tablespace ' || tablespace_name || ' read only;'
from dba_tablespaces
where CONTENTS='PERMANENT'
and TABLESPACE_NAME not in ('SYSTEM','SYSAUX')
order by tablespace_name;
select tablespace_name,status from dba_tablespaces where CONTENTS='PERMANENT' order by status,tablespace_name;
alter system archive log current;
--3.7.检查源库dataguard物理备库同步状态
select * from v$dataguard_stats;
--3.8.源库dataguard物理备库检查tablespace状态
select tablespace_name,status from dba_tablespaces where CONTENTS='PERMANENT' order by status,tablespace_name;
--3.9.源库停止dataguard同步
alter system set log_archive_dest_state_2=defer scope=both;
--3.10.停止源库dataguard物理备库
--获取移动数据文件命令
select 'mv \' cmd from dual
union all
select
FILE_NAME || ' \' cmd
from dba_data_files
where tablespace_name in (
select tablespace_name
from dba_tablespaces
where CONTENTS='PERMANENT'
and
TABLESPACE_NAME not in ('SYSTEM','SYSAUX'))
union all
select '/dddd/oracle/data/Cdbb/dbb' cmd from dual;
result:
mv \
/dddd/oracle/data/dbb/users01.dbf \
/dddd/oracle/data/Cdbb/dbb
--停止源库dataguard物理备库
alter database recover managed standby database cancel;
shutdown immediate;
--3.11.移动源库dataguard物理备库数据文件至目标库PDB数据文件目录
--scp传输数据文件至目标库操作系统data卷相应的pdb目录
result:
mv \
/dddd/oracle/data/dbb/users01.dbf \
/dddd/oracle/data/Cdbb/dbb
--4.元数据迁移
--1.2.3.4.4.1.目标库新建连接源库的DBLINK
--目标库新建连接源库的DBLINK
--
drop public database link conn_to_11g;
create public database link conn_to_11g connect to dbauser identified by "dbauser_1234" using '10.241.25.176:1521/dbb';
select instance_name from v$instance@conn_to_11g;
--4.2.目标库通过DBLINK导入元数据
--目标库新建directory
mkdir /dddd/oracle/dumpdir
sqlplus / as sysdba
alter session set container=dbb;
CREATE DIRECTORY dp_dir AS '/dddd/oracle/dumpdir';
grant read,write on DIRECTORY dp_dir to public;
--目标库导入dump文件。使用以下命令获取导入命令,检查并执行:
set serveroutput on
declare
v_source_path VARCHAR2(100);
v_target_path VARCHAR2(100);
cursor imp_cur is
select 'impdp dbauser@dbb NETWORK_LINK=conn_to_11g \' cmd from dual
union all
select ' DIRECTORY=dp_dir logfile=impdp_full_transportable_11g_to_19c.log \' cmd from dual
union all
select 'VERSION=12 FULL=Y TRANSPORTABLE=ALWAYS \' cmd from dual
union all
select 'TRANSPORT_DATAFILES=''' || replace(FILE_NAME,v_source_path,v_target_path) || ''' \' cmd
from dba_data_files@conn_to_11g
where tablespace_name in (
select tablespace_name
from dba_tablespaces@conn_to_11g
where CONTENTS='PERMANENT'
and TABLESPACE_NAME not in ('SYSTEM','SYSAUX'))
union all
select 'exclude=statistics cluster=n' cmd from dual;
begin
select substr(f.FILE_NAME,1,instr(f.FILE_NAME,'/',-1)) into v_source_path from dba_data_files@conn_to_11g f where upper(f.file_name) like '%SYSTEM01%';
select substr(f.FILE_NAME,1,instr(f.FILE_NAME,'/',-1)) into v_target_path from dba_data_files f where upper(f.file_name) like '%SYSTEM01%';
dbms_output.put_line('cmd');
dbms_output.put_line('--------------------------------------------');
for c in imp_cur loop
dbms_output.put_line(c.cmd);
end loop;
end;
/
Job "dbauser"."SYS_IMPORT_FULL_01" completed with 328 error(s) at Sat Oct 9 15:39:53 2021 elapsed 0 00:22:39
--4.3.源库获取SYS对象授权语句,并在目标库执行
create table sys.t_user_cfg as SELECT username, default_tablespace
FROM dba_users
WHERE username in
('dbbDATA','dbbOPER','dbbCOP','dbbETL');
grant select on sys.t_user_cfg to public;
create table sys.t_user_cfg as SELECT username, default_tablespace
FROM dba_users@conn_to_11g
WHERE username in
('dbbDATA','dbbOPER','dbbCOP','dbbETL');
grant select on sys.t_user_cfg to public;
SELECT 'grant '||privilege||
' on "'||owner||'"."'||
table_name||'" to "'||grantee||'" '||
DECODE(grantable, 'YES', 'WITH Grant option')||';'
FROM dba_tab_privs@conn_to_11g
WHERE owner = 'SYS'
AND grantee in (select username from t_user_cfg);
SELECT 'grant '||privilege||' ("'||column_name||'") '||
' on "'||owner||'"."'||table_name||'" to "'||grantee||'" '||
DECODE(grantable, 'YES', 'WITH Grant option')||';'
FROM dba_col_privs@conn_to_11g
WHERE owner = 'SYS'
AND grantee in (select username from t_user_cfg);
--4.4.目标库重编译失效对象
--目标库重新编译失效对象
sqlplus / as sysdba
alter session set container=dbb;
@?/rdbms/admin/utlrp.sql
--5.结果校验
--1.2.3.4.5.5.1.数据文件校验
--检查物理和逻辑block corruption
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
rman
connect target "dbauser@dbb as sysbackup"
run {
allocate channel ch1 device type DISK;
allocate channel ch2 device type DISK;
allocate channel ch3 device type DISK;
allocate channel ch4 device type DISK;
allocate channel ch5 device type DISK;
allocate channel ch6 device type DISK;
allocate channel ch7 device type DISK;
allocate channel ch8 device type DISK;
validate tablespace USERS check logical;
release channel ch1;
release channel ch2;
release channel ch3;
release channel ch4;
release channel ch5;
release channel ch6;
release channel ch7;
release channel ch8;
}
5.2.元数据校验
--6.2.目标库调整默认表空间为USERS
--目标库调整默认表空间为USERS
alter database default tablespace USERS;
--6.3.目标库回收dbauser用户授权
sqlplus / as sysdba
alter session set container=dbb;
revoke sysbackup from dbauser;
--6.4.目标库删除连接源库的DBLINK
drop public database link conn_to_11g;
--6.5.检查目标库表空间状态是否都为READ WRITE
select tablespace_name,status from dba_tablespaces where CONTENTS='PERMANENT' order by status,tablespace_name;
--6.6.源库设置表空间为READ WRITE
源库根据实际需求,设置表空间为READ WRITE或保持READ ONLY
select 'alter tablespace ' || tablespace_name || ' read WRITE;'
from dba_tablespaces
where CONTENTS='PERMANENT'
and TABLESPACE_NAME not in ('SYSTEM','SYSAUX')
and STATUS='READ ONLY'
order by tablespace_name;
--6.1.收集统计信息
--目标库执行以下命令获取统计信息收集语句,检查并执行:
exec dbms_stats.gather_database_stats(degree=>8);