建立測試物理Standby日誌

aluocp發表於2008-09-18

作業系統: Windows
Oracle版本:10.2.0.1

主庫SID: ALEX (192.168.217.1)
從庫SID: MYDG (192.168.217.201)


一、建立過程:

1、在主庫執行force logging
SQL> alter database force logging;


2、設定主庫的引數檔案和從庫的引數檔案:
主庫確認以下引數:
--主庫必要
db_name='alex'
DB_UNIQUE_NAME='alex'
SERVICE_NAMES='alex'
INSTANCE_NAME='alex'
control_files='D:/oracle/oradata/alex/control01.ctl','D:/oracle/oradata/alex/control02.ctl','D:/oracle/oradata/alex/control03.ctl'
LOG_ARCHIVE_CONFIG='DG_CONFIG=(alex,mydg)'
LOG_ARCHIVE_DEST_1='LOCATION=D:/oracle/oradata/alex/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=alex'
LOG_ARCHIVE_DEST_2='SERVICE=MYDG LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=mydg'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT='%t_%s_%r.arc'
LOG_ARCHIVE_MAX_PROCESSES=30
--主庫備庫可互換
FAL_CLIENT='alex'
FAL_SERVER='mydg'
STANDBY_FILE_MANAGEMENT='AUTO'
STANDBY_ARCHIVE_DEST='D:/oracle/oradata/alex/standbyarchive'
--主備庫不同路徑選要
--DB_FILE_NAME_CONVERT='D:/oracle/oradata/mydg','D:/oracle/oradata/alex'
--LOG_FILE_NAME_CONVERT='D:/oracle/oradata/mydg/archive','D:/oracle/oradata/alex/archive'

[@more@]

從庫確認以下引數:
--備庫必要
db_name='alex'
DB_UNIQUE_NAME='mydg'
SERVICE_NAMES='mydg'
INSTANCE_NAME='mydg'
control_files='D:/oracle/oradata/alex/control01.ctl','D:/oracle/oradata/alex/control02.ctl','D:/oracle/oradata/alex/control03.ctl'
LOG_ARCHIVE_CONFIG='DG_CONFIG=(alex,mydg)'
LOG_ARCHIVE_DEST_1='LOCATION=D:/oracle/oradata/alex/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=mydg'
LOG_ARCHIVE_DEST_2='SERVICE=alex LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=alex'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT='%t_%s_%r.arc'
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_CLIENT='mydg'
FAL_SERVER='alex'
STANDBY_FILE_MANAGEMENT='AUTO'
STANDBY_ARCHIVE_DEST='D:/oracle/oradata/alex/standbyarchive'
--主備庫不同路徑選要
--DB_FILE_NAME_CONVERT='D:/oracle/oradata/alex','D:/oracle/oradata/mydg'
--LOG_FILE_NAME_CONVERT='D:/oracle/oradata/alex/archive','D:/oracle/oradata/mydg/archive'


3、在主庫開啟archivelog和flashback:
SQL> create spfile from pfile;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database flashback on;
SQL> alter database open;


4、在主庫新增standby redo log:
考慮新增的公式:(maximum number of logfiles for each thread + 1) * maximum number of threads
standby redo log至少比online redo log多一組
SQL> alter database add standby LOGFILE GROUP 4 ('D:/ORACLE/ORADATA/ALEX/REDO04.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 5 ('D:/ORACLE/ORADATA/ALEX/REDO05.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 6 ('D:/ORACLE/ORADATA/ALEX/REDO06.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 7 ('D:/ORACLE/ORADATA/ALEX/REDO07.log') size 20m;


5、RMAN備份主庫並將檔案轉到備機:
rman target /
RMAN> backup device type disk format 'd:/full_%U' database plus archivelog;
RMAN> backup device type disk format 'd:/ctrl_%U' current controlfile for standby;


6、配置主庫和備庫的的listener.ora和tnsnames.ora:
--主庫listener.ora的配置
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = alex)
(ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
(SID_NAME = alex)
)
)

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

--主庫tnsnames.ora的配置
ALEX =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = alex)
)
)

MYDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.201)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = alex)
)
)

LISTENER_ALEX =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.1)(PORT = 1521))

--備庫listener.ora的配置
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = alex)
(ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
(SID_NAME = mydg)
)
)

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

--備庫tnsnames.ora的配置
ALEX =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = alex)
)
)

MYDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.201)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = mydg)
)
)

LISTENER_MYDG =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.201)(PORT = 1521))


7、建立從庫的服務和密碼檔案:
cmd> oradim -new -sid MYDG -intpwd alex -startmode m


8、在主機上用RMAN恢復從庫
$ rman target /
RMAN> connect auxiliary
RMAN> duplicate target database for standby nofilenamecheck;


9、在從庫執行:
SQL> alter database mount standby database;
alter database mount standby database
*
ERROR at line 1:
ORA-01100: database already mounted

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


10、在主庫切換一次日誌,使備庫中記錄的原主庫的standby redo log資訊被清除:
SQL> alter system switch logfile;
稍等一會,等在主庫中查詢v$standby_log沒有原來主庫standby log的資訊返回


11、在從庫建立standby redo log
SQL> alter database recover managed standby database cancel;
SQL> alter database add standby LOGFILE GROUP 4 ('D:/ORACLE/ORADATA/ALEX/REDO04.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 5 ('D:/ORACLE/ORADATA/ALEX/REDO05.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 6 ('D:/ORACLE/ORADATA/ALEX/REDO06.log') size 20m;
SQL> alter database add standby LOGFILE GROUP 7 ('D:/ORACLE/ORADATA/ALEX/REDO07.log') size 20m;
SQL> alter database recover managed standby database disconnect from session;


12、檢查主庫和從庫的狀態:
檢查資料庫角色和狀態
SQL> select database_role, switchover_status from v$database;

檢查archive log的應用狀態
SQL> SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME, APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;

檢查archive log的遺失
SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

檢查archive log的路徑是否可用
SQL> select dest_name,status,error from v$archive_dest;


二、測試過程

1、模式切換
1.1 啟動standby database到recover manage模式
SQL>alter database recover managed standby database disconnect from session;

1.2 啟動到實時日誌應用模式
SQL>alter database recover managed standby database using current logfile;
(之前需要先建立standby logfile,使用命令,同時還需要修改主庫的引數LOG_ARCHIVE_DEST_2= 'SERVICE=MYDG LGWR')

1.3 切換standby database到read only模式
SQL>alter database recover managed standby database cancel;
SQL>alter database open read only;

1.4 切換回recover manage模式
SQL> alter database recover managed standby database disconnect from session;


2、Switchover
2.1 在主庫上
SQL>select switchover_status from v$database;

2.2 switchover_status的值如果是To standby,可以直接switchover:
SQL> alter database commit to switchover to physical standby;
SQL> shutdowm immediate;
SQL> startup mount;

如果是sessions active,則需要在switchover的命令後面加上with session shutdown:
SQL> alter database commit to switchover to physical standby with session shutdown;
SQL> shutdowm immediate;
SQL> startup mount;

2.3 在目標從庫上
SQL> select switchover_status from v$database
switchover_status的值入股是To primary,可以直接switchover:
SQL> alter database commit to switchover to primary;
SQL> shutdown immediate;
SQL> startup;

如果是sessions active,則需要在switchover的命令後面加上with session shutdown:
SQL> alter database commit to switchover to primary with session shutdown;
SQL> shutdown immediate;
SQL> startup;


3、Failover
3.1 在目標從庫上,檢查遺失的archive log:
SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

3.2 如有遺失,Copy到從庫目錄並應用:
SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE 'filespec1';

3.3 再次檢查遺失的archive log:
SQL> SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST FROM V$ARCHIVED_LOG;

3.4 關閉從庫的recover模式:
SQL> alter database recover managed standby database finish;
OR
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH FORCE;
OR
SQL> alter database recover managed standby database finish skip standby logfile;

3.5 切換從庫為主庫:
SQL> alter database commit to switchover to primary;
SQL> shutdown immediate;
SQL> startup;


4、使用Flashback恢復failover的主庫
4.1 在新的主庫上查詢切換時候的SCN號:
SQL> SELECT standby_became_primary_scn FROM v$database;

4.2 恢復舊主庫後,Mount:
SQL> STARTUP MOUNT;

4.3 Flashback舊主庫到步驟4.1查詢得的SCN:
SQL> FLASHBACK DATABASE TO SCN ;

4.4 在舊主庫上,轉換control file為standby control file:
SQL> ALTER DATABASE CONVERT TO PHYSICAL STANDBY;

4.5 恢復舊主庫為新從庫:
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;

4.6 在新主庫上,設定日誌傳輸到舊主庫(新從庫),並確認狀態:
SQL> SELECT DEST_ID, DEST_NAME, STATUS, PROTECTION_MODE,DESTINATION, ERROR, SRL FROM V$ARCHIVE_DEST_STATUS;

4.7 在新主庫上,歸檔日誌到新從庫:
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;

4.8 在新從庫上開啟recover模式:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
如果是real-time應用:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT;

追加flashback開啟Dataguard:

操作前:
備庫:
alter database recover managed standby database cancel;
create restore point restore_point_test guarantee flashback database;
主庫:
alter system archive log current;
alter system set log_archive_dest_state_3=defer;
備庫:
alter database activate standby database;
alter database open;

操作後:
備庫:
startup mount force;
flashback database to restore point restore_point_test;
alter database convert to physical standby;
startup mount force;
alter database recover managed standby database disconnect from session;
主庫:
alter system set log_archive_dest_state_3=enable;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7319461/viewspace-1010797/,如需轉載,請註明出處,否則將追究法律責任。

相關文章