Oracle数据库DG切换

2018-04-23  本文已影响47人  平凡的运维之路

[TOC]

Oracle数据库切换

主库操作

su - oracle
lsnrctl stop(只在主库操作)

sqlplus / as sysdba
SQL> select switchover_status from v$database;

如果SWITCHOVER_STATUS 的值为TO STANDBY 表示可以正常切换.

SQL>alter database commit to switchover to physical standby;

Database altered.

如果SWITCHOVER_STATUS 的值为SESSIONS ACTIVE:

SQL> alter database commit to switchover to physical standby with session shutdown;

Database altered.

tailf $ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log 查看日志

ping 10.100.0.201

sed -i 's/10.100.0.9/10.100.0.201/g' /etc/sysconfig/network-scripts/ifcfg-bond0
cat /etc/sysconfig/network-scripts/ifcfg-bond0

/etc/init.d/network restart

备库操作
如果SWITCHOVER_STATUS 的值为TO PRIMARY 则:

SQL> alter database commit to switchover to primary;

Database altered.
如果SWITCHOVER_STATUS 的值为SESSIONS ACTIVE 则:

SQL> alter database commit to switchover to primary with session shutdown;

Database altered.

SQL> select FILE_NAME,TABLESPACE_NAME,status from dba_temp_files;

tailf $ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log 查看日志


sed -i 's/10.100.0.18/10.100.0.9/g' /etc/sysconfig/network-scripts/ifcfg-bond0

sed -i 's/10.100.0.18/10.100.0.9/g' $ORACLE_HOME/network/admin/listener.ora
cat /etc/sysconfig/network-scripts/ifcfg-bond0

/etc/init.d/network restart

lsnrctl stop
lsntctl start

Sql> shutdown immediate;
Sql> startup;

lsnrctl status 如果一直未注册到监听里面,需手动注册一下
Sql>ALTER SYSTEM REGISTER;

恢复DG同步状态

cat << EOF > $ORACLE_HOME/network/admin/tnsnames.ora.bak 
DB_WENDING =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.0.18)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = WENDING)
    )
  )
DB_PHYSTDBY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.0.9)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = PHYSTDBY)
    )
  )
EOF

cat << EOF > $ORACLE_HOME/network/admin/listener.ora 
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /home/oracle/oracle10g/product/10.2.0/db_1)
      (PROGRAM = extproc)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.0.9)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )
EOF

lsnrctl start

重启新备库

Sql>shutdown immediate;

Sql> startup;

SQL> alter database recover managed standby database disconnect from session; 

select sequence#,dest_id,first_time,next_time,archived,applied from v$archived_log  order by   sequence#;

set linesize 160 pagesize 999
col destination for a30;
col error for a60;
select status,destination,error from v$archive_dest;

set linesize 160 pagesize 999
col destination for a30;
col error for a60;
select dbid,database_role from v$database;
select max(sequence#) from v$archived_log; 
select max(sequence#) from v$archived_log where applied='YES';

select process,status,sequence#from v$managed_standby;

检查新主库脚本


status=`sqlplus -silent  "/as sysdba " <<eof
set feedback off
set verify off
set heading off
set echo off
set pagesize 0
SELECT /*+ rule */ DECODE (
             TRUNC (SYSDATE - start_time),
             0, DECODE (
                   status,
                    'COMPLETED',  '0',
                    'COMPLETED WITH WARNINGS ', '1',
                    '2'),
              '2')
          backup_status
  FROM v\\$rman_backup_job_details
 WHERE start_time = (SELECT MAX(start_time) FROM v\\$rman_backup_job_details);
exit;
eof`
output=`sqlplus -silent  "/as sysdba " <<eof
set feedback off
set verify off
set heading off
set echo off
set pagesize 0
SELECT /*+ rule */ DECODE (
          TRUNC (SYSDATE - start_time),
          0, DECODE (status,
                      'COMPLETED',  'ccod:备份成功!',
                      'COMPLETED WITH WARNINGS',  'ccod:备份有警告,请检查!',
                      'ccod:备份失败,请检查!'),
           'ccod:无备份!')
          backup_status
  FROM v\\$rman_backup_job_details
 WHERE start_time = (SELECT MAX (start_time) FROM v\\$rman_backup_job_details);
exit;
eof`
hostip=$IP
checkname=Rman_Backup_$SID
if [ -z "$status" ]||[ "$status" != 0 ]
then
/bin/logger -p local0.crit "result=ERROR described:$status $output  ccod: 请及时RMAN检查备份情况"
fi

#!/bin/bash

. ~/.bash_profile

##指标1:alert日志文件告警##
b=`cat /home/oracle/b.log`
c=`grep -c ORA- /home/oracle/oracle10g/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log`
if [[ $c -gt $b ]];
then
echo "OraAlarm=false"
else
echo "OraAlarm=true"
fi
##将最新的值更新到b.log文件,用于下次比对##
echo $c >/home/oracle/b.log


##指标2:dg同步监控##
var=`sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
select (select max(SEQUENCE#) from v\\$archived_log where applied='NO')-(select max(SEQUENCE#) from v\\$archived_log where applied='YES') from dual;
EOF`

if [ -z "$var" ]||[ "$var" -gt 1 ]
then
echo "OracleDG=false"
elif [ "$var" -le 1 ]
then
echo "OracleDG=true"
fi

##指标3:监控数据库状态##
STA=`sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
select status from v\\$instance;
EOF`
##传输数据库状态至网管监控##
echo "OracleSID=$STA"

##指标4:监控连接数使用情况##
##获取当前会话数##
CONS=`sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
select count(*) from V\\$SESSION;
EOF`
##获取参数配置情况##
PROS=`sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
select value from v\\$parameter where name = 'processes';
EOF`
##获取连接数使用情况##
num=`expr $CONS \* 100 / $PROS`
##传输连接数使用情况至网管监控##
echo "OracleCON=$num"

#!/bin/bash
. ~/bash_profile
##指标1:alert日志文件告警##
b=`cat /home/oracle/b.log`
c=`grep -c ORA- /home/oracle/oracle10g/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log`
if [[ $c -gt $b ]];
then
echo "OraAlarm = false"
else
echo "OraAlarm = true"
fi
##将最新的值更新到b.log文件,用于下次比对##
echo $c >/home/oracle/b.log

#!/bin/bash
. /home/oracle/.bash_profile
##指标1:alert日志文件告警##
b=`cat /home/oracle/b.log`
c=`grep -c ORA- /home/oracle/oracle10g/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log`
if [[ $c -gt $b ]];
then
echo "OraAlarm=false"
/bin/logger -p local0.crit "OraAlarm=false described:OraAlarm=false  ccod: 请及时检查alert日志情况"
else
echo "OraAlarm=true"
fi
##将最新的值更新到b.log文件,用于下次比对##
echo $c >/home/oracle/b.log

##指标2:dg同步监控##
var=`$ORACLE_HOME/bin/sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
select (select max(SEQUENCE#) from v\\$archived_log where applied='NO')-(select max(SEQUENCE#) from v\\$archived_log where applied='YES') from dual;
EOF`

if [ -z "$var" ]||[ "$var" -gt 1 ]
then
echo "OracleDG=false"
/bin/logger -p local0.crit "DG=ERROR described:OracleDG=false  ccod: 请及时DG同步情况"
elif [ "$var" -le 1 ]
then
echo "OracleDG=true"
fi
$ORACLE_HOME/bin/sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
spool /home/oracle/oracle_gap.log
select * from v\$archive_gap;
exit
EOF
Gap=`cat /home/oracle/oracle_gap.log|wc -l`
if [ "$Gap" != 0 ];
then
echo "OracleDG=false"
/bin/logger -p local0.crit "DG=ERROR described:OracleDG=false  ccod:存在gap, 请及时DG同步情况"
else
echo "OracleDG=true"
fi
##指标3:监控数据库状态##
sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
spool /home/oracle/sta.log
select status from v\$instance;
EOF
STA=`cat /home/oracle/sta.log`
##传输数据库状态至网管监控##
echo "OracleSID=$STA"
if [ "$STA" = "OPEN" ]
then
echo "OracleSID=true"
else
echo "OracleSID=false"
/bin/logger -p local0.crit "ERROR described:OracleSID=false  ccod:数据库状态不是open"
fi


##指标4:监控连接数使用情况##
##获取当前会话数##
sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
spool /home/oracle/con.log
select count(*) from V\$SESSION;
EOF
CONS=`cat /home/oracle/con.log`
##获取参数配置情况##
sqlplus -s / as sysdba <<EOF
set trimspool on
set linesize 2000
set pagesize 0
set newpage 1
set heading off
set feedback off 
set term on
spool /home/oracle/pros.log
select value from V\$parameter where name = 'processes';
EOF
PROS=`cat /home/oracle/pros.log`
##获取连接数使用情况##
num=`expr $CONS \* 100 / $PROS`
##传输连接数使用情况至网管监控##
echo "OracleCON=$num"
if [ "$num" -gt 60 ]
then
echo "OracleCON=false"
/bin/logger -p local0.crit "ERROR described:OracleCON=false  ccod:数据库连接数超过阈值"
else
echo "OracleCON=true"
fi


#!/bin/bash
#########################################################################
#         This shell is for primary and standby database                #
#         to rm applied archivelog that before some day ago.            #
#                                                                       #
#       You can define "some day" in variables ${day_before}            #
#            This shell can be put in crontab for auto run              #
#                                                                       #
#            2008-01-18   writen by www.oracleblog.cn                   #
#########################################################################

## load profile file
. /home/oracle/.bash_profile

## Path Define
main_path=$ORACLE_BASE/del_appl_arc
bin_path=${main_path}/bin
log_path=${main_path}/log
arc_path=$ORACLE_BASE/arch1/WENDING

cd ${bin_path}

## Initial script
touch app_arc_name.sh
chmod +x app_arc_name.sh

## rm applied archivelog that before ${day_before} day ago
day_before=7

## Db info
dbuser=system
dbpwd=oracle
dbsid=db_WENDING

### Create shell for rm applied archive that before some day ago
sqlplus -s "/ as sysdba"<<EOF>/dev/null
set feedback off
set pages 0
set head off
set timing off
set echo off
spool app_arc_name.tmp
select 'rm -f '||name from v\$archived_log 
where DEST_ID=1 and name like '%.dbf' 
and SEQUENCE#<(select max(SEQUENCE#) from v\$archived_log where applied='YES')
and COMPLETION_TIME<=sysdate-${day_before};
spool
exit
EOF
## clear delete expired archivelog all;

## Exec the shell in background mode
cat app_arc_name.tmp |grep -v spooling>app_arc_name.sh
./app_arc_name.sh
chmod -x app_arc_name.sh
mv app_arc_name.sh rm_appl_arc_`date +"%Y%m%d%H%M"`.log
mv rm_appl_arc*.log ${log_path}
rm app_arc_name.tmp 


##clear alter log
export BACKUP_DATE=`date +%y%m%d`
cd $ORACLE_BASE/admin/$ORACLE_SID/bdump

if  [ -f alert_$ORACLE_SID.log ];  then
cat alert_$ORACLE_SID.log >> alert_$ORACLE_SID.$BACKUP_DATE
cat /dev/null > alert_$ORACLE_SID.log
fi

echo 0 > /home/oracle/b.log

#!/bin/sh
#########################################################################
#         This shell is for primary and standby database                #
#         to rm applied archivelog that before some day ago.            #
#                                                                       #
#       You can define "some day" in variables ${day_before}            #
#            This shell can be put in crontab for auto run              #
#                                                                       #
#            2008-01-18   writen by www.oracleblog.cn                   #
#########################################################################

## load profile file
. /home/oracle/.bash_profile

## Path Define
main_path=$ORACLE_BASE/del_appl_arc
bin_path=${main_path}/bin
log_path=${main_path}/log
arc_path=$ORACLE_BASE/arch1/PHYSTDBY

cd ${bin_path}

## Initial script
touch app_arc_name.sh
chmod +x app_arc_name.sh

## rm applied archivelog that before ${day_before} day ago
day_before=7


### Create shell for rm applied archive that before some day ago
sqlplus -s "/ as sysdba"<<EOF>/dev/null
set feedback off
set pages 0
set head off
set timing off
set echo off
spool app_arc_name.tmp
select 'rm -f '||name from v\$archived_log 
where DEST_ID=1 and name like '%.dbf' 
and SEQUENCE#<(select max(SEQUENCE#) from v\$archived_log where applied='YES')
and COMPLETION_TIME<=sysdate-${day_before};
spool
exit
EOF
## clear delete expired archivelog all;



## Exec the shell in background mode
cat app_arc_name.tmp |grep -v spooling>app_arc_name.sh
./app_arc_name.sh
chmod -x app_arc_name.sh
mv app_arc_name.sh rm_appl_arc_`date +"%Y%m%d%H%M"`.log

mv rm_appl_arc*.log ${log_path}
rm app_arc_name.tmp 

##clear alter log
export BACKUP_DATE=`date +%y%m%d`
cd $ORACLE_BASE/admin/$ORACLE_SID/bdump

if  [ -f alert_$ORACLE_SID.log ];  then
cat alert_$ORACLE_SID.log >> alert_$ORACLE_SID.$BACKUP_DATE
cat /dev/null > alert_$ORACLE_SID.log
fi

echo 0 >/home/oracle/b.log



新主库启动后如有以下错误,请参考解决方法

解决方法: 
SQL> show parameter undo  
NAME                                 TYPE        VALUE  
------------------------------------ ----------- ------------------------------  
undo_management                      string      AUTO  
undo_retention                       integer     900  
undo_tablespace                      string      UNDOTBS1 

 SQL> select name from v$tablespace where name like '%UNDO%'; 
NAME  
------------------------------  
UNDOTBS3  

sql > create pfile from spfile;

cd $ORACLE_HOME/dbs

vi init$ORACLE_SID.ora
修改undo_tablespace=UNDOTBS3

然后startup mount pfile=$ORACLE_HOME/dbs/init$ORACLE_SID.ora  (此处请写绝对路径)

sql > create spfile from pfile;

sql> shutdown immediate;

sql> startup

SQL> select TABLESPACE_NAME,FILE_NAME,STATUS from dba_temp_files;

TABLESPACE_NAME            FILE_NAME              STATUS
------------------------------ ------------------------------ ---------
TEMP                   /oradata/ygdb/temp01.dbf       AVAILABLE

如果不是AVAILABLE,请执行以下语句
alter tablespace TEMP add tempfile '/oradata/ygdb/temp02.dbf' size 100m autoextend on;
alter tablespace TEMP drop tempfile '/oradata/ygdb/temp01.dbf';

上一篇 下一篇

猜你喜欢

热点阅读