前提條件:
完成Oracle單機搭建,Oracle軟體和資料庫安裝完成。
完成Oracle備機的Oracle軟體安裝。
可以參考文章:
http://blog.itpub.net/22996654/viewspace-2149816/
完成上述安裝後開始搭建物理DG:
主機IP:172.16.0.21
備機IP:172.16.0.75
把兩個主機都設定好/etc/hosts
[root@aa ~]# cat /etc/hosts
172.16.0.21 aa
172.16.0.75 bb
在從庫把對應主庫的所有資料目錄、日誌目錄、歸檔目錄都建立好。
[oracle@bb db_1]$ mkdir -p $ORACLE_BASE/admin/$ORACLE_SID/adump
[oracle@bb db_1]$ mkdir -p $ORACLE_BASE/oradata/WMSPROD
[oracle@bb db_1]$ mkdir -p $ORACLE_BASE/flash_recovery_area
[oracle@bb db_1]$ chown -R oracle:oinstall /usr/local/bin/*
[oracle@bb oradata]$ mkdir -p standbylog
[oracle@bb oradata]$ ll
total 8
drwxr-xr-x 2 oracle oinstall 4096 May 15 15:45 standbylog
drwxr-xr-x 2 oracle oinstall 4096 May 14 20:25 WMSPROD
設定監聽:
主庫:
[oracle@aa admin]$ cat listener.ora
# listener.ora Network Configuration File: /data/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = aa)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /data/oracle
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=wmsprod)
(ORACLE_HOME=/data/oracle/product/11.2.0/db_1)
(SID_NAME=wmsprod)
)
)
[oracle@aa admin]$ cat tnsnames.ora
WMSPROD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = aa)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = WMSPROD)
)
)
WMSPRODDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = bb)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = WMSPRODDG)
)
)
備庫:
[oracle@bb admin]$ cat listener.ora
# listener.ora Network Configuration File: /data/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = bb)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /data/oracle
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=wmsproddg)
(ORACLE_HOME=/data/oracle/product/11.2.0/db_1)
(SID_NAME=wmsproddg)
)
)
[oracle@bb admin]$ cat tnsnames.ora
WMSPRODDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = bb)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = WMSPRODDG)
)
)
WMSPROD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = aa)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = WMSPROD)
)
)
重啟監聽。
在主庫加standby的日誌檔案:比redo日誌檔案多一組。
SQL> alter database add standby logfile
2 GROUP 7 ('/data/oracle/oradata/standbylog/standby01.log') SIZE 600m,
3 GROUP 8 ('/data/oracle/oradata/standbylog/standby02.log') size 600m,
4 GROUP 9 ('/data/oracle/oradata/standbylog/standby03.log') size 600m,
5 GROUP 10 ('/data/oracle/oradata/standbylog/standby04.log') size 600m,
6 GROUP 11 ('/data/oracle/oradata/standbylog/standby05.log') size 600m,
7 GROUP 12 ('/data/oracle/oradata/standbylog/standby06.log') size 600m,
8 GROUP 13 ('/data/oracle/oradata/standbylog/standby07.log') size 600m;
Database altered.
修改主從的init檔案:
主庫:
[oracle@aa dbs]$ cat initwmsprod.ora
*.db_name='WMSPROD'
*.memory_target=5G
*.processes = 2000
*.audit_file_dest='/data/oracle/admin/wmsprod/adump'
*.audit_trail ='db'
*.db_block_size=8192
*.db_domain=''
*.db_recovery_file_dest='/data/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2G
*.diagnostic_dest='/data/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=WMSPRODXDB)'
*.open_cursors=300
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
*.control_files = (/data/oracle/oradata/WMSPROD/ora_control01.ctl,/data/oracle/oradata/WMSPROD/ora_control02.ctl)
*.compatible ='11.2.0'
*.standby_file_management='AUTO'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(wmsprod,wmsproddg)'
*.log_archive_dest_1='location=/data/oracle/oradata/WMSPROD/archivelog
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=wmsprod'
*.DB_UNIQUE_NAME=wmsprod
*.DB_FILE_NAME_CONVERT='/data/oracle/oradata/WMSPROD/','/data/oracle/oradata/WMSPROD/'
*.LOG_FILE_NAME_CONVERT='/data/oracle/oradata/WMSPROD/','/data/oracle/oradata/WMSPROD/'
*.log_archive_dest_2='SERVICE=wmsproddg LGWR ASYNC
valid_for=(online_logfiles,primary_role)
db_unique_name=wmsproddg'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER=wmsproddg
*.fal_client=wmspro
備庫:
[oracle@bb dbs]$ cat initwmsproddg.ora
*.db_name='WMSPROD'
*.memory_target=5G
*.processes = 2000
*.audit_file_dest='/data/oracle/admin/wmsprod/adump'
*.audit_trail ='db'
*.db_block_size=8192
*.db_domain=''
*.db_recovery_file_dest='/data/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2G
*.diagnostic_dest='/data/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=WMSPRODXDB)'
*.open_cursors=300
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
*.control_files = (/data/oracle/oradata/WMSPROD/ora_control01.ctl,/data/oracle/oradata/WMSPROD/ora_control02.ctl)
*.compatible ='11.2.0'
*.standby_file_management='AUTO'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(wmsprod,wmsproddg)'
*.log_archive_dest_1='location=/data/oracle/oradata/WMSPROD/archivelog
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=wmsproddg'
*.DB_UNIQUE_NAME=wmsproddg
*.DB_FILE_NAME_CONVERT='/data/oracle/oradata/WMSPROD/','/data/oracle/oradata/WMSPROD/'
*.LOG_FILE_NAME_CONVERT='/data/oracle/oradata/WMSPROD/','/data/oracle/oradata/WMSPROD/'
*.log_archive_dest_2='SERVICE=wmsprod LGWR ASYNC
valid_for=(online_logfiles,primary_role)
db_unique_name=wmsprod'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER=wmsprod
*.fal_client=wmsprodd
cp 主庫的密碼檔案生成備庫的密碼檔案並放到備庫對應位置:
[oracle@aa dbs]$ cp orapwwmsprod orapwwmsproddg
建立備庫的spfile
檔案,啟動備庫到nomount
模式
SQL> shutdown immediate;
SQL> create spfile from pfile;
SQL> startup nomount;
RMAN
複製主庫到備庫
首先從庫RMAN
連線到目標資料庫和輔助資料庫
rman target sys/password@wmsprod auxiliary sys/password@wmsproddg
使用RMAN
的duplicate
命令進行復制,兩邊目錄結構相同,需要新增nofilenamecheck
引數
RMAN> duplicate target database for standby from active database nofilenamecheck;
複製成功後,備庫自動被載入為mount
模式,進入sqlplus
檢視
SQL> select status from v$instance;
備庫執行過程:
[oracle@bb admin]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Fri May 15 18:09:13 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL> create spfile from pfile;
File created.
SQL> startup nomount;
ORACLE instance started.
Total System Global Area 5344731136 bytes
Fixed Size 2262656 bytes
Variable Size 3724544384 bytes
Database Buffers 1593835520 bytes
Redo Buffers 24088576 bytes
SQL> !
[oracle@bb admin]$ rman target sys/oracle@wmsprod auxiliary sys/oracle@wmsproddg
Recovery Manager: Release 11.2.0.4.0 - Production on Fri May 15 18:10:47 2020
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: WMSPROD (DBID=1351169222)
connected to auxiliary database: WMSPROD (not mounted)
RMAN> duplicate target database for standby from active database nofilenamecheck;
Starting Duplicate Db at 15-MAY-20
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=2376 device type=DISK
contents of Memory Script:
{
backup as copy reuse
targetfile '/data/oracle/product/11.2.0/db_1/dbs/orapwwmsprod' auxiliary format
'/data/oracle/product/11.2.0/db_1/dbs/orapwwmsproddg' ;
}
executing Memory Script
Starting backup at 15-MAY-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=96 device type=DISK
Finished backup at 15-MAY-20
contents of Memory Script:
{
backup as copy current controlfile for standby auxiliary format '/data/oracle/oradata/WMSPROD/ora_control01.ctl';
restore clone controlfile to '/data/oracle/oradata/WMSPROD/ora_control02.ctl' from
'/data/oracle/oradata/WMSPROD/ora_control01.ctl';
}
executing Memory Script
Starting backup at 15-MAY-20
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/data/oracle/product/11.2.0/db_1/dbs/snapcf_wmsprod.f tag=TAG20200515T181846 RECID=1 STAMP=1040494726
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 15-MAY-20
Starting restore at 15-MAY-20
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 15-MAY-20
contents of Memory Script:
{
sql clone 'alter database mount standby database';
}
executing Memory Script
sql statement: alter database mount standby database
contents of Memory Script:
{
set newname for tempfile 1 to
"/data/oracle/oradata/WMSPROD/temp01.dbf";
switch clone tempfile all;
set newname for datafile 1 to
"/data/oracle/oradata/WMSPROD/system01.dbf";
set newname for datafile 2 to
"/data/oracle/oradata/WMSPROD/sysaux01.dbf";
set newname for datafile 3 to
"/data/oracle/oradata/WMSPROD/undotbs01.dbf";
set newname for datafile 4 to
"/data/oracle/oradata/WMSPROD/users01.dbf";
backup as copy reuse
datafile 1 auxiliary format
"/data/oracle/oradata/WMSPROD/system01.dbf" datafile
2 auxiliary format
"/data/oracle/oradata/WMSPROD/sysaux01.dbf" datafile
3 auxiliary format
"/data/oracle/oradata/WMSPROD/undotbs01.dbf" datafile
4 auxiliary format
"/data/oracle/oradata/WMSPROD/users01.dbf" ;
sql 'alter system archive log current';
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /data/oracle/oradata/WMSPROD/temp01.dbf in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting backup at 15-MAY-20
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/data/oracle/oradata/WMSPROD/users01.dbf
output file name=/data/oracle/oradata/WMSPROD/users01.dbf tag=TAG20200515T181853
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/data/oracle/oradata/WMSPROD/system01.dbf
output file name=/data/oracle/oradata/WMSPROD/system01.dbf tag=TAG20200515T181853
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/data/oracle/oradata/WMSPROD/sysaux01.dbf
output file name=/data/oracle/oradata/WMSPROD/sysaux01.dbf tag=TAG20200515T181853
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/data/oracle/oradata/WMSPROD/undotbs01.dbf
output file name=/data/oracle/oradata/WMSPROD/undotbs01.dbf tag=TAG20200515T181853
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
Finished backup at 15-MAY-20
sql statement: alter system archive log current
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
datafile 1 switched to datafile copy
input datafile copy RECID=1 STAMP=1040494790 file name=/data/oracle/oradata/WMSPROD/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=2 STAMP=1040494790 file name=/data/oracle/oradata/WMSPROD/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=3 STAMP=1040494790 file name=/data/oracle/oradata/WMSPROD/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=4 STAMP=1040494790 file name=/data/oracle/oradata/WMSPROD/users01.dbf
Finished Duplicate Db at 15-MAY-20
檢視備庫歸檔模式:
[oracle@bb admin]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Fri May 15 18:32:46 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /data/oracle/oradata/WMSPROD/archivelog
Oldest online log sequence 7
Next log sequence to archive 0
Current log sequence 8
如果沒有建這個archivelog目錄。
會在上級目錄生成同步的歸檔日誌檔案。
主庫切日誌組:
切日誌:
SQL> alter system switch logfile
2 ;
System altered.
[oracle@aa archivelog]$ ll
total 105956
-rw-r----- 1 oracle oinstall 56715776 May 14 20:50 1_2_1040413510.dbf
-rw-r----- 1 oracle oinstall 47865344 May 15 17:33 1_3_1040413510.dbf
-rw-r----- 1 oracle oinstall 44544 May 15 17:33 1_4_1040413510.dbf
-rw-r----- 1 oracle oinstall 2842624 May 15 18:18 1_5_1040413510.dbf
-rw-r----- 1 oracle oinstall 28160 May 15 18:19 1_6_1040413510.dbf
-rw-r----- 1 oracle oinstall 151040 May 15 18:24 1_7_1040413510.dbf
-rw-r----- 1 oracle oinstall 844800 May 15 18:50 1_8_1040413510.dbf
備庫日誌同步:
[oracle@bb archivelog]$ ll
total 828
-rw-r----- 1 oracle oinstall 844800 May 15 18:50 1_8_1040413510.dbf
查備庫:
SQL> select status from v$instance;
STATUS
------------
MOUNTED
SQL> select name from v$database;
NAME
---------
WMSPROD
關閉審計:
第1步:檢視審計功能是否開啟?
SQL> show parameter audit;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
audit_file_dest string /u01/app/oracle/admin/ORCL/adump
audit_sys_operations boolean FALSE
audit_syslog_level string
audit_trail string NONE
audit_trail 的value值為NONE表示不開啟;
audit_trail 的value值為FALSE表示不開啟;
audit_trail 的value值為DB表示開啟;
audit_trail 的value值為TURE表示開啟;
audit_trail 的value值為OS表示審計記錄寫入一個作業系統檔案(不是特別理解);
第2步:開啟審計功能
SQL> alter system set audit_sys_operations=TRUE scope=spfile;--審計管理使用者(以sysdba/sysoper角色登陸)
SQL> alter system set audit_trail=db,extended scope=spfile;
重啟例項
第3步:關閉審計功能
SQL> conn /as sysdba
SQL> show parameter audit
SQL> alter system set audit_trail = none scope=spfile;
重啟例項
OK。
SQL> show parameter audit;
NAME
TYPE
VALUE
------------------------------------ ----------- ------------------------------
audit_file_dest
string
/data/oracle/admin/wmsprod/adu
mp
audit_sys_operations
boolean
FALSE
audit_syslog_level
string
audit_trail
string
DB
SQL> alter system set audit_trail = none scope=spfile;
System altered.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup;
ORACLE instance started.
Total System Global Area 5344731136 bytes
Fixed Size
2262656 bytes
Variable Size
3724544384 bytes
Database Buffers
1593835520 bytes
Redo Buffers
24088576 bytes
Database mounted.
Database opened.
SQL> show parameter audit;
NAME
TYPE
VALUE
------------------------------------ ----------- ------------------------------
audit_file_dest
string
/data/oracle/admin/wmsprod/adu
mp
audit_sys_operations
boolean
FALSE
audit_syslog_level
string
audit_trail
string
NONE
在備庫開啟實時日誌應用
SQL> alter database recover managed standby database using current logfile disconnect from session;
主備庫角色狀態查詢
SQL> select switchover_status,database_role from v$database;
--
主庫顯示:TO STANDBY/PRIMARY
,如果顯示SESSION ACTIVE
表示還有活動的會話,需要關閉活動的會話再檢查
--
備庫顯示:NOT ALLOWED/PHYSICAL STANDBY
測試
DG
執行日誌切換測試
在主庫端切換歸檔,在備庫檢查是否也發生了切換
主庫上執行日誌切換
SQL> archive log list;
SQL> alter system switch logfile;
SQL> archive log list;
備庫上檢視,日誌的sequence
號也跟著變了
SQL> archive log list;
1
檢視備庫啟動的
DG
程式
SQL> select PROCESS ,STATUS from v$managed_standby;
PROCESS STATUS
--------------------------- ------------------------------------
ARCH CONNECTED
ARCH CONNECTED
ARCH CONNECTED
ARCH CLOSING
RFS IDLE
RFS IDLE
RFS IDLE
MRP0 APPLYING_LOG
檢視資料庫的保護模式
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE PROTECTION_LEVEL OPEN_MODE
------------------------------------------------ ------------------------------------------------------------ ------------------------------------------------------------ ------------------------------------------------------------
PRIMARY MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE READ WRITE
檢視
DG
的日誌資訊
SQL> select * from v$dataguard_status;
1
Open Read Only Standby
資料庫
以只讀方式開啟備庫,並開啟實時日誌應用
SQL> shutdown immediate
SQL> startup
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
SQL> select process,client_process,sequence#,status from v$managed_standby;
SQL> alter database recover managed standby database using current logfile disconnect from session;
在DG上執行:
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
物理備庫。
SQL> select status from v$instance;
STATUS
------------
MOUNTED
原本DG是mount狀態。
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
Database altered.
斷應用同步。
SQL> alter database open read only;
Database altered.
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
Database altered.
開啟應用同步。
SQL> select RECOVERY_MODE from v$archive_dest_status where rownum<5;
RECOVERY_MODE
-----------------------
MANAGED REAL TIME APPLY
IDLE
IDLE
IDLE
SQL> SELECT NAME,VALUE,UNIT,TIME_COMPUTED
2 FROM V$DATAGUARD_STATS
3 WHERE NAME IN ('transport lag','apply lag');
NAME
VALUE
UNIT
TIME_COMPUTED
------------- -------------------- ------------------------------ ------------------------------
transport lag +00 00:00:00
day(2) to second(0) interval 05/15/2020 19:41:55
apply lag +00 00:00:00
day(2) to second(0) interval 05/15/2020 19:41:55
主庫日誌資訊:
SQL> select thread#, max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1
14
SQL> archive log list;
Database log mode
Archive Mode
Automatic archival
Enabled
Archive destination
/data/oracle/oradata/WMSPROD/archivelog
Oldest online log sequence 10
Next log sequence to archive 15
Current log sequence
15
備庫日誌資訊:
SQL> select thread#, max(sequence#) from v$archived_log where applied='YES' group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1
13
SQL> archive log list;
Database log mode
Archive Mode
Automatic archival
Enabled
Archive destination
/data/oracle/oradata/WMSPROD/archivelog
Oldest online log sequence 10
Next log sequence to archive 0
Current log sequence
15
主備同步正常。
以上完成物理DG搭建。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2692483/,如需轉載,請註明出處,否則將追究法律責任。