• Dataguard常用命令汇总


    ----标准DataGuard参数设置------------------------------
    alter system set log_archive_dest_2='SERVICE=ta_std REOPEN=300  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES) db_unique_name=ta_std';
    alter system set fal_server='ta_std';
    alter system set fal_client='ta';

    alter system set fal_server='ta';
    alter system set fal_client='ta_std';


    ----利用ssh tunnel的DataGuard参数设置------------------------------
    alter system set log_archive_dest_2 = 'SERVICE=ta_ssh NOREOPEN VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES) db_unique_name=ta_std alternate=log_archive_dest_3';
    alter system set log_archive_dest_state_2= enable;
    --注意:log_archive_dest_2需要设置noreopen或者reopen=0属性,否则无法迅速切换到备用路径。

    alter system set log_archive_dest_3 = 'SERVICE=ta_std REOPEN=300 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES) db_unique_name=ta_std';
    alter system set log_archive_dest_state_3=alternate;
    --注意:当归档目标log_archive_dest_2不能正常归档时,数据库切换归档目标到log_archive_dest_3,即使log_archive_dest_2归档目标恢复正常,也不会再自动切换回去,需要手工重新修改路径状态参数:
    --alter system set log_archive_dest_state_2= enable;
    --alter system set log_archive_dest_state_3= alternate;

    alter system set fal_server = 'ta_ssh';
    alter system set fal_client = 'ta';

    测试
    tnsping ta     tnsing ta_std     tnsing ta_ssh

    ------------------------------------------------------
    p170a:    ssh -L 15210:p170b:1521 -C -N p170b
    p170b:    ssh -L 15210:p170a:1521 -C -N p170a

    redhat1:    ssh -L 15210:redhat2:1521 -C -N redhat2
    redhat2:    ssh -L 15210:redhat1:1521 -C -N redhat1

    ----DataGuard 中注册日志文件------------------------------
    alter database register OR replace logfile '/data/arch_ta/1_515_673519034.dbf';


    注册日志文件
    catalog archivelog '....';


    ALTER SESSION SET nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
    SELECT TIMESTAMP,MESSAGE FROM V$DATAGUARD_STATUS;

    SELECT * FROM (SELECT THREAD#,SEQUENCE#,FIRST_CHANGE#,NEXT_CHANGE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY FIRST_CHANGE# DESC) WHERE ROWNUM<20;


    一、PRIMARY DATABASE

    1、设置DB FORCE_LOGGING为YES 
    SELECT FORCE_LOGGING FROM V$DATABASE;
    ALTER DATABASE FORCE LOGGING;
    ALTER DATABASE NO FORCE LOGGING;


    2、设置保护模式
    SELECT OPEN_MODE,DATABASE_ROLE,SWITCHOVER_STATUS,PROTECTION_MODE, PROTECTION_LEVEL FROM V$DATABASE;
    ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PROTECTION;
    ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE AVAILABILITY;
    ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE;

    3、设置归档目标
    ALTER SYSTEM SET log_archive_dest_1='location=/app/oracle/arch';
    ALTER SYSTEM set LOG_ARCHIVE_DEST_2='SERVICE=ta_std REOPEN=300  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES)';
    ALTER SYSTEM SET log_archive_dest_state_1=enable;
    ALTER SYSTEM SET log_archive_dest_state_2=defer;

    ALTER SYSTEM SET fal_server=ta_std;
    ALTER SYSTEM SET fal_client=fmp;

    ALTER SYSTEM set DB_FILE_NAME_CONVERT='fmpdb','fmpdb' SCOPE=SPFILE;
    ALTER SYSTEM set LOG_FILE_NAME_CONVERT='fmpdb','fmpdb' SCOPE=SPFILE;
    ALTER SYSTEM SET standby_file_management=auto;

    二、STANDBY DATABASE

    1、建目录结构
    D:oracleproduct10.2.0admin
    D:oracleproduct10.2.0flash_recovery_area
    D:oracleproduct10.2.0oradatafmpdb
    D:oracleproduct10.2.0oradatafmpdbarch

    2、copy 
    D:oracleproduct10.2.0db_1databasePWDfmpdb.ora
    D:oracleproduct10.2.0db_1databaseSPFILEFMPDB.ORA
    D:oracleproduct10.2.0db_1NETWORKADMINlistener.ora  需修改IP
    D:oracleproduct10.2.0db_1NETWORKADMIN nsnames.ora

    C:> ORADIM -NEW -sid fmpdb
    C:> orapwd file=D:oracleproduct10.2.0db_1databasePWDfmpdb.ora password=oracle force=y
    --password必须与PRIMARY保持一致

    starup nomount
    ALTER SYSTEM SET log_archive_dest_1='location=D:oracleproduct10.2.0oradatafmpdbarch';
    ALTER SYSTEM set LOG_ARCHIVE_DEST_2='SERVICE= REOPEN=300 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES)';
    --ALTER SYSTEM reset LOG_ARCHIVE_DEST_2 scope=spfile sid='*';
    ALTER SYSTEM SET log_archive_dest_state_1=enable;
    ALTER SYSTEM SET log_archive_dest_state_1=defer;

    ALTER SYSTEM SET fal_server=fmp;
    ALTER SYSTEM SET fal_client=fmp_std;

    三、PRIMARY DATABASE

    RMAN 备份:
    rman target /
    crosscheck backup;
    delete noprompt expired backup;
    crosscheck archivelog all;
    delete noprompt expired archivelog all;

    BACKUP format 'D:oracleproduct10.2.0oradataackup\%d_STBY_%T_%s_%p' current controlfile for standby;
    BACKUP format 'D:oracleproduct10.2.0oradataackup\%d_FULL_%T_%s_%p' database MAXSETSIZE 2G;
    SQL 'alter system archive log current';
    BACKUP format 'D:oracleproduct10.2.0oradataackup\%d_ARCH_%T_%s_%p' archivelog all MAXSETSIZE 2G;
    exit;

    四、STANDBY DATABASE
    1、copy 主D:oracleproduct10.2.0oradataackup*.*至备
    2、
    RMAN target sys/oracle@fmp
    connect auxiliary sys/oracle@fmp_std
    duplicate target database for standby nofilenamecheck;
     [dorecover];

    五、PRIMARY DATABASE
    ALTER SYSTEM SET log_archive_dest_state_2=enable;

    六、STANDBY DATABASE
    startup mount
    ALTER SYSTEM SET log_archive_dest_state_2=enable;

    --启动了MRP0进程(Managed Standby Recovery)
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
    ALTER DATABASE OPEN;

    七、检查
    --查询尚未APPLIED的archived log(STANDBY端)
    SELECT * FROM (SELECT THREAD#,SEQUENCE#,FIRST_CHANGE#,NEXT_CHANGE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY FIRST_CHANGE# DESC) WHERE ROWNUM<20;

    --察看从数据库已经归档的redo
    SELECT REGISTRAR,CREATOR,THREAD#,SEQUENCE#,APPLIED,FIRST_CHANGE#,NEXT_CHANGE# FROM V$ARCHIVED_LOG;

    --察看从数据库已经应用的redo
    SELECT THREAD#, SEQUENCE#, FIRST_CHANGE#, NEXT_CHANGE# FROM V$LOG_HISTORY;

    --DATAGUARD 状态
    ALTER SESSION SET nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
    SELECT TIMESTAMP,MESSAGE FROM V$DATAGUARD_STATUS;

    --从数据库端察看RFS(Remote File Service)接收日志情况和MRP应用日志同步主数据库情况
    COL STATUS FORMAT A15
    SELECT PROCESS,STATUS,THREAD#,SEQUENCE#,BLOCK#,BLOCKS FROM V$MANAGED_STANDBY;

    --察看从数据库是否和主数据库同步
    COL DEST_NAME FORMAT A20
    SELECT DEST_NAME,ARCHIVED_THREAD#, ARCHIVED_SEQ#, APPLIED_THREAD#, APPLIED_SEQ# FROM V$ARCHIVE_DEST_STATUS;

    col DESTINATION format a50
    SELECT DEST_NAME,DESTINATION, STATUS, ARCHIVED_THREAD#, ARCHIVED_SEQ# FROM V$ARCHIVE_DEST_STATUS WHERE STATUS <> 'DEFERRED' AND STATUS <> 'INACTIVE';

    --GAP
    SELECT * FROM V$ARCHIVE_GAP;


    第二部分 主数据库正常切换

    一 人工干预主数据库正常切换

    1 在主数据库端检验数据库可切换状态 
    SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
    SWITCHOVER_STATUS 
    ----------------- 
    TO STANDBY 
    1 row selected

    SWITCHOVER_STATUS:TO STANDBY表示可以正常切换.
    如果SWITCHOVER_STATUS的值为SESSIONS ACTIVE,表示当前有会话处于ACTIVE状态

    2 开始主数据库正常切换 IP:172.16.75.35
    如果SWITCHOVER_STATUS的值为TO STANDBY 则:
    SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY;
    如果SWITCHOVER_STATUS的值为SESSIONS ACTIVE 则:
    SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;
    成功运行这个命令后,主数据库被修改为从数据库

    3 重启先前的主数据库 
    SQL> SHUTDOWN IMMEDIATE;
    SQL> STARTUP MOUNT;

    4 在从数据库端验证可切换状态 IP:172.16.75.30
    SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE; 
    SWITCHOVER_STATUS 
    ----------------- 
    TO_PRIMARY 
    1 row selected

    5 将目标从数据库转换为主数据库 IP:172.16.75.30
    如果SWITCHOVER_STATUS的值为TO STANDBY 则:
    SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
    如果SWITCHOVER_STATUS的值为SESSIONS ACTIVE 则:
    SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
    成功运行这个命令后,从数据库被修改为主数据库

    6 重启目标从数据库
    SQL> SHUTDOWN IMMEDIATE;
    SQL> STARTUP;

    7 先前主数据库启动日志传送进程
    SQL> alter database recover managed standby database disconnect;

    ============================================================================
    --ALTER SYSTEM set log_archive_config='DG_CONFIG=(testdb,testdb2)';

    ALTER DATABASE CREATE STANDBY CONTROLFILE AS 'D:oracleproduct10.2.0oradataostonCONTROL01.CTL';

    --启动了MRP0进程(Managed Standby Recovery)
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
    ALTER DATABASE OPEN;

    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE;
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT;
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE;
    ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;


    SET PAGESIZE 200
    SELECT * FROM V$PROCESS;
    SELECT * FROM V$BGPROCESS WHERE PADDR<>'00';

    SELECT USERNAME,TERMINAL,PROGRAM FROM V$PROCESS;
    SELECT * FROM V$BGPROCESS WHERE PADDR<>'00';
    COL USERNAME FORMAT A15
    COL MACHINE FORMAT A30
    SELECT USERNAME,MACHINE,TERMINAL,PROGRAM FROM V$SESSION;

    -------

    rman nocatalog target sys/oracle@ta
    run {
    ALLOCATE CHANNEL c1 DEVICE TYPE DISK CONNECT 'sys/oracle@ta1' format '/app/oracle/backup/rac1_%U';
    ALLOCATE CHANNEL c2 DEVICE TYPE DISK CONNECT 'sys/oracle@ta2' format '/app/oracle/backup/rac2_%U';
    sql 'alter system archive log current';
    backup filesperset 3 archivelog all delete all input;
    release channel c1;
    release channel c2;
    }


    rman nocatalog target sys/oracle@ta
    run {
    ALLOCATE CHANNEL c1 DEVICE TYPE DISK CONNECT 'sys/oracle@ta1';
    ALLOCATE CHANNEL c2 DEVICE TYPE DISK CONNECT 'sys/oracle@ta2';
    BACKUP FORMAT '/app/oracle/backup/%t_%U' DATABASE PLUS ARCHIVELOG;
    BACKUP FORMAT '/app/oracle/backup/%t_%U' CURRENT CONTROLFILE FOR STANDBY;
    release channel c1;
    release channel c2;
    }


    -----RAC环境下,交叉验证-----------------------------------------
    allocate channel for maintenance device type DISK connect 'sys/oracle@ora921';
    allocate channel for maintenance device type DISK connect 'sys/oracle@ora922';
    crosscheck archivelog all;
    delete noprompt expired archivelog all;
    release channel;

    crosscheck backup;
    delete noprompt expired backup;
    delete noprompt obsolete;
    release channel;
    -------------------------------------------------------------------------

    RMAN> 
    catalog archivelog '/arch/ora92/2_1.dbf';
    list archivelog all;


    scp -p -r oracle@192.168.192.71:/app/oracle/backup /app/oracle
    scp -p -r oracle@192.168.192.72:/app/oracle/backup /app/oracle


    orapwd file=/app/oracle/product/10.2.0/db_1/dbs/orapwta password=oracle force=y


    ALTER SYSTEM set DB_FILE_NAME_CONVERT='ta','ta' SCOPE=SPFILE;
    ALTER SYSTEM set LOG_FILE_NAME_CONVERT='ta','ta' SCOPE=SPFILE;


    rman target sys/oracle@ta
    connect auxiliary sys/oracle@ta_std
    duplicate target database for standby nofilenamecheck;

    --primary
    ALTER SYSTEM SET log_archive_config='dg_config=(ta,ta_std)' sid='*';
    ALTER SYSTEM SET log_archive_dest_1='location=/app/oracle/arch' sid='*';
    ALTER SYSTEM set LOG_ARCHIVE_DEST_2='SERVICE=ta_std REOPEN=300  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLES) db_unique_name=ta_std' sid='*';
    ALTER SYSTEM SET log_archive_dest_state_1=enable sid='*';
    ALTER SYSTEM SET log_archive_dest_state_2=defer sid='*';
    ALTER SYSTEM SET standby_file_management=auto sid='*'

    --standby
    ALTER SYSTEM SET log_archive_config='dg_config=(ta,ta_std)';
    ALTER SYSTEM SET standby_file_management=auto;
    ALTER SYSTEM SET fal_server='ta1','ta2';
    ALTER SYSTEM SET fal_client='ta_std';

    ALTER SYSTEM SET log_archive_dest_state_2=enable sid='*';


    ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
    recover managed standby database finish;


    通过故障切换实现角色转换
    1、查询备用数据中v$archive_gap视图来定位备用数据库中的日志缺失。
    2、在备用数据库中通过下面的命令对归档日志进行注册
       alter database register physical logfile '......';
    3、alter database recover managed standby database finish;
       alter database recover managed standby database finish skip standby logfile;
    4、alter database commit to switchover to primary;
    5、shutdown immediate;
       startup;

    fmpdb =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.20.1)(PORT = 1521))
          (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.20.2)(PORT = 1521))
          (LOAD_BALANCE = yes)
        )
        (CONNECT_DAfmpdb =
          (SERVICE_NAME = fmpdb)
           (FAILOVER_MODE =
            (TYPE = SELECT)
            (METHOD = BASIC)
            (RETRIES = 180)
            (DELAY = 5)
          )
        )
      )


    fmpdb1 =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.20.1)(PORT = 1521))
        )
        (CONNECT_DAfmpdb =
          (SERVICE_NAME = fmpdb)
        )
      )


    fmpdb2 =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.20.2)(PORT = 1521))
        )
        (CONNECT_DAfmpdb =
          (SERVICE_NAME = fmpdb)
        )
      )

    fmpdb_std =
      (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.18.0.11)(PORT = 1521))
        (CONNECT_DAfmpdb =
          (SERVER = DEDICATED)
          (SERVICE_NAME = fmpdb)
        )
      )

    -------------------------------------------------------------------------------
    http://hi.baidu.com/dbaeyes/blog/item/740c3bfb3e0d5361034f568e.html


    standby 管理和维护2008年07月13日 星期日 00:31管理:

    查询库状态:
    SQL> select name,SWITCHOVER_STATUS,open_mode,PROTECTION_MODE,DATABASE_ROLE
              from v$database;

    验证archive log是否接收和 applied.
    SQL> alter session set nls_datE_format='YYYY-MM-DD HH24:MI:SS';

    select SEQUENCE#,DEST_ID,ARCHIVED,APPLIED,DELETED,
    STATUS,FIRST_TIME from v$archived_log order by SEQUENCE#;

    启动到管理模式:
    SQL> shutdown immediate;
    SQL> startup nomount
    SQL> alter database mount standby database
    SQL> recover managed standby database disconnect from session;
    ----------

    SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;

    10g以后,采用联机日志的传递传递方式,
    SQL> recover managed standby database using current logfile disconnect from session;

    启动到只读模式:
    SQL> shutdown immediate;
    SQL> startup nomount
    SQL> alter database mount standby database
    SQL> alter databae open read only;

    在管理模式恢复到只读模式:
    SQL> recover managed standby database cancel;
    SQL> alter database open read only

    在只读模式可以给数据库添加临时数据文件(在rman备份是没有备份的),想让standby提供只读服务或者切换成主库,最好先增加链式文件
    SQL> alter tablespace temp add tempfile 'C:oracleproduct10.2.0oradatadgtest emp02.dbf' size 100m;

    只读模式到管理模式
    SQL> recover managed standby database disconnect from session;

    在备库进行备份
    1.停止应用程序,跳转到 read only模式,同坐backup database命令来备份数据库,这样数据库处于一致性模式
    2. 备份完成后,备份控制文件
    SQL> ALTER DATABASE BACKUP CONTROLFILE TO 'FILE NAME';

    主备切换

    正常切换 (switchover)
    切换准备: 准备参数文件,平时应该备好,注意参数 fal_server,fal_client

    先确认能否转换:
    SELECT SWITCHOVER_STATUS FROM V$DATABASE;

    主 -> 备
    $lsnrclt stop 
    杀光进程或者重启数据库
    SQL> alter database commit to switchover to physical standby with session shutdown;

    SQL> shutdown immediate;
    SQL> startup nomount pfile ='';
    SQL> alter database mount standby database;
    SQL> recover managed standby database disconnect;

    备 -> 主
    SQL> alter database commit to switchover to primary with session shutdown;
    SQL> shutdown immediate
    SQL> startup pfile =''

    如果考虑在主备库来回切换,要保证数据库版本一致,参数compatible一致

    失败切换 (Failover)
    主服务器不能使用情况下

    在备库
    SQL> recover managed standby database cancel;

    --如果在备用库上有备用库日志文件,参考命令
    SQL> alter database recover managed standby database finish; -- [force|wait|nowait] 10g or later
    --没有备库日志文件[10gR2之前]
    SQL> alter database recover managed standby database finish skip standby logfile;

    --切换备库到主库
    SQL> alter database commit to switchover to primary;
    -- open
    SQL> shutdown immediate;
    SQL> startup pfile=''

    如果还有为传递的归档日志或者主库的联机日志
    SQL>alter database register logfile 'c:...'
    SQL>recover standby database;

    强行切换(激活)
    激活备用服务器,在重启数据库时,备库会resetlog。
    SQL>alter system archive log current;
    SQL>recover managed standby database cancel;
    SQL>alter database activate standby database;
    SQL>shutdown immediate;
    SQL>startup pfile=''

    -----
    apply 主库redolog并强制切换
    SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
    --选定redolog恢复
    SQL> recover standby database until cancel;

    Specify log: { =suggested | filename | AUTO | CANCEL} 
    /u01/oradata/dgtest/redo02.log 
    Log applied. 
    Media recovery complete.

    recovery 完成之后就要failover了。

    SQL>alter database activate standby database;
    SQL>shutdown immediate;
    SQL>startup pfile=''
    -- http://hi.baidu.com/wa0362/blog/item/33e52912a91eadcbc2fd78b9.html
    -- http://zf_wu.itpub.net/

    -- 参考来源: 《oracle高可用环境》

    --http://www.oracleblog.cn/study-note/reduce-the-data-loss-of-data-guard-when-failover/

    相关视图

    v$archive_dest 
    v$archive_dest_status
    v$log_history
    v$archvied_log
    v$managed_standby
    v$archive_gap

    -- EOF --

    http://oracle.chinaitlab.com/serial/740174.html

    激活备库到read/write状态 
    SQL> alter database activate standby database;
    数据库已更改。
    SQL> alter database open;
    数据库已更改。


    转换成备库 
    SQL> alter database convert to physical standby;
    数据库已更改。

    -- EOF -- 

  • 相关阅读:
    260.java风格简介
    259.Java标识符和注释
    258.计算机网络的性能指标
    257.计算机网络的组成
    256.常见的网络拓扑结构
    HDFS文件的读写流程入门学习
    Servlet入门
    Linux下扩展根分区-非LVM
    Tomcat基本使用
    JDBC入门
  • 原文地址:https://www.cnblogs.com/yaoyangding/p/15089600.html
Copyright © 2020-2023  润新知