CentOS 5.11下Oracle 11G R2 DataGuard搭建

來源:互聯網
上載者:User

CentOS 5.11下Oracle 11G R2 DataGuard搭建

Datagard算是Oracle企業版的一種容災方案,在企業中廣泛應用,我就將搭建過程記錄下來以作備用。
主機名稱    資料庫版本    執行個體名    IP
db1    Oracle 11G R2    member    172.16.1.250
db2    Oracle 11G R2    member    172.16.1.251
預設情況下以上都已經安裝好了Oracle資料庫,但是只在db1上建立了資料庫和監聽,db2隻安裝Oracle軟體不建庫,不建監聽。

目錄:

開啟強制歸檔日誌

增加standby日誌組

修改主備啟動參數

密碼檔案的處理

修改監聽

複製監聽檔案、參數檔案、密碼檔案到備庫

建立備庫控制檔案

複製主庫資料檔案和記錄檔到備庫

初始化及配置備庫做standby

DataGuard測試

主備切換測試

基礎工作:
    a.安裝CentOS 5.11 x86_64,關閉selinux,iptables,自動對時
    b.安裝Oracle 11G R2,db1安裝軟體、監聽及建庫,db2隻安裝軟體不建庫
    可以參考:  與

在db1的/etc/hosts裡增加
127.0.0.1      db1
172.16.1.251    db2
在db2的/etc/hosts裡增加
127.0.0.1      db2
172.16.1.250    db1

開啟強制歸檔(db1)

(db1)SQL >shutdown immediate;

(db1)SQL >startup mount;
(db1)SQL >alter database force logging;
(db1)SQL >alter database archivelog;
2.建立重做日誌組(必須要比原來的redo log多一組或多組,standby redo log是使用Real Time Apply的必要條件)
(db1)SQL> select group#,member from v$logfile;
    GROUP#    MEMBER
-------------  -----------------------------------------------------------------
    3    /opt/oracle/oradata/member/redo03.log
    2    /opt/oracle/oradata/member/redo02.log
    1    /opt/oracle/oradata/member/redo01.log
從上面可以看出現在已經有3組redo log.
(db1)SQL >alter database add standby logfile ('/opt/oracle/oradata/member/standby04.log') size 50m;
(db1)SQL >alter database add standby logfile ('/opt/oracle/oradata/member/standby05.log') size 50m;
(db1)SQL >alter database add standby logfile ('/opt/oracle/oradata/member/standby06.log') size 50m;
(db1)SQL >alter database add standby logfile ('/opt/oracle/oradata/member/standby07.log') size 50m;
再查下日誌組是否建立成功
(db1)SQL> select group#,member from v$logfile;
    GROUP#    MEMBER
-------------  -----------------------------------------------------------------
    3    /opt/oracle/oradata/member/redo03.log
    2    /opt/oracle/oradata/member/redo02.log
    1    /opt/oracle/oradata/member/redo01.log
    4    /opt/oracle/oradata/member/standby04.log
    5    /opt/oracle/oradata/member/standby05.log
    6    /opt/oracle/oradata/member/standby06.log
    7    /opt/oracle/oradata/member/standby07.log
可以看到我們建立的4組日誌

3.修改主備庫的啟動參數

產生參數檔案
(db1)SQL> create pfile='/tmp/member.pfile' from spfile;
退出sqlplus,用編輯器開啟/tmp/member.pfile
[oracle@db1 ~]$ vi /tmp/member.pfile
member.__db_cache_size=331350016
member.__java_pool_size=4194304
member.__large_pool_size=4194304
member.__oracle_base='/opt/oracle'#ORACLE_BASE set from environment
member.__pga_aggregate_target=339738624
member.__sga_target=503316480
member.__shared_io_pool_size=0
member.__shared_pool_size=150994944
member.__streams_pool_size=0
*.audit_file_dest='/opt/oracle/admin/member/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/opt/oracle/oradata/member/control01.ctl','/opt/oracle/flash_recovery_area/member/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='member'
*.db_recovery_file_dest='/opt/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=4070572032
*.diagnostic_dest='/opt/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=memberXDB)'
*.log_archive_format='%t_%s_%r.dbf'
*.memory_target=839909376
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
#增加一下部分
*.db_unique_name='db1'
*.archive_lag_target=1800
*.fal_client='db1'
*.fal_server='db2'
*.log_archive_config='DG_CONFIG=(db1,db2)'
*.log_archive_dest_1='location=/opt/oracle/flash_recovery_area/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=db1'
*.log_archive_dest_2='service=db2 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=db2'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
*.db_file_name_convert='/opt/oracle/flash_recovery_area',' /opt/oracle/flash_recovery_area'
*.log_file_name_convert='/opt/oracle/flash_recovery_area ','/opt/oracle/flash_recovery_area'


並拷貝一個作為備機db2的啟動參數檔案
[oracle@db1 ~]$ cp /tmp/member.pfile /tmp/db2.pfile
[oracle@db1 ~]$ vim /tmp/db2.pfile
則將上面增加的部分修改為
*.db_unique_name='db2'
*.archive_lag_target=1800
*.fal_client='db2'
*.fal_server='db1'
*.log_archive_config='DG_CONFIG=(db1,db2)'
*.log_archive_dest_1='location=/opt/oracle/flash_recovery_area/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=db2'
*.log_archive_dest_2='service=db1 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=db1'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
*.db_file_name_convert='/opt/oracle/flash_recovery_area',' /opt/oracle/flash_recovery_area'
*.log_file_name_convert='/opt/oracle/flash_recovery_area ','/opt/oracle/flash_recovery_area'


在db1上以修改過的/tmp/member.pfile啟動
(db1)SQL >shutdown immediate;
(db1)SQL> startup pfile='/tmp/member.pfile' nomount;
(db1)SQL> create spfile from pfile='/tmp/member.pfile';
(db1)SQL >shutdown immediate;
(db1)SQL> startup;


4.主庫密碼檔案:
[1]存在密碼檔案
[oracle@db1 dbs]$ ls $ORACLE_HOME/dbs
hc_DBUA0.dat  hc_member.dat  init.ora  lkDB1  lkMEMBER  orapwmember  spfilemember.ora
看到上面有一個密碼檔案orapwmember,在建庫的時候預設會建立一個
=================================================================================
[2]不存在密碼檔案
如果沒有的話可以手動建立一個。
[oracle@db1 dbs]$ cd $ORACLE_HOME/dbs
[oracle@db1 dbs]$ orapwd file=orapwmember password=123456 entries=3
#注意以上需要根據SID名建立的,file=orapwSID
===================================================================================


5.修改監聽
[oracle@db1 dbs]$ cd $ORACLE_HOME/network/admin
[oracle@db1 admin]$ mv listener.ora listener.ora.default
[oracle@db1 admin]$ vim listener.ora
LISTENER =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
  )
 
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = member)
      (ORACLE_HOME = /opt/oracle/product/11.2.0/db_1)
      (SID_NAME = member)
    )
  )
#注意以上的HOST,就是HOSTNAME
[oracle@db1 admin]$ vim tnsnames.ora
MEMBER =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = member)
    )
  )
db1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = db1)
    )
  )
db2 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = db2)
    )
  )

重啟下監聽
[oracle@db1 admin]$ lsnrctl stop
[oracle@db1 admin]$ lsnrctl start

6.複製監聽檔案、參數檔案、密碼檔案到備庫
#注意一下我這邊$ORACLE_HOME
[oracle@db1 ~]$ echo $ORACLE_HOME
/opt/oracle/product/11.2.0/db_1
#傳輸啟動參數檔案
[oracle@db1 ~]$ scp /tmp/db2.pfile db2:~
#傳輸密碼檔案
[oracle@db1 ~]$ scp /opt/oracle/product/11.2.0/db_1/dbs/orapwmember db2:/opt/oracle/product/11.2.0/db_1/dbs/orapwmember
#傳輸監聽檔案
[oracle@db1 ~]$ scp -r /opt/oracle/product/11.2.0/db_1/network/admin/{listener.ora,tnsnames.ora} db2:/opt/oracle/product/11.2.0/db_1/network/admin/


在db2上修改/opt/oracle/product/11.2.0/db_1/network/admin/listener.ora,將db1修改為db2
[oracle@db2 ~]$ vim /opt/oracle/product/11.2.0/db_1/network/admin/listener.ora
LISTENER =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
  )
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = member)
      (ORACLE_HOME = /opt/oracle/product/11.2.0/db_1)
      (SID_NAME = member)
    )
  )
#只需要修改listener.ora即可,tnsnames.ora不需要動

7.主庫建立standby控制檔案,我們這邊利用scp傳送全部檔案
查看下控制檔案的路徑
(db1)SQL> select name from v$controlfile;
NAME
--------------------------------------------------------------------------------
/opt/oracle/oradata/member/control01.ctl
/opt/oracle/flash_recovery_area/member/control02.ctl

(db1)SQL> shutdown immediate;
(db1)SQL> startup mount;
(db1)SQL> alter database create standby controlfile as '/opt/oracle/oradata/member/standby.ctl';
#在/opt/oracle/oradata/member/目錄下建立standby.ctl備機控制檔案


8.複製主庫資料檔案和記錄檔到備庫
[oracle@db1 ~]$ scp -r /opt/oracle/flash_recovery_area/ /opt/oracle/admin/ /opt/oracle/diag/ /opt/oracle/oradata/ db2:/opt/oracle


9.初始化備庫
在備機上使用standby的控制檔案覆蓋原有的控制檔案,覆蓋的路徑可以通過上一步尋找控制檔案的路徑瞭解到
[oracle@db2 ~]$ cp /opt/oracle/oradata/member/standby.ctl /opt/oracle/oradata/member/control01.ctl
[oracle@db2 ~]$ cp /opt/oracle/oradata/member/standby.ctl /opt/oracle/flash_recovery_area/member/control02.ctl

使用db2.pfile之前修改過的參數檔案進行db2

(db2)SQL> startup pfile='/home/oracle/db2.pfile' nomount;
(db2)SQL> create spfile from pfile='/home/oracle/db2.pfile';
(db2)SQL> shutdown immediate;
(db2)SQL> startup nomount;
(db2)SQL> alter database mount standby database;
(db2)SQL> alter database open read only;
以下3種應用日誌的方法:(a和b選一)
a.開啟即時應用日誌,這樣在主庫插入立馬就可以在備機上尋找到
(db2)SQL> alter database recover managed standby database using current logfile disconnect from session;
b.開啟redolog應用日誌,時間較長才能查詢到
(db2)SQL> alter database recover managed standby database disconnect from session;
c.停止應用redolog,只接受日誌,不重做
(db2)SQL> alter database recover managed standby database cancel;
至此,DataGuard搭建成功,在db1上建立表並插入資料,然後在db2上進行查詢就可以查到了。一開始做的時候總是查不到資料,最後發現是上面應用日誌的方法問題。

 

10.Dataguard測試:
查看Standby管理進程
(db1)SQL> select process from v$managed_standby;
PROCESS
---------
ARCH
ARCH
ARCH
ARCH
LNS
(db2)SQL> select process from v$managed_standby;
PROCESS
---------
ARCH
ARCH
ARCH
ARCH
RFS
RFS
RFS
RFS
MRP0
以上需要看到在主機上需要有LNS進程,在備機上需要RFS進程用來接收redo日誌,MRP0進程就是負責將日誌寫入資料庫中

在db1上進行切換日誌,然後在db2上查看日誌是否正常
(db1)SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
    11
(db2)SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
    11

(db1)SQL> alter system switch logfile;
(db1)SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
    12
(db2)SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
    12

從上面可以看出日誌切換成功,DataGuard正常運行

11.主備切換測試:
db1---primary/db2---standby  ===》db2--primary/db1--standby

[oracle@db1 ~]$ lsnrctl stop
(db1)SQL> alter database commit to switchover to physical standby with session shutdown;
(db1)SQL> shutdown immediate;
(db1)SQL> startup mount;
(db1)SQL> alter database recover managed standby database using current logfile disconnect from session;
(db1)SQL> alter database open;
[oracle@db1 ~]$ lsnrctl start

(db2)SQL> alter database commit to switchover to primary;
(db2)SQL> shutdown immediate;
(db2)SQL> startup;

以上就是主備切換的流程。

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

Oracle Data Guard 重要配置參數

基於同一主機配置 Oracle 11g Data Guard

探索Oracle之11g DataGuard

Oracle Data Guard (RAC+DG) 歸檔刪除策略及指令碼

Oracle Data Guard 的角色轉換

Oracle Data Guard的日誌FAL gap問題

Oracle 11g Data Guard Error 16143 Heartbeat failed to connect to standby 處理方法

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.