配置好dataguard后,可以演练主库崩溃后的操作。一般有两种方式,一种是用oracle的dataguard broker,启用fast_start failover,自动切换;一种是用failover手工进行切换。下面就两种方式,分别做介绍。
dataguard broker的配置:
相关的理论知识,可以到如下链接,这里不作赘述
http://www.oracle.com/pls/db112/to_toc?pathname=server.112%2Fe17023%2Ftoc.htm&remark=docindex
1。主备库做如下修改
主库:
SQL>alter system set log_archive_dest_2='SERVICE=dbstandby LGWR SYNC AFFIRM NET_TIMEOUT=120 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=db249' SCOPE=BOTH;
备库:
SQL>alter system set log_archive_dest_2='SERVICE=dbstandby LGWR SYNC AFFIRM NET_TIMEOUT=120 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=db248' SCOPE=BOTH;
其中,LGWR,SYNC,AFFIRM,NET_TIMEOUT为必要设置
2。主备库做如下修改
主库:
SQL>alter system set local_listener=dbprimary
值为tnsnames中配置的主库的链接标志
备库:
SQL>alter system set local_listener=dbstandby
值为tnsnames中配置的备库的链接标志
3。查看并修改dg模式,保证主备库处于最大可用状态
主备库同样操作:
SQL>select protection_mode,protection_level from v$database;
切换模式,需要在mount状态下,所以主库应想关闭,然后启动到mount状态
SQL>shutdown immediate;
SQL>startup mount;
切换模式:
SQL>alter database set standby database to maximize availability;
查看修改结果:
SQL>select protection_mode,protection_level from v$database;
4。启动flashback,数据库缺省状态为关闭
开启flashback需要在mount状态下
查看是否开启:
SQL>show parameter db_recover_file_dest
SQL>select flashback_on from v$database;
主库:
SQL>shutdown immediate
SQL>startup mount
SQL>alter database flashback on;
备库:
SQL>alter database recover managed standby database cancel;
SQL>alter database flashback on;
SQL> select flashback_on from v$database;
SQL>alter database recover managed standby database disconnect from session;
5。安装dgmgrl
一般推荐dataguard broker配置在主备库以外的机器上,以保证主备库的崩溃,不会影响dataguard broker,否则,就没有意义了。这里作为测试,配置在备库机器上。
安装dgmgrl,就是安装oracle的客户端,最好是相同版本。
6。配置broker参数
主备库同样操作
SQL>show parameter dg_broker_start
SQL>alter system set dg_broker_start=true scope=both;
7。配置监听
主备库同样操作,在listener.ora文件中,添加如下行
(SID_DESC =
(GLOBAL_NAME = db248_DGMGRL.node248.gewara)
(ORACLE_HOME = /u01/app/oracle/products/11.2/db_1)
(SID_NAME = dbtest)
)
其中,global_name必须按照固定格式
添加完毕后,保存,用命令
$lsnrctl reload
重新加载配置。
8。设置fast_start failover
进入dgmgrl命令行模式
$dgmgrl
DGMGRL>connect sys/oracle@dbprimary #链接主库
DGMGRL>help create
DGMGRL>create configuration ‘db248’ as primary database is ‘dbprimary’ connect identifier is dbprimary;
DGMGRL>help add
DGMGRL>add database ‘db249’ as connect identifier is dbstandby maintained as physical;
DGMGRL>show configuration;
db248,db249分别是主备库的db_unique_name,dbprimary,dbstandby是tnsnames配置中的主备库的链接标识
之后,可以用sql命令查看生成的配置文件
SQL>show parameter dg_broker_config_file;
启用configuration
DGMGRL>enable configuration;
DGMGRL>show database verbose ‘db248’;
DGMGRL>show database verbose ‘db249’;
在启用fast_start failover之前,主备库日志传输的模式必须为sync,不可以是aync,可以用如下语句修改:
DGMGRL>edit database dbprimary set property logxptmode=sync;
启用FSFO
DGMGRL>enable fast_start failover;
9。启动observer
DGMGRL>start observer;
10。验证
查看FSFO的状态,在主库执行如下SQL语句:
SQL>select fs_failover_observer_present,fs_failover_server_host,fs_failover_threshold from v$database;
在如下情况发生时,会触发failover
1》主库实例崩溃
2》书库shutdown abort
3》由于io错误,数据库离线
4》网络故障
配置TAF,关于TAF,可以参照如下内容:
http://blog.csdn.net/tianlesoftware/archive/2010/12/13/6072294.aspx
在tnsnames中添加如下内容:
TAFTEST =
(DESCRIPTION =
(LOAD_BALANCE = on)
(FAILOVER = on)
(ADDRESS =
(PROTOCOL = tcp)
(HOST = 192.168.2.248)
(PORT = 1521)
)
(ADDRESS =
(PROTOCOL = tcp)
(HOST = 192.168.2.249)
(PORT = 1521)
)
(CONNECT_DATA =
(SERVICE_NAME = dbtest)
(FAILOVER_MODE =
(TYPE = select)
(METHOD = basic)
)
)
)
可以用这个TAF实例,来查看dg主库的信息
$sqlplus /nolog
SQL>conn sys/oracle@taftest
SQL>select name,db_unique_name from v$database;
灾难模拟:
主库
SQL>shutdown abort;
2分钟后,可以查看dgmgrl日志,查看切换结果。
SQL>conn sys/oracle@taftest
SQL>select name,db_unique_name from v$database;
在原来的备库:
SQL>select database_role from v$database;
在dgmgrl中查看:
DGMGRL>show database verbose db248;
DGMGRL>show database verbose db249;
如果broker有sysdba权限,broker会尝试重启并恢复原主库,使之变成备库。若不行,则需要手工进行恢复。
确定原主库需要恢复到的目标scn
SQL>select to_char(standby_became_primary_scn) from v$database;
原主库启动到mount状态:
flashback database to scn *******;
转换数据库,以生成备库用的controlfile
SQL>alter database convert to physical standby;
SQL>shutdown immediate
SQL>startup mount
重启redo传输:
查看log_archive_dest状态:
SQL>select dest_id,dest_name,status,protection_mode,destination,error,srl from v$archive_dest_status;
如果需要,特别是dest1和dest2,执行如下语句:
SQL>alter system set log_archive_dest_state_n=enable;
新的主库上执行:
SQL>alter system switch logfile;
成功则说明日志传输正常。
应用redolog
SQL>alter database recover managed standby database disconnect from session;
SQL>alter database recover managed standby database using current logfile disconnect from session;
手工failover:
先将备库切换到最大性能:
SQL>alter database set standby database maximize performance;
查看备库归档是否连续:
SQL>select thread#,low_sequence#,high_sequence# from v$archive_gap;
若有返回,则应拷贝相应的归档日志,到备库,并用如下命令注册:
SQL>alter database register physical logfile ‘。。。'
查看归档是否完整
SQL>select distinct thread#,max(sequence#) over(partition by thread#) a from v$archived_log;
拷贝相应文件,如下操作
SQL>alter database register physical logfile ‘。。。'
反复执行,知道上面两步返回值为空。
备库上执行:
SQL>alter database recover managed standby database finish force;
角色装换:
SQL>select database_role from v$database;
SQL>alter database commit to switchover to primary;
SQL>select database_role from v$database;
SQL>select open_mode from v$database;
若为mount,则直接打开。
SQL>alter database open;
SQL>select open_mode from v$database;
若为readonly,再关闭,再重启
SQL>shutdown immediate
SQL>startup
后面的,如果需要,在主备切换一下。