Oracle11g R2之Dataguard搭建物理standby

梓沐發表於2015-12-23

資料庫版本:11.2.0.4

伺服器版本:RedHat6.4

主庫Primary:安裝資料庫軟體及例項

備庫Standby: 僅安裝資料庫軟體

Database             DB_NAME      DB_UNIQUE_NAME    Oracle Net Service Name

Primary              orcl         orcl              orcl

Physical standby     orcl         dg                dg

一、搭設前主備庫環境:

主庫Primary環境變數:

[oracle@oracle dbs]$ cat /home/oracle/.bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

export TMP=/tmp

export TMPDIR=$TMP

export ORACLE_HOSTNAME=orcl

export ORACLE_UNQNAME=orcl

export ORACLE_BASE=/u01/oracle

export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1

export ORACLE_SID=orcl

export PATH=$ORACLE_HOME/bin:$PATH

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH

export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$CLASSPATH

?

備庫Standby環境變數:

[oracle@dg ~]$ cat /home/oracle/.bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

export TMP=/tmp

export TMPDIR=$TMP

export ORACLE_HOSTNAME=dg

export ORACLE_UNQNAME=dg

export ORACLE_BASE=/u01/oracle

export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1

export ORACLE_SID=dg

export PATH=$ORACLE_HOME/bin:$PATH

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH

export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$CLASSPATH

二、搭設Dataguard

主庫Primary操作:

1)檢查資料庫是否支援Dataguard

SQL> select * from v$option where parameter = 'Managed Standby';

PARAMETER           VALUE

----------------    ----------------

Managed Standby     TRUE

?

2)檢查主庫是否開啟歸檔模式

SQL> archive log list

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination            /u01/archive/orcl

Oldest online log sequence     23

Next log sequence to archive   25

Current log sequence           25

--如未開啟執行下列操作

SQL> shutdown immediate

SQL> startup mount

SQL> alter database archivelog;

SQL> alter database open;

3)將資料庫設為強制日誌模式

SQL> alter database force logging;

4)建立資料庫密碼檔案(如果沒有)

[oracle@oracle /]$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle force=y entries=5

5)建立主庫歸檔目錄

[oracle@oracle /]$ mkdir -p /u01/archive/orcl

6)備份資料庫初始化引數檔案

SQL> create pfile='/home/oracle/pfile.ora' from spfile;

7)建立pfile檔案,該檔案方便修改初始化引數

SQL> create pfile from spfile;

--開啟pfile檔案,新增如下內容

DB_UNIQUE_NAME=orcl

LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl,dg)'

LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive/orcl VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl'

LOG_ARCHIVE_DEST_2='SERVICE=dg ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg'

LOG_ARCHIVE_DEST_STATE_1=ENABLE

LOG_ARCHIVE_DEST_STATE_2=ENABLE

REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE

LOG_ARCHIVE_FORMAT=%t_%s_%r.arc

FAL_SERVER=dg

DB_FILE_NAME_CONVERT='/u01/oracle/oradata/dg/','/u01/oracle/oradata/orcl/'

LOG_FILE_NAME_CONVERT='/u01/archive/dg','/u01/archive/orcl'

STANDBY_FILE_MANAGEMENT=AUTO

8)關閉資料庫,透過pfile建立spfile後,開啟資料庫

SQL> shutdown immediate

SQL> create spfile from pfile;

SQL> startup

9)建立備份目錄,進行rman備份

[oracle@oracle /]$ mkdir -p /u01/backup

--rman備份

[oracle@oracle /]$ rman target /

RMAN> backup full database format '/u01/backup/backup_%t_%s_%p.bak';

10)在主庫上建立備庫的控制檔案(2)

SQL> alter database create standby controlfile as '/u01/backup/stdby_control01.ctl';

[oracle@oracle /]$ cd /u01/backup/

[oracle@oracle backup]$ cp stdby_control01.ctl stdby_control02.ctl

備庫Standby操作:

1)建立必要的目錄

mkdir -p /u01/archive/dg

mkdir -p /u01/oracle/admin/dg/adump

mkdir -p /u01/oracle/oradata/dg

mkdir -p /u01/oracle/oradata/orcl

mkdir -p /u01/oracle/fast_recovery_area/dg

mkdir -p /u01/backup

2)透過scp將主庫中的備份檔案傳到備庫上

[oracle@oracle backup]$ cd /u01/backup

[oracle@oracle backup]$ scp backup*.bak 192.168.8.223:/u01/backup

[oracle@oracle backup]$ scp stdby_control*.ctl 192.168.8.223:/u01/oracle/oradata/

[oracle@oracle backup]$ cd $ORACLE_HOME/dbs

[oracle@oracle dbs]$ scp initorcl.ora 192.168.8.223:$ORACLE_HOME/dbs/

傳輸好後,將控制檔案放到初始化引數中設定的目錄即可?

3)將密碼檔案從主庫scp同步過來:

[oracle@oracle dbs]$ scp $ORACLE_HOME/dbs/orapworcl 192.168.8.223:$ORACLE_HOME/dbs/

4)修改備庫初始化引數檔案(主庫初始化引數檔案為initorcl.ora需修改成initdg.ora)

將初始化檔案中新增如下內容(主庫中類似的內容刪除或進行修改,除了db_name,其他的orcl需要改成dg)

DB_UNIQUE_NAME=dg

LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl,dg)'

LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive/dg VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg'

LOG_ARCHIVE_DEST_2='SERVICE=orcl ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'

LOG_ARCHIVE_DEST_STATE_1=ENABLE

LOG_ARCHIVE_DEST_STATE_2=ENABLE

REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE

LOG_ARCHIVE_FORMAT=%t_%s_%r.arc

FAL_SERVER=orcl

DB_FILE_NAME_CONVERT='/u01/oracle/oradata/orcl/','/u01/oracle/oradata/dg/'

LOG_FILE_NAME_CONVERT='/u01/archive/orcl','/u01/archive/dg'

STANDBY_FILE_MANAGEMENT=AUTO

5)透過pfile建立spfile檔案

[oracle@dg ~]$ sqlplus / as sysdba

SQL> create spfile from pfile;

6)根據pfile中配置的控制檔案路徑及引數,將scp過來的控制檔案放置對應目錄

7)啟動物理備庫standby

SQL> startup nomount

SQL> alter database mount standby database;

8)備庫standbyrman恢復

[oracle@dg ~]$ rman target /

RMAN> restore database;
9)配置主備庫檔案(備庫沒有可以從主庫scp過來進行修改),下面僅貼出主備庫相關檔案內容

--主庫Primarylistener.ora

# listener.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/listener.ora

# Generated by Oracle configuration tools.

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))

      (ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1521))

    )

  )

ADR_BASE_LISTENER = /u01/oracle

--備庫standbylistener.ora

# listener.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/listener.ora

# Generated by Oracle configuration tools.

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))

      (ADDRESS = (PROTOCOL = TCP)(HOST = dg)(PORT = 1521))

    )

  )

ADR_BASE_LISTENER = /u01/oracle

?

--主庫Primarytnsnames.ora

# tnsnames.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora

# Generated by Oracle configuration tools.

ORCL =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = orcl)

    )

  )

DG =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.223)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = dg)

    )

  )

--備庫standbytnsnames.ora

# tnsnames.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora

# Generated by Oracle configuration tools.

DG =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = dg)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = dg)

    )

  )

ORCL =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.205)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = orcl)

    )

  )

10)完成後重啟listener

lsnrctl stop

lsnrctl start

--tnsping主備庫

tnsping orcl

tnsping dg

--tnsping時可能會有linux防火牆限制,禁用防火牆命令

service iptables stop

11)配置standby redolog(如果資料庫是最佳效能模式可以忽略,如果以後變成備庫且要轉為其它兩種模式則要建立)

--主庫Primary上建立:

SQL> alter database add standby logfile

     group 4 ('/u01/oracle/oradata/orcl/stdby_redo04.log') size 50m,

     group 5 ('/u01/oracle/oradata/orcl/stdby_redo05.log') size 50m,

     group 6 ('/u01/oracle/oradata/orcl/stdby_redo06.log') size 50m,

     group 7 ('/u01/oracle/oradata/orcl/stdby_redo07.log') size 50m;

--備庫standby上建立

SQL> alter database add standby logfile

     group 4 ('/u01/oracle/oradata/dg/stdby_redo04.log') size 50m,

     group 5 ('/u01/oracle/oradata/dg/stdby_redo05.log') size 50m,

     group 6 ('/u01/oracle/oradata/dg/stdby_redo06.log') size 50m,

     group 7 ('/u01/oracle/oradata/dg/stdby_redo07.log') size 50m;

12)在備庫standby上啟動redo apply

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

到此物理standby建立完畢!

?

一些常用命令(整理中)

1)查詢角色切換狀態

select switchover_status from v$database;

2)備庫啟動順序

startup nomount

alter database mount standby database;

alter database recover managed standby database disconnect from session;

3)從正在恢復狀態只讀開啟

alter database recover managed standby database cancel;

alter database open read only;

4)主備庫實時同步

alter database recover managed standby database using current logfile disconnect from

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

相關文章