data guard的主要功能就是作为备库来同步主库的数据变化,一般使用中物理standby使用的比较多。data guard显示威力的一个场景就是swithover了,即主备切换。这种切换方式执行时间很短,能够在一些灾难场景中极大的提高系统的可用性和稳定性。
自己在本地的环境中搭建了一套data guard的环境,开始比较生疏,切换中碰到了不少的问题,最后搭建完成,把切换中的一些细节信息都总结起来,整理成了一个初步的脚本。能够很方便的实现swith over
这个脚本适用于物理standby,在本地环境中反复测试,切换了十多次,还算是比较稳定的。
在脚本中也对需要切换的实例进行了基本的校验,保证不会出现低级错误。比如主库切为主库,备库切为备库等等。
当然对于一些更加细节的信息没有做过滤,比如对于归档gap的判定等。
PRI_DB=`sqlplus -s sys/oracle@$1 as sysdba
set feedback off
set pages 0
select database_role from v\\$database;
EOF`
echo $PRI_DB
if [[ $PRI_DB = 'PHYSICAL STANDBY' ]]
then echo 'PRIMARY DB INSTANCE IS NOT '$1 ',PLEASE CHECK AGAIN'
exit
fi
PRI_DB=$1
#echo $PRI_DB
STD_DB=`sqlplus -s sys/oracle@$2 as sysdba
set feedback off
set pages 0
select database_role from v\\$database;
EOF`
if [[ $STD_DB = 'PRIMARY' ]]
then echo 'STANDBY DB INSTANCE IS NOT '$2 ',PLEASE CHECK AGAIN'
exit
fi
STD_DB=$2
#export ORACLE_SID=$STD_DB
sqlplus -s sys/oracle@$PRI_DB as sysdba
break on db_name
set pages 50
set linesize 100
prompt
prompt Primary Instance
prompt ~~~~~~~~~~~~~~~~
select d.dbid dbid
, d.name db_name
, i.instance_number inst_num
, i.instance_name inst_name
, d.database_role
from v\$database d,
v\$instance i;
EOF
#export ORACLE_SID=$STD_DB
sqlplus -s sys/oracle@$STD_DB as sysdba
break on db_name
set pages 50
set linesize 100
prompt
prompt Standby Instance
prompt ~~~~~~~~~~~~~~~~
select d.dbid dbid
, d.name db_name
, i.instance_number inst_num
, i.instance_name inst_name
, d.database_role
from v\$database d,
v\$instance i;
EOF
sqlplus sys/oracle@$STD_DB as sysdba
prompt recover managed standby database cancel;
recover managed standby database cancel;
EOF
#export ORACLE_SID=$PRI_DB
sqlplus sys/oracle@$PRI_DB as sysdba
prompt Alter database commit to switchover to physical standby with session shutdown;
Alter database commit to switchover to physical standby with session shutdown;
EOF
sqlplus sys/oracle@$PRI_DB as sysdba
prompt shutdown immediate;
shutdown immediate;
EOF
sqlplus sys/oracle@$PRI_DB as sysdba
prompt startup mount
startup mount
prompt recover managed standby database disconnect from session;
recover managed standby database disconnect from session;
EOF
#export ORACLE_SID=$STD_DB
sqlplus sys/oracle@$STD_DB as sysdba
Select name,switchover_status from v\$database;
prompt alter database recover managed standby database finish force;
alter database recover managed standby database finish force;
select name,switchover_status from v\$database;
prompt alter database commit to switchover to primary;
alter database commit to switchover to primary;
select name,database_role from v\$database;
select instance_name,status from v\$instance;
prompt alter database open;
alter database open;
EOF
切换的日志如下,限于篇幅,适当做了整理。
Primary Instance
~~~~~~~~~~~~~~~~
DBID DB_NAME INST_NUM INST_NAME DATABASE_ROLE
---------- --------- ---------- ---------------- ----------------
1028247664 TEST11G 1 TEST11G PRIMARY
Standby Instance
~~~~~~~~~~~~~~~~
DBID DB_NAME INST_NUM INST_NAME DATABASE_ROLE
---------- --------- ---------- ---------------- ----------------
1028247664 TEST11G 1 DG11G PHYSICAL STANDBY
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
idle> recover managed standby database cancel
idle> Media recovery complete.
idle>
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
sys@TEST11G> Alter database commit to switchover to physical standby with session shutdown
sys@TEST11G>
Database altered.
sys@TEST11G>
idle> shutdown immediate
idle> ORA-01507: database not mounted
ORACLE instance shut down.
Connected to an idle instance.
idle> startup mount
idle> ORACLE instance started.
Total System Global Area 435224576 bytes
Fixed Size 1337044 bytes
Variable Size 272632108 bytes
Database Buffers 155189248 bytes
Redo Buffers 6066176 bytes
Database mounted.
idle> recover managed standby database disconnect from session
idle> Media recovery complete.
NAME SWITCHOVER_STATUS
--------- --------------------
TEST11G SWITCHOVER LATENT
idle> alter database recover managed standby database finish force
idle>
Database altered.
NAME SWITCHOVER_STATUS
--------- --------------------
TEST11G TO PRIMARY
idle> alter database commit to switchover to primary
idle>
Database altered.
NAME DATABASE_ROLE
--------- ----------------
TEST11G PRIMARY
idle>
INSTANCE_NAME STATUS
---------------- ------------
DG11G MOUNTED
idle> alter database open
idle>
Database altered.