oracle dg有几种搭建方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle dg有几种搭建方法相关的知识,希望对你有一定的参考价值。
1、默认模式,提供主库的最高可用性能2、保证主库在运行之中不会受到从库的干扰
优点:避免从库对主库的影响
缺点:不能保证数据不丢失(通过归档的方式,一直查一个arch,如果主库彻底没了,那么redo中的数据会丢失),最低的情况,丢失一个redo组
步骤如下
1.前期规划
2.配置网络监听
3.修改参数文件
4.rman duplicate 复制数据库
5.启动调试数据库
6.测试 参考技术A 可以的,使用EM搭建DG如果一切顺利要比手工快很多,配置顺利的话不到半个小时就可以搞定,但是如果报错,从头找问题就不如按照文档手工一步一步做来的踏实了。
Oracle DG 搭建 生产版
Oracle DG 搭建
1.环境介绍
hostname | ip | db_unique_name | net service name |
---|---|---|---|
db1 | 172.23.17.227 | db1 | orcl |
db2 | 172.23.17.228 | db2 | orcl |
2.DG部署
编辑
2.1.主机db1建立主库orcl
https://www.cnblogs.com/lei01/p/10688036.html
主机db1静默安装数据库软件
主机db1静默创建监听程序
主机db1静默安装数据库
主机db2静默安装数据库软件
主机db2静默创建监听程序
2.2.主库开启归档
主库开启归档并设置强制日志 force logging
--干净的关闭数据库
SQL> shutdown immediate
--以mount模式启动
SQL> startup mount
--切换到归档模式
SQL> alter database archivelog;
--开启强制日志
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
--查看Redo和Standby Redo
SQL> select * from v$logfile;
--仅仅显示Online Redo,不显示Standby Redo
SQL> select * from v$log;
--将日志组大小调整到500M
--添加三组日志
SQL>alter database add logfile group 4 ('/data/app/oracle/oradata/orcl/redo04.log') size 500M;
SQL>alter database add logfile group 5 ('/data/app/oracle/oradata/orcl/redo05.log') size 500M;
SQL>alter database add logfile group 6 ('/data/app/oracle/oradata/orcl/redo06.log') size 500M;
--将日志组切换至inactive并删除旧日志组
SQL>alter system switch logfile;
--删除旧日志组
SQL>alter database drop logfile group 1;
SQL>alter database drop logfile group 2;
SQL>alter database drop logfile group 3;
--重新添加
SQL>alter database add logfile group 1 ('/data/app/oracle/oradata/orcl/redo01.log') size 500M;
SQL>alter database add logfile group 2 ('/data/app/oracle/oradata/orcl/redo02.log') size 500M;
SQL>alter database add logfile group 3 ('/data/app/oracle/oradata/orcl/redo03.log') size 500M;
--删除多余日志组
SQL>alter system switch logfile;
SQL>alter database drop logfile group 4;
SQL>alter database drop logfile group 5;
SQL>alter database drop logfile group 6;
--新增一组大小为500M的Standby Redo,这里的group号不得与Online redo重复
SQL> alter database add standby logfile group 21 '/u01/app/oracle/oradata/orcl/standby21.log' size 500M;
SQL> alter database add standby logfile group 22 '/u01/app/oracle/oradata/orcl/standby22.log' size 500M;
SQL> alter database add standby logfile group 23 '/u01/app/oracle/oradata/orcl/standby23.log' size 500M;
SQL> alter database add standby logfile group 24 '/u01/app/oracle/oradata/orcl/standby24.log' size 500M;
2.4.从主库创建pfile文件
主库
#创建pfile文件
SQL>create pfile from spfile;
#修改篇file文件
[oracle@db1 dbs]$ more initorcl.ora
orcl.__db_cache_size=8355053568
orcl.__java_pool_size=67108864
orcl.__large_pool_size=100663296
orcl.__oracle_base='/data/app/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=3355443200
orcl.__sga_target=10066329600
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=1476395008
orcl.__streams_pool_size=0
*.audit_file_dest='/data/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/data/app/oracle/oradata/orcl/control01.ctl','/data/app/oracle/fast_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/data/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/data/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.open_cursors=300
*.pga_aggregate_target=3354394624
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=10064232448
*.undo_tablespace='UNDOTBS1'
#添加内容
*.db_unique_name='db1'
*.fal_client='db1'
*.fal_server='db2'
*.log_archive_config='DG_CONFIG=(db1,db2)'
*.log_archive_dest_1='location=/data/app/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=db1'
*.log_archive_dest_2='service=db2 lgwr sync valid_for=(online_logfiles,primary_role) db_unique_name=db2'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='log_%t_%s_%r.arc'
*.log_file_name_convert='db2','db1'
*.db_file_name_convert='db2','db1'
*.standby_file_management='auto'
从库参数文件
[oracle@db2 dbs]$ more initorcl.ora
orcl.__db_cache_size=8355053568
orcl.__java_pool_size=67108864
orcl.__large_pool_size=100663296
orcl.__oracle_base='/data/app/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=3355443200
orcl.__sga_target=10066329600
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=1476395008
orcl.__streams_pool_size=0
*.audit_file_dest='/data/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/data/app/oracle/oradata/orcl/control01.ctl','/data/app/oracle/fast_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/data/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/data/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.open_cursors=300
*.pga_aggregate_target=3354394624
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=10064232448
*.undo_tablespace='UNDOTBS1'
#修改内容
*.db_unique_name='db2'
*.fal_client='db2'
*.fal_server='db1'
*.log_archive_config='DG_CONFIG=(db2,db1)'
*.log_archive_dest_1='location=/data/app/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=db2'
*.log_archive_dest_2='service=db1 lgwr sync valid_for=(online_logfiles,primary_role) db_unique_name=db1'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='log_%t_%s_%r.arc'
*.log_file_name_convert='db1','db2'
*.db_file_name_convert='db1','db2'
*.standby_file_management='auto'
2.5 将主库的pfile和密码文件复制到备库$ORACLE_HOME/dbs/下
scp initorcl.ora db2:/data/app/oracle/product/11.2.0/db_1/dbs/
scp orapworcl db2:/data/app/oracle/product/11.2.0/db_1/dbs/
2.6 创建备库相应的目录结构
mkdir -p /data/app/oracle/oradata/orcl/
mkdir -p /data/app/oracle/admin/orcl/adump/
mkdir -p /data/app/oracle/fast_recovery_area/orcl/
2.7 配置主库和备库的监听
修改主库监听文件
[oracle@db1 admin]$ vim listener.ora
# listener.ora Network Configuration File: /data/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME=/data/app/oracle/product/11.2.0/db_1)
(SID_NAME=orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /data/app/oracle
[oracle@db1 admin]$ vim tnsnames.ora
# tnsnames.ora Network Configuration File: /data/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
db1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
db2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
修改备库监听文件
[oracle@db2 admin]$ vim listener.ora
# listener.ora Network Configuration File: /data/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME=/data/app/oracle/product/11.2.0/db_1)
(SID_NAME=orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /data/app/oracle
[oracle@db2 admin]$ vim tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
db1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
db2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
#在主从库测试监听
tnsping db1
tnsping db2
2.8 主库生成spfile文件,创建备库并启动
主库
SQL> shutdown immediate
SQL> create spfile from pfile;
SQL> startup
备库
sqlplus / as sysdba
SQL> create spfile from pfile;
SQL> startup nomount
2.9 RMAN复制主库到备库
#从库执行
rman target sys/password@db1 auxiliary sys/password@db2
RMAN> duplicate target database for standby from active database nofilenamecheck;
#复制成功后,备库自动被加载为mount模式,进入sqlplus查看
SQL> select status from v$instance;
2.10 在备库开启实时日志应用
SQL> alter database recover managed standby database using current logfile disconnect from session;
2.11 主备库角色状态查询
SQL> select protection_mode,database_role,protection_level,switchover_status from v$database;
--主库显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭活动的会话再检查
--备库显示:NOT ALLOWED/PHYSICAL STANDBY
3.测试DG
3.1.执行日志切换测试
在主库端切换归档,在备库检查是否也发生了切换
#主库上执行日志切换
SQL> archive log list;
SQL> alter system switch logfile;
SQL> archive log list;
#备库上查看,日志的sequence号也跟着变了
SQL> archive log list;
#查看备库启动的DG进程
SQL> select process,client_process,sequence#,status from v$managed_standby;
#检查主备日志应用是否有遗漏
SQL>select thread#,low_sequence#,high_sequence# from v$archive_gap;
#检查主备归档日志应用状态
SQL>select sequence#,name,applied from v$archived_log;
#缺少归档日志的话需要物理copy等方式拷贝回来,然后备库注册
alter database register logfile '/opt/oracle/admin/orcl/arch/arc/log_1_122678_1012734063.arc';
alter database register logfile '/opt/oracle/admin/orcl/arch/arc/log_1_122679_1012734063.arc'
#查看数据库的保护模式
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
#查看DG的日志信息
SQL> select * from v$dataguard_status;
#查看备库延迟
SQL> select ceil((sysdate-next_time)*24*60) "M" from v$archived_log where applied='YES' AND SEQUENCE#=(SELECT MAX(SEQUENCE#) FROM V$ARCHIVED_LOG WHERE applied='YES');
3.2 Open Read Only Standby数据库
以只读方式打开备库,并开启实时日志应用
#可以将备库设置为只读状态
SQL>shutdown immediate;
SQL>startup nomount;
SQL>alter database mount standby database;
SQL> alter database open read only;
SQL> alter database recover managed standby database disconnect from session; --应用归档
SQL> alter database recover managed standby database using current logfile disconnect from session; --应用redo实时
SQL> select switchover_status,database_role,open_mode from v$database;
select sequence#,name,applied from v$archived_log;
3.3 解锁scott用户并添加数据,验证是否同步
在主库解锁scott用户并创建测试表,插入10000行数据。
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行数据
SQL> conn scott/tiger;
SQL> select * from tab;
SQL> select count(*) from test001;
4.DG三种模式
4.1.最大性能模式max performance-默认
这种保护模式(默认)提供了可能的最高级别的数据保护,而不影响主数据库的性能。这是通过允许事务在恢复该事务所需重做数据在写到本地联机重做日志后立即提交而实现的。
主数据库的重做数据流也写到至少一个备数据库,但是那个重做流相对于创建重做数据的事务是异步写的。
当所用的网络连接有足够的带宽,这种模式提供了近似于最大可用性模式的数据保护级别,并且对主数据库性能的影响最小
4.2.最大可用性模式max availability
这种保护模式提供了可能的最高级别的数据保护,而不用与主数据库的可用性相折衷。与最大保护模式相同,
在恢复事务所需的重做写到本地联机重做日志和至少一个事务一致性备数据库上的备重做日志之前,事务将不会提交。
与最大保护模式不同的是,如果故障导致主数据库无法写重做流到异地备重做日志时,主数据库不会关闭。替代地,主数据库以最大性能模式运行直到故障消除
并且解决所有重做日志文件中的中断。当所有中断解决之后,主数据库自动继续以最大可用性模式运行。
这种模式确保如果主数据库故障,但是只有当第二次故障没有阻止完整的重做数据集从主数据库发送到至少一个备数据库时,不发生数据丢失。
4.3.最大保护模式max protection
这种保护模式确保如果主数据库故障不会发生数据丢失。要提供这种级别的保护,
恢复每个事务所需的重做数据必须在事务提交之前同时写到本地联机重做日志和至少一个备数据库上的备重做日志。
要确保不发生数据丢失,如果故障导致主数据库无法写重做流到至少一个事务一致性备数据库的备重做日志时,主数据库会关闭。
4.4.查询当前模式
SQL> select protection_mode,protection_level from v$database;
5.DG切换测试
5.1.DG switchover 切换测试
**db1-orcl:主库------>备库 **
db2-orcl:备库------>主库
主备库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
#db1-orcl显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭会话再检查
#db2-orcl显示:NOT ALLOWED/PHYSICAL STANDBY
db1-orcl切换到备库
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
SQL> alter database commit to switchover to primary;
SQL> alter database open;
db1-orcl执行APPLY LOG命令
#启用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>shutdown immediate;
SQL>startup nomount;
SQL>alter database mount standby database;
SQL> alter database open read only;
SQL> alter database recover managed standby database disconnect from session;
SQL> select switchover_status,database_role,open_mode from v$database;
select sequence#,name,applied from v$archived_log;
5.2.DG failover 切换测试
**db1-orcl:主库------>崩溃 **
db2-orcl:备库------>主库
主备库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
db1-orcl通过shutdown abort方式人工模拟主库崩溃,直接关闭
SQL> select open_mode from v$database;
SQL> shutdown abort
SQL> startup mount
SQL> alter system flush redo to 'db1';
db12-orcl执行如下操作切换为主库
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;
select sequence#,name,applied from v$archived_log;
5.3.DG failover后重建DG
**db1-orcl:崩溃------>备库 **
db2-orcl:主库------>主库(保持主库状态不变)
db2-orcl主库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
db2-orcl主库创建备库控制文件
mkdir -p /u01/bak/
SQL> alter database create standby controlfile as '/data/bak/control01.ctl';
将备库控制文件拷贝至db1
scp db2:/data/bak/control01.ctl /data/app/oracle/oradata/orcl/control01.ctl
cp /data/app/oracle/oradata/orcl/control01.ctl /data/app/oracle/fast_recovery_area/orcl/
mv /data/app/oracle/fast_recovery_area/orcl/control01.ctl /data/app/oracle/fast_recovery_area/orcl/control02.ctl
恢复对应数据文件至db1,并启动db1-orcl到mount状态,应用APPLY模式
SQL> startup mount;
SQL> alter database recover managed standby database disconnect from session;
启动db1-orcl至OPEN状态,并应用APPLY REDO模式
SQL> alter database recover managed standby database cancel;
SQL> alter database open read only;
SQL> alter database recover managed standby database disconnect from session;
select sequence#,name,applied from v$archived_log;
6.开启和关闭DataGuard的流程
6.1.关闭
#关闭主库
SQL> shutdown immediate
关闭主库监听
#lsnrctl stop
#查询备库是否正在执行重做应用或实时应用。如果MRP0或MRP进程存在,则备库正在应用重做。
SQL> select process, status from v$managed_standby;
#如果重做应用程序正在运行,停止备库的Redo日志的应用
SQL> alter database recover managed standby database cancel;
#关闭备库
SQL> shutdown immediate
#关闭备库监听
lsnrctl stop
6.2.开启
启动备库监听
lsnrctl start
启动备库到mount状态
SQL> startup mount;
开启备库的Redo日志应用
SQL> alter database recover managed standby database using current logfile disconnect from session;
启动主库监听
lsnrctl start
启动主库
SQL> startup;
检查日志情况的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;
以上是关于oracle dg有几种搭建方法的主要内容,如果未能解决你的问题,请参考以下文章
ORACLE Physical Standby 级联备库搭建