hostname | ip | db_name | db_unique_name | net service name |
---|---|---|---|---|
db11 | 172.16.8.11 | orcl | orcl11 | orcl11 |
db12 | 172.16.8.12 | orcl | orcl12 | orcl12 |
DBCA建立主库,修改online redo 大小为500M
主库开启归档并设置强制日志 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;
--查看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 '/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;
创建pfile文件, 默认路径为$ORACLE_HOME/dbs,此处为/u01/app/oracle/product/11.2.0/dbhome_1/dbs/,在sqlplus里执行以下命令
SQL> create pfile from spfile;
将主库的pfile复制到备库/u01/app/oracle/product/11.2.0/dbhome_1/dbs/下
cd /u01/app/oracle/product/11.2.0/dbhome_1/dbs/
scp initorcl.ora db12:/u01/app/oracle/product/11.2.0/dbhome_1/dbs/
编辑/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora文件,追加
cat >> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora << "EOF"
*.db_unique_name='orcl11'
*.fal_server='orcl12'
*.log_archive_config='dg_config=(orcl11,orcl12)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=orcl11'
*.log_archive_dest_2='service=orcl12 lgwr async valid_for=(online_logfile,primary_role) db_unique_name=orcl12'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
EOF
创建新的主库spfile文件,并重新启动主库
SQL> shutdown immediate
SQL> create spfile from pfile;
SQL> startup
编辑/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora文件,修改备库初始化参数
cat >> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora << "EOF"
*.db_unique_name='orcl12'
*.fal_server='orcl11'
*.log_archive_config='dg_config=(orcl11,orcl12)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=orcl12'
*.log_archive_dest_2='service=orcl11 lgwr async valid_for=(online_logfile,primary_role) db_unique_name=orcl11'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
EOF
将密码文件orapworcl复制到备库的/u01/app/oracle/product/11.2.0/dbhome_1/dbs/下
cd /u01/app/oracle/product/11.2.0/dbhome_1/dbs/
scp orapworcl db12:/u01/app/oracle/product/11.2.0/dbhome_1/dbs/
使用oracle用户创建以下目录,避免权限问题
mkdir -p /u01/app/oracle/oradata/orcl/
mkdir -p /u01/app/oracle/admin/orcl/adump/
mkdir -p /u01/app/oracle/fast_recovery_area/orcl/
使用图形界面配置,采用静态监听
netmgr
使用图形界面配置,在主备库上均需配置orcl11、orcl12两个服务名
netmgr
创建备库的spfile文件,启动备库到nomount模式
SQL> create spfile from pfile;
SQL> startup nomount
首先RMAN连接到目标数据库和辅助数据库
rman target sys/password@orcl11 auxiliary sys/password@orcl12
使用RMAN的duplicate命令进行复制,两边目录结构相同,需要添加nofilenamecheck参数
RMAN> duplicate target database for standby from active database nofilenamecheck;
复制成功后,备库自动被加载为mount模式,进入sqlplus查看
SQL> select status from v$instance;
SQL> alter database recover managed standby database using current logfile disconnect from session;
SQL> select switchover_status,database_role from v$database;
--主库显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭活动的会话再检查
--备库显示:NOT ALLOWED/PHYSICAL STANDBY
在主库端切换归档,在备库检查是否也发生了切换
主库上执行日志切换
SQL> archive log list;
SQL> alter system switch logfile;
SQL> archive log list;
备库上查看,日志的sequence号也跟着变了
SQL> archive log list;
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.
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
SQL> select * from v$dataguard_status;
以只读方式打开备库,并开启实时日志应用
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;
在主库解锁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;
这种保护模式(默认)提供了可能的最高级别的数据保护,而不影响主数据库的性能。这是通过允许事务在恢复该事务所需重做数据在写到本地联机重做日志后立即提交而实现的。主数据库的重做数据流也写到至少一个备数据库,但是那个重做流相对于创建重做数据的事务是异步写的。
当所用的网络连接有足够的带宽,这种模式提供了近似于最大可用性模式的数据保护级别,并且对主数据库性能的影响最小。
这种保护模式提供了可能的最高级别的数据保护,而不用与主数据库的可用性相折衷。与最大保护模式相同,在恢复事务所需的重做写到本地联机重做日志和至少一个事务一致性备数据库上的备重做日志之前,事务将不会提交。与最大保护模式不同的是,如果故障导致主数据库无法写重做流到异地备重做日志时,主数据库不会关闭。替代地,主数据库以最大性能模式运行直到故障消除,并且解决所有重做日志文件中的中断。当所有中断解决之后,主数据库自动继续以最大可用性模式运行。
这种模式确保如果主数据库故障,但是只有当第二次故障没有阻止完整的重做数据集从主数据库发送到至少一个备数据库时,不发生数据丢失。
这种保护模式确保如果主数据库故障不会发生数据丢失。要提供这种级别的保护,恢复每个事务所需的重做数据必须在事务提交之前同时写到本地联机重做日志和至少一个备数据库上的备重做日志。要确保不发生数据丢失,如果故障导致主数据库无法写重做流到至少一个事务一致性备数据库的备重做日志时,主数据库会关闭。
SQL> select protection_mode,protection_level from v$database;
db11-orcl:主库------>备库
db12-orcl:备库------>主库
主备库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
--db11-orcl显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭会话再检查
--db12-orcl显示:NOT ALLOWED/PHYSICAL STANDBY
db11-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
db12-orcl切换到主库
SQL> alter database commit to switchover to primary;
SQL> alter database open;
db11-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> alter database open;
SQL> alter database recover managed standby database disconnect from session;
SQL> select switchover_status,database_role,open_mode from v$database;
db11-orcl:主库------>崩溃
db12-orcl:备库------>主库
主备库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
db11-orcl通过shutdown abort方式人工模拟主库崩溃,直接关闭
SQL> select open_mode from v$database;
SQL> shutdown abort
SQL> startup mount
SQL> alter system flush redo to 'orcl12';
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;
db11-orcl:崩溃------>备库
db12-orcl:主库------>主库(保持主库状态不变)
db12-orcl主库角色状态查询
SQL> select switchover_status,database_role,open_mode from v$database;
db12-orcl主库创建备库控制文件
mkdir -p /u01/bak/
SQL> alter database create standby controlfile as '/u01/bak/control01.ctl';
将备库控制文件拷贝至db11
scp ezdb12:/u01/bak/control01.ctl /u01/app/oracle/oradata/orcl/control01.ctl
cp /u01/app/oracle/oradata/orcl/control01.ctl /u01/app/oracle/fast_recovery_area/orcl/
mv /u01/app/oracle/fast_recovery_area/orcl/control01.ctl /u01/app/oracle/fast_recovery_area/orcl/control02.ctl
恢复对应数据文件至db11,并启动db11-orcl到mount状态,应用APPLY模式
SQL> startup mount;
SQL> alter database recover managed standby database disconnect from session;
启动db11-orcl至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;
关闭主库
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
启动备库监听
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;