序言:
DATAGUARD是通過建立一個PRIMARY和STANDBY組來確立其參照關系。
STANDBY一旦創建,DATAGUARD就會通過將主數據庫(PRIMARY)的REDO傳遞給STANDBY數據庫,然后在STANDBY中應用REDO實現數據庫的同步。
有兩種類型的STANDBY:物理STANDBY和邏輯STANDBY
物理STANDBY提供與主數據庫完全一樣的拷貝(塊到塊),數據庫SCHEMA,包括索引都是一樣的。它是直接應用REDO實現同步的。
邏輯STANDBY則不是這樣,在邏輯STANDBY中,邏輯信息是相同的,但物理組織和數據結構可以不同,它和主庫保持同步的方法是將接收的REDO轉換成SQL語句,然后在STANDBY上執行SQL語句。邏輯STANDBY除災難恢復外還有其它用途,比如用於用戶進行查詢和報表。
1、安裝環境
在primary搭建數據庫軟件,建立lsnrctl監聽,采用dbca搭建實例,在standby上搭建數據庫軟件,建立監聽,但是不需要采用dbca建立實例。
如何在linux上搭建oracle數據庫,請參考以前的blog實驗:http://blog.itpub.net/26230597/viewspace-1413242/
操作系統: 都是centos6.4
oracle軟件版本: oracle 11.2.0.1.0
IP地址: primary庫(192.168.121.217)、standby庫(192.168.121.218)
db_unique_name: primary庫(pdunq)、standby庫(pdunq_dg)
2、准備工作 在primary上操作
2.1、打開Forced Logging 模式
先確認primary庫處於歸檔模式
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 362
Next log sequence to archive 364
Current log sequence 364
SQL>
強制歸檔
SQL> alter database force logging;
Database altered.
SQL>
確認primary庫是歸檔模式
添加standby文件
alter database add standby logfile group 4 ('/home/oradata/powerdes/redo_dg_021.log') size 20M;
alter database add standby logfile group 5 ('/home/oradata/powerdes/redo_dg_022.log') size 20M;
alter database add standby logfile group 6 ('/home/oradata/powerdes/redo_dg_023.log') size 20M;
alter database drop standby logfile group 4;
alter database drop standby logfile group 5;
alter database drop standby logfile group 6;
select * from v$logfile order by 1;
2.3 准備參數文件
2.3.1 生成pfile
create pfile from spfile;
shutdown immediate
2.3.2 修改pfile
cp $ORACLE_HOME/dbs/initpowerdes.ora $ORACLE_HOME/dbs/initpowerdes.ora.bak
vim $ORACLE_HOME/dbs/initpowerdes.ora
*.db_unique_name=pdunq
*.diagnostic_dest='/oracle/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=powerdesXDB)'
*.fal_client='pdunq'
*.fal_server='pdunq_dg'
*.standby_file_management='AUTO'
*.db_file_name_convert='/home/oradata/powerdes','/home/oradata/pwerdes'
*.log_file_name_convert='/home/oradata/powerdes','/home/oradata/powerdes'
*.log_archive_config='DG_CONFIG=(pdunq,pdunq_dg)'
*.log_archive_dest_2='SERVICE=pdunq_dg lgwr sync affirm VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=pdunq_dg'
*.log_archive_dest_state_2='ENABLE'
2.3.3 生成spfile
create spfile from pfile;
startup #這里可以啟動也可以不啟動,這里不啟動,后面就要記得startup;讓新的參數文件生效
2.4 修改監聽文件
[oracle@powerlong4 admin]$ vim listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1)
(PROGRAM = extproc)
)
(SID_DESC =
(SID_NAME = powerdes)
(ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.217)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
ADR_BASE_LISTENER = /oracle/app/oracle
INBOUND_CONNECT_TIMEOUT_listener=10
2.5,修改tns配置文件
[oracle@powerlong4 admin]$ vim tnsnames.ora
PD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.217)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = pdunq)
)
)
SC_SID =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.218)(PORT = 1521))
)
(CONNECT_DATA =
(SID = powerdes)
(SERVER = DEDICATED)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
2.6 監聽服務重啟
lsnrctl stop
lsnrctl start
2.7 primary上配置最大可用模式:
SQL>startup
SQL>alter database set standby database to maximize availability;
2.8 備份數據庫
backup database plus archivelog;
backup current controlfile for standby;
exit;
備份結束后會在閃回區產生備份文件
3,數據庫配置 standby上
3.1 建立相應的文件目錄
包括dump文件目錄,數據文件目錄,通過show parameter dest;查看,保持和primary一樣的路徑地址
3.2 從primary上copy數據文件到standby上
在主庫上執行:
ps:在primary上執行
copy閃回區內容
copy閃回文件
cd /oracle/app/oracle/flash_recovery_area/
scp -r ./* 192.168.121.218:/oracle/app/oracle/flash_recovery_area/
copy參數文件
cd /oracle/app/oracle/product/11.2.0/dbhome_1/dbs
scp -r ./* 192.168.121.218:/oracle/app/oracle/product/11.2.0/dbhome_1/dbs
copy監聽文件
cd /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/
scp -r ./* 192.168.121.218:/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/
3.3 在standby庫 修改配置文件 在standby上修改
[oracle@powerlong5 admin]$ vim listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1)
(PROGRAM = extproc)
)
(SID_DESC =
(SID_NAME = powerdes)
(ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.218)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
在standby修改tns文件
3.4,修改參數文件
*.db_unique_name='pdunq_dg'
*.diagnostic_dest='/oracle/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=powerdes)'
*.fal_client='pdunq'
*.fal_server='pdunq_dg'
*.standby_file_management='AUTO'
*.db_file_name_convert='/home/oradata/powerdes','/home/oradata/powerdes'
*.log_file_name_convert='/home/oradata/powerdes','/home/oradata/powerdes'
*.log_archive_config='DG_CONFIG=(pdunq,pdunq_dg)'
*.log_archive_dest_2='SERVICE=pdunq_dg lgwr sync affirm VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=pdunq_dg'
*.log_archive_dest_state_2='ENABLE'
PS:將*.log_archive_dest_2=后面的DB_UNIQUE_NAME改成primary的DB_UNIQUE_NAME值改為pdunq,這樣在做switchover的時候,新的primary能通過這個將redo日志傳到新的standby上面去。
log_archive_dest_N 目的是告訴數據庫,把歸檔放到那里去可選項,首先是本地,然后考慮遠程的從庫,所以,假設A是主庫,B是從庫,切換之后B是主庫,A是從庫,所以,log_archive_dest_N需要設置為對方
3.5,重啟監聽 standby
[oracle@powerlong5 dbs]$ lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 10-FEB-2015 15:41:36
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.218)(PORT=1521)))
The command completed successfully
[oracle@powerlong5 dbs]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 10-FEB-2015 15:41:41
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Starting /oracle/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.1.0 - Production
System parameter file is /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Log messages written to /oracle/app/oracle/diag/tnslsnr/powerlong5/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.121.218)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.218)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 10-FEB-2015 15:41:41
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File /oracle/app/oracle/diag/tnslsnr/powerlong5/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.121.218)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "powerdes" has 1 instance(s).
Instance "powerdes", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@powerlong5 dbs]$
3.6,恢復數據庫
在standby庫上操作
[oracle@powerlong5 admin]$ rman target sys/syxxlxxxx58@PD1 auxiliary /
Argument Value Description
-----------------------------------------------------------------------------
target quoted-string connect-string for target database
catalog quoted-string connect-string for recovery catalog
nocatalog none if specified, then no recovery catalog
cmdfile quoted-string name of input command file
log quoted-string name of output message log file
trace quoted-string name of output debugging message log file
append none if specified, log is opened in append mode
debug optional-args activate debugging
msgno none show RMAN-nnnn prefix for all messages
send quoted-string send a command to the media manager
pipe string building block for pipe names
timeout integer number of seconds to wait for pipe input
checksyntax none check the command file for syntax errors
-----------------------------------------------------------------------------
Both single and double quotes (' or ") are accepted for a quoted-string.
Quotes are not required unless the string contains embedded white-space.
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00552: syntax error in command line arguments
RMAN-01009: syntax error: found "end-of-file": expecting one of: "double-quoted-string, identifier, single-quoted-string, "
RMAN-01007: at line 0 column 0 file: command line arguments
[oracle@powerlong5 admin]$
[oracle@powerlong5 admin]$
[oracle@powerlong5 admin]$