Oracle dataguard配置Step by Step

一、準備工作

1.1 設置主庫的force logging模式
SQL> alter database force logging;
Database altered.

1.2 創建主庫密碼文件
$ orapwd file=orapw$ORACLE_SID password=thunis entries=4

1.3 配置主庫standby redo log,並驗證結果
SQL> alter database add standby logfile group 4('/u01/app/oradata/guard1/redo04.log') size 50m;
Database altered.

SQL> select group#, thread#, sequence#, archived, status from v$standby_log;

    GROUP#    THREAD#  SEQUENCE# ARC STATUS
---------- ---------- ---------- --- ----------
         4          0          0 YES UNASSIGNED

1.4 配置主庫初始化參數文件
    DB_NAME=guard1
    DB_UNIQUE_NAME=guard1
    LOG_ARCHIVE_CONFIG='DG_CONFIG=(guard1,guard2)'
    LOG_ARCHIVE_DEST_1='LOCATION=/u01/app/flash_recovery_area/GUARD1/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=guard1'
    LOG_ARCHIVE_DEST_2='SERVICE=guard2 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=guard2'
    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_SERVER=guard2
    FAL_CLIENT=guard1
    DB_FILE_NAME_CONVERT='guard2','guard1'
    LOG_FILE_NAME_CONVERT='/u01/app/oradata/guard2','/u01/app/oradata/guard1'
    STANDBY_FILE_MANAGEMENT=AUTO

1.5 確認主庫的歸檔模式
SQL> startup mount
ORACLE instance started.

Total System Global Area  104857600 bytes
Fixed Size                  1217980 bytes
Variable Size              96471620 bytes
Database Buffers            4194304 bytes
Redo Buffers                2973696 bytes
Database mounted.

SQL> alter database archivelog;
Database altered.

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     23
Next log sequence to archive   25
Current log sequence           25

二、創建主庫相關文件的備份

2.1 創建主庫數據文件的物理備份(這裏採用tar方式)
$ cd /u01/app/oradata
$ tar -cvf guard1.tar guard1
guard1/
guard1/temp01.dbf
guard1/control01.ctl
guard1/system01.dbf
guard1/sysaux01.dbf
guard1/control02.ctl
guard1/undotbs01.dbf
guard1/redo03.log
guard1/redo01.log
guard1/redo02.log
guard1/control03.ctl
guard1/example01.dbf
guard1/users01.dbf
guard1/redo04.log
[oracle@test01 oradata]$ ll *tar
-rw-r--r--  1 oracle dba 1133783040 Oct 28 16:48 guard1.tar

2.2 爲備庫創建控制文件
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/tmp/control01.ctl';

2.3 爲備庫創建初始化參數文件
SQL> CREATE PFILE='/tmp/initguard1.ora' FROM SPFILE;

2.4 將tar包以及主庫控制文件和參數文件ftp到備庫
# ftp 192.168.0.88
Connected to 192.168.0.88.
220 (vsFTPd 2.0.1)
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (192.168.0.88:root): oracle
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /u01/app/oradata
250 Directory successfully changed.
ftp> pwd
257 "/u01/app/oradata"
ftp> lcd /home/oracle
Local directory now /home/oracle
ftp> get guard1.tar
local: guard1.tar remote: guard1.tar
227 Entering Passive Mode (192,168,0,88,240,125)
150 Opening BINARY mode data connection for guard1.tar (1133783040 bytes).

226 File send OK.
1133783040 bytes received in 2.6e+02 seconds (4.3e+03 Kbytes/s)

ftp> cd /tmp
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,0,88,31,48)
150 Here comes the directory listing.
-rw-------    1 500      500             0 Oct 28 08:07 6GXyHcjG4Y
-rw-rw-rw-    1 500      500             0 Oct 28 08:05 EM_TARGET_INSTALLER.lk
-rw-r-----    1 500      500            20 Oct 28 07:55 LinuxVendor_output.txt
-rw-r-----    1 500      500       6832128 Oct 28 08:52 control.ctl
-rw-r-----    1 500      500           444 Oct 28 07:55 cpuinfo.txt
-rw-------    1 500      500             0 Oct 28 08:07 cyzsdX1KWR
-rw-r-----    1 500      500            18 Oct 28 07:55 glibc.txt
-rw-r--r--    1 500      500           881 Oct 28 08:57 initguard1.ora
drwx------    2 0        0            4096 Sep 27 01:52 keyring-606Cr9
srwxr-xr-x    1 0        0               0 Oct 28 07:52 mapping-root
-rw-r-----    1 500      500         15998 Oct 28 07:55 pkginfo.txt
-rw-r-----    1 500      500           100 Oct 28 07:55 swapinfo.txt
-rw-r-----    1 500      500            11 Oct 28 07:55 tmpFileKernelParms.txt
226 Directory send OK.
ftp> mget *ctl
mget control.ctl? y
227 Entering Passive Mode (192,168,0,88,220,219)
150 Opening BINARY mode data connection for control.ctl (6832128 bytes).
226 File send OK.
6832128 bytes received in 0.83 seconds (8e+03 Kbytes/s)
ftp> mget *ora
mget initguard1.ora? y
227 Entering Passive Mode (192,168,0,88,99,183)
150 Opening BINARY mode data connection for initguard1.ora (881 bytes).
226 File send OK.
881 bytes received in 0.0042 seconds (2.1e+02 Kbytes/s)
ftp> bye
221 Goodbye.

三、設置備庫

3.1 解壓主庫數據文件到備庫相應位置
$ cd $ORACLE_BASE/oradata
$ tar -xvf guard1.tar
guard1/
guard1/temp01.dbf
guard1/control01.ctl
guard1/system01.dbf
guard1/sysaux01.dbf
guard1/control02.ctl
guard1/undotbs01.dbf
guard1/redo03.log
guard1/redo01.log
guard1/redo02.log
guard1/control03.ctl
guard1/example01.dbf
guard1/users01.dbf
guard1/redo04.log

$ mv guard1 guard2
$ cp /tmp/control01.ctl $ORACLE_BASE/oradata/guard1
$ cp /tmp/initguard2.ora $ORACLE_HOME/dbs

3.2 修改參數文件
--參照前面主庫參數文件的配置,值得一提的是db_name參數不能修改,保持和主庫一致

3.3 創建相關目錄
$ cd $ORACLE_BASE/admin
$ mkdir guard2
$ cd guard2
$ mkdir adump bdump cdump udump

3.4 創建備庫密碼文件
$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=thunis entries=4

四、創建主備庫監聽及tnsnames文件

4.1 監聽文件
$ cat listener.ora

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = test)
      (SID_NAME = test)
    )
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (PROGRAM = extproc)
    )
  )

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

4.2 tnsnames.ora文件
$ cat tnsnames.ora

GUARD2 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.99)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = guard2)
    )
  )

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )

4.3 分別啓動主備庫監聽

五、測試主備庫之間的網絡連通性

5.1 tnsping測試
$ hostname
test02

$ tnsping guard1
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 28-OCT-2008 21:14:18
Copyright (c) 1997, 2005, Oracle.  All rights reserved.

Used parameter files:
/u01/app/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.88)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = guard1)))
OK (30 msec)

$ hostname
test01

$ tnsping guard2
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 28-OCT-2008 21:14:18
Copyright (c) 1997, 2005, Oracle.  All rights reserved.

Used parameter files:
/u01/app/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.89)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = guard1)))
OK (30 msec)

5.2 sqlplus登錄測試
[oracle@test02 guard2]$ sqlplus 'sys/thunis@guard1' as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Tue Oct 28 22:24:59 2008
Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL>

[oracle@test01 guard1]$ sqlplus 'sys/thunis@guard2' as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Tue Oct 28 22:24:59 2008
Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL>

六、啓動備用數據庫

SQL> startup nomount;
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1218316 bytes
Variable Size              62916852 bytes
Database Buffers          100663296 bytes
Redo Buffers                2973696 bytes

SQL> alter database mount standby database;
Database altered.

SQL> alter database recover managed standby database disconnect from session;
Database altered.

--至此,一個完整的physical standby database就配置完成了。

七、驗證歸檔日誌應用情況

7.1 通過數據字典視圖驗證
--主庫執行

SQL> startup
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1218316 bytes
Variable Size              67111156 bytes
Database Buffers           96468992 bytes
Redo Buffers                2973696 bytes
Database mounted.
Database opened.

SQL> select sequence#, first_time, next_time, applied, fal from v$archived_log order by sequence#;

 SEQUENCE# FIRST_TIM NEXT_TIME APP FAL
---------- --------- --------- --- ---
......
         6 28-OCT-08 28-OCT-08 NO  NO
         6 28-OCT-08 28-OCT-08 YES YES
         7 28-OCT-08 28-OCT-08 NO  NO

 SEQUENCE# FIRST_TIM NEXT_TIME APP FAL
---------- --------- --------- --- ---
         7 28-OCT-08 28-OCT-08 YES YES
         8 28-OCT-08 28-OCT-08 YES YES
         8 28-OCT-08 28-OCT-08 NO  NO
         9 28-OCT-08 28-OCT-08 NO  NO
         9 28-OCT-08 28-OCT-08 YES YES

16 rows selected.

7.2 查看告警日誌文件
--備庫上有類似的信息
Tue Oct 28 22:50:22 2008
Attempt to start background Managed Standby Recovery process (guard2)
MRP0 started with pid=10, OS id=7262
Tue Oct 28 22:50:22 2008
MRP0: Background Managed Standby Recovery process started (guard2)
Managed Standby Recovery not using Real Time Apply
Media Recovery Waiting for thread 1 sequence 13 (in transit)
Tue Oct 28 22:50:28 2008
Completed: alter database recover managed standby database disconnect from session
Tue Oct 28 22:51:22 2008
idle dispatcher 'D000' terminated, pid = (13, 1)
Tue Oct 28 22:52:08 2008
RFS[2]: Possible network disconnect with primary database
Tue Oct 28 22:52:12 2008
Redo Shipping Client Connected as PUBLIC
-- Connected User is Valid
RFS[3]: Assigned to RFS process 7268
RFS[3]: Identified database type as 'physical standby'
RFS[3]: Successfully opened standby log 4: '/u01/app/oradata/guard2/redo04.log'
Redo Shipping Client Connected as PUBLIC
-- Connected User is Valid
RFS[4]: Assigned to RFS process 7270
RFS[4]: Identified database type as 'physical standby'
RFS[4]: Successfully opened standby log 4: '/u01/app/oradata/guard2/redo04.log'
Tue Oct 28 22:52:17 2008
Media Recovery Log /u01/app/flash_recovery_area/GUARD2/arch/1_13_669311857.dbf

八、主備庫切換

8.1 將主庫切換到備用狀態
SQL> alter database commit to switchover to physical standby;
Database altered.

SQL> shutdown immediate
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
SQL> startup nomount;
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1218316 bytes
Variable Size              67111156 bytes
Database Buffers           96468992 bytes
Redo Buffers                2973696 bytes

SQL> alter database mount standby database;
Database altered.

SQL> select name,open_mode,protection_mode,database_role from v$database;

NAME      OPEN_MODE  PROTECTION_MODE      DATABASE_ROLE
--------- ---------- -------------------- ----------------
GUARD1    MOUNTED    MAXIMUM PERFORMANCE  PHYSICAL STANDBY

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

Database altered.

8.2 將備庫轉換成主庫模式
SQL> alter database commit to switchover to primary;
Database altered.

SQL> shutdown immediate
ORA-01507: database not mounted

ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1218316 bytes
Variable Size              62916852 bytes
Database Buffers          100663296 bytes
Redo Buffers                2973696 bytes
Database mounted.
Database opened.

8.3 查看主備庫告警日誌文件

九、數據驗證

最後,我們在主庫上創建一個表並插入一些數據,然後模擬日誌切換,最後在備庫上驗證數據的傳輸情況。

9.1 主庫上建測試表
SQL> create table randy (id number);
Table created.

SQL> insert into randy values(1);
1 row created.

SQL> /
1 row created.

SQL> /
1 row created.

SQL> commit;
Commit complete.

SQL> select * from randy;

        ID
----------
         1
         1
         1

9.2 主庫上模擬日誌切換
SQL> alter system archive log current;

System altered.

9.3 查看備庫告警日誌文件
Wed Oct 29 14:56:09 2008
Fetching gap sequence in thread 1, gap sequence 27-27
Wed Oct 29 14:56:31 2008
RFS[3]: Archived Log: '/u01/app/flash_recovery_area/GUARD2/arch/1_27_669311857.dbf'
Wed Oct 29 14:56:39 2008
Media Recovery Log /u01/app/flash_recovery_area/GUARD2/arch/1_27_669311857.dbf
Media Recovery Waiting for thread 1 sequence 28 (in transit)

--可以發現主庫的數據已經傳送過來。

9.4 以只讀模式打開備庫,驗證數據
SQL> alter database recover managed standby database cancel;

Database altered.

SQL> alter database open read only;

Database altered.

SQL> select * from randy;

        ID
----------
         1
         1
         1

9.5 恢復備庫的standby狀態
SQL> alter database recover managed standby database disconnect from session;
Database altered.


--End--

發佈了52 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章