環(huán)境(備庫只需要安裝數(shù)據(jù)庫軟件):
主庫:192.168.1.100 OS: CentOS 7.3 X64 DB: ORACLE12.2.0.1 SID: ORA12C db_unique_name: ORA12C
備庫:192.168.1.101 OS: CentOS 7.3 X64 DB: ORACLE12.2.0.1 SID: ORA12C db_unique_name: ORA12CDG
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,朝陽網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:朝陽等地區(qū)。朝陽做網(wǎng)站價格咨詢:13518219792
1.設(shè)置主庫為強制歸檔模式:
(1)SQL> alter database force logging;
Database altered.
SQL> select name,force_logging from v$database;
NAME FORCE_LOG
--------------------------- ---------
ORA12C YES
(2)如果在主庫添加或者刪除數(shù)據(jù)文件時,這些文件也需要在備份添加或刪除,需要如下設(shè)置:
SQL> alter system set STANDBY_FILE_MANAGEMENT='AUTO' scope=spfile;
默認(rèn)此參數(shù)是MANUAL手工方式,此方式在添加或者刪除數(shù)據(jù)文件時會導(dǎo)致日志無法應(yīng)用。SQL> show parameter STANDBY_FILE;
2.創(chuàng)建standby log files(備用日志文件)
從庫使用standby log files來保存從主庫接收到的重做日志。既然主要是從庫在使用,那為什么需要在主庫上也建立standby log files?原因主要是主庫可能轉(zhuǎn)換為備庫,而備庫是需要有standby log files的。建立standby如要注意以下幾點:
<1>standby log files的大小和redo log files一樣。
查詢redo log files文件大?。?br/>SQL> select group#,thread#,archived,status, bytes/1024/1024 from v$log;
GROUP# THREAD# ARC STATUS BYTES/1024/1024
---------- ---------- --- ---------------- ---------------
1 1 YES INACTIVE 200
2 1 NO CURRENT 200
3 1 YES INACTIVE 200
<2>一般而言, standbyredo 日志文件組數(shù)要比 primary 數(shù)據(jù)庫的 online redo 日志文件組數(shù)至少多一個。
SQL> select member from v$logfile;
SQL> alter database add standby logfile thread 1 group 11 '/oracle/oradata/ORA12C/stdby11.log' size 200m;
SQL> alter database add standby logfile thread 1 group 12 '/oracle/oradata/ORA12C/stdby12.log' size 200m;
SQL> alter database add standby logfile thread 1 group 13 '/oracle/oradata/ORA12C/stdby13.log' size 200m;
SQL> alter database add standby logfile thread 1 group 14 '/oracle/oradata/ORA12C/stdby14.log' size 200m;
3.密碼文件創(chuàng)建傳輸
(1)一般數(shù)據(jù)庫默認(rèn)就有密碼文件,存放為$ORACLE_HOME/dbs/orapwSID 這里為orapwORA12C
如果沒有則手動創(chuàng)建:
[oracle@ora12c ~]$ orapwd file=$ORACLE_HOME/dbs/orapwORA12C password=ccdadmin
(2)檢查REMOTE_LOGIN_PASSWORDFILE值是否為 EXCLUSIVE
SQL> show parameter REMOTE_LOGIN_PASSWORDFILE
如果值不是EXCLUSIVE,則:alter system set remote_login_passwordfile=exclusive scope=spfile;
(3)將密碼文件復(fù)制到備庫
[oracle@ora12c ~]$ scp $ORACLE_HOME/dbs/orapwORA12C oracle@192.168.1.101:/oracle/orahome/dbs/
4.db_name和db_unique_name
默認(rèn)db_name和db_unique_name和實例名是一致的,這里是ORA12C,需要注意在DG中主庫和從庫的db_unique_name是不能一致的,需要區(qū)分開。這里我們設(shè)置主庫的db_unique_name為ORA12C,從庫為ORA12CDG
SQL> show parameter db_unique_name;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_unique_name string ORA12C
設(shè)置:SQL> alter system set db_unique_name=ORA12C SCOPE=SPFILE;
--注意:雖然默認(rèn)db_unique_name和db_name是一致的,但是需要顯式設(shè)置,否則在spfile中沒有此參數(shù)
5.開啟flashback
SQL> select flashback_on from v$database;
SQL> alter database flashback on;
如果碰到 ORA-01153 報錯,一定是在備庫進行此操作。需要先取消重做日志應(yīng)用,啟用閃回日志,然后重新啟用日志應(yīng)用。
在主庫啟用flashback,不會同步備庫也啟用,必須手動在主庫和備庫上均啟用flashback。
6.配置監(jiān)聽及tnsnames.ora
主庫監(jiān)聽:
[oracle@ora12c ~]$ cd $ORACLE_HOME/network/admin
[oracle@ora12c admin]$ more listener.ora
SID_LIST_PRIM =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ORA12C)
(ORACLE_HOME = /oracle/orahome)
(SID_NAME = ORA12C)
)
)
PRIM =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
)
備庫監(jiān)聽:
[oracle@ora12cdg admin]$ more listener.ora
SID_LIST_STDBY =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ORA12C)
(ORACLE_HOME = /oracle/orahome)
(SID_NAME = ORA12C)
)
)
STDBY =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
)
主庫tnsnames.ora(備庫和主庫一樣):
[oracle@ora12c ~]$ cd $ORACLE_HOME/network/admin
[oracle@ora12c admin]$ more tnsnames.ora
ORA12C =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
)
(CONNECT_DATA =
(SID = ORA12C)
)
)
ORA12CDG =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
)
(CONNECT_DATA =
(SID = ORA12C)
)
)
7.重做日志傳輸配置
(1)配置歸檔日志路徑:
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oracle/archive
Oldest online log sequence 56
Next log sequence to archive 58
Current log sequence 58
SQL> alter system set log_archive_dest_1='LOCATION=/oracle/archive valid_for=(all_logfiles,all_roles) db_unique_name=ORA12C' scope=spfile;
SQL> alter system set log_archive_dest_state_1='enable' scope=spfile;
官方文檔里說使用 valid_for=(online_logfiles, all_roles) 將導(dǎo)致備庫無法歸檔備用日志文件,因為它們不是在線日志。使用all_logfiles選項,主備庫將都能歸檔在線以及備用日志。如果想在備庫進行備份,并同時備份歸檔日志的話,必須使用all_logfiles
(2)配置重做日志到備份庫:
SQL> alter system set log_archive_dest_2='SERVICE=ORA12CDG lgwr sync affirm valid_for=(online_logfile,primary_role) db_unique_name=ORA12CDG';
SQL> alter system set log_archive_dest_state_2='enable' scope=spfile;
(3)設(shè)置db_file_name_convert和log_file_name_conver參數(shù):
SQL> alter system set log_file_name_convert='/oracle/archive','/oracle/archive' scope=spfile;
SQL> alter system set db_file_name_convert='/oracle/oradata/ORA12C','/oracle/oradata/ORA12C' scope=spfile;
如果主備庫的目錄結(jié)構(gòu)不一樣,或者主備庫在同一臺服務(wù)器上,則必須要設(shè)置db_file_name_convert和log_file_name_conver參數(shù)來進行數(shù)據(jù)文件和聯(lián)機日志的保存路徑的轉(zhuǎn)換。
(4)STANDBY_ARCHIVE_DEST 參數(shù)不再需要,已經(jīng)被官方棄用。
8.配置FAL_SERVER
這個參數(shù)指定當(dāng)日志傳輸出現(xiàn)問題時,備庫到哪里去找缺少的歸檔日志。它用在備庫接收的到的重做日志間有缺口的時候。這種情況會發(fā)生在日志傳輸出現(xiàn)中斷時,比如你需要對備庫進行維護操作。在備庫維護期間,沒有日志傳輸過來,這時缺口就出現(xiàn)了。設(shè)置了這個參數(shù),備庫就會主動去尋找那些缺少的日志,并要求主庫進行傳輸。
在主庫:fal_server=從庫
從庫上就反過來:fal_server=主庫
SQL> alter system set FAL_SERVER='ORA12CDG';
注意:FAL_CLIENT在11g中已經(jīng)廢棄,雖然可以配置但是已經(jīng)不起作用了。
9.Data Guard 配置里的另外一個庫的名字
SQL> alter system set log_archive_config = 'dg_config=(ORA12C,ORA12CDG)';
10.在備庫創(chuàng)建所需要的目錄以放置數(shù)據(jù)文件、控制文件和跟蹤文件等
[oracle@ora12cdg ~]$ cd /oracle/
[oracle@ora12cdg oracle]$ mkdir admin archive fast_recovery_area
[oracle@ora12cdg oracle]$ cd admin/
[oracle@ora12cdg admin]$ mkdir ORA12C
[oracle@ora12cdg admin]$ cd ORA12C/
[oracle@ora12cdg ORA12C]$ mkdir adump dpdump pfile
[oracle@ora12cdg ORA12C]$ cd /oracle/fast_recovery_area/
[oracle@ora12cdg fast_recovery_area]$ mkdir ORA12C
11.主庫生成pfile (initORA12C.ora),并傳輸?shù)絺鋷爝M行修改
SQL> create pfile from spfile;
[oracle@ora12c ~]$ scp /oracle/orahome/dbs/initORA12C.ora oracle@192.168.1.101:/oracle/orahome/dbs/
主庫pfile:
[oracle@ora12c dbs]$ more initORA12C.ora
ORA12C.__data_transfer_cache_size=0
ORA12C.__db_cache_size=2298478592
ORA12C.__inmemory_ext_roarea=0
ORA12C.__inmemory_ext_rwarea=0
ORA12C.__java_pool_size=16777216
ORA12C.__large_pool_size=83886080
ORA12C.__oracle_base='/oracle'#ORACLE_BASE set from environment
ORA12C.__pga_aggregate_target=1056964608
ORA12C.__sga_target=3154116608
ORA12C.__shared_io_pool_size=167772160
ORA12C.__shared_pool_size=570425344
ORA12C.__streams_pool_size=0
*.audit_file_dest='/oracle/admin/ORA12C/adump'
*.audit_trail='db'
*.compatible='12.2.0'
*.control_files='/oracle/oradata/ORA12C/control01.ctl','/oracle/fast_recovery_area/ORA12C/control02.ctl'
*.db_block_size=8192
*.db_file_name_convert='/oracle/oradata/ORA12C','/oracle/oradata/ORA12C'
*.db_name='ORA12C' #數(shù)據(jù)庫的名字,在整個DG的所有主、備庫都應(yīng)一致
*.db_recovery_file_dest='/oracle/fast_recovery_area/ORA12C'
*.db_recovery_file_dest_size=8016m
*.db_unique_name='ORA12C'
*.diagnostic_dest='/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=ORA12CXDB)'
*.fal_server='ORA12CDG'
*.log_archive_config='dg_config=(ORA12C,ORA12CDG)'
*.log_archive_dest_1='LOCATION=/oracle/archive valid_for=(all_logfiles,all_roles) db_unique_name=ORA12C'
*.log_archive_dest_2='SERVICE=ORA12CDG lgwr sync affirm valid_for=(online_logfile,primary_role) db_unique_name=ORA12CDG'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.arc'
*.log_file_name_convert='/oracle/archive','/oracle/archive'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=1000m
*.processes=500
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=3000m
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
備庫修改部分:
*.db_file_name_convert='/oracle/oradata/ORA12C','/oracle/oradata/ORA12C'
*.db_unique_name='ORA12CDG'
*.fal_server='ORA12C'
*.log_archive_config='dg_config=(ORA12CDG,ORA12C)'
*.log_archive_dest_1='LOCATION=/oracle/orabackup/archive valid_for=(all_logfiles,all_roles) db_unique_name=ORA12CDG'
*.log_archive_dest_2='SERVICE=ORA12C lgwr sync affirm valid_for=(online_logfile,primary_role) db_unique_name=ORA12C'
*.log_file_name_convert='/oracle/archive','/oracle/archive'
12.啟動備庫到nomount狀態(tài):
SQL> conn /as sysdba
SQL> create spfile from pfile='/oracle/orahome/dbs/initORA12C.ora';
SQL> startup nomount
13.在主系統(tǒng)上執(zhí)行RMAN 并連接主數(shù)據(jù)庫和輔助數(shù)據(jù)庫(即備庫)
[oracle@ora12c admin]$ rman target sys/passwd@ORA12C auxiliary sys/passwd@ORA12CDG
connected to target database: ORA12C (DBID=376201757)
connected to recovery catalog database
connected to auxiliary database: ORA12C (not mounted)
RMAN> duplicate target database for standby from active database nofilenamecheck dorecover;
14.啟用物理備用數(shù)據(jù)庫:
duplicate 完成之后,備庫是mount的。
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
MOUNTED
SQL> alter database recover managed standby database disconnect from session;
15.查看主備庫同步狀態(tài):
SQL> alter system switch logfile; //主庫
SQL> select max(sequence#) from v$archived_log;
SQL> select process, client_process, sequence#, status from v$managed_standby;
16.備庫開啟flashback:
SQL> select flashback_on from v$database;
SQL> alter database recover managed standby database cancel;
SQL> alter database flashback on;
SQL> alter database recover managed standby database disconnect from session;
17.如果要開啟Active Data Guard,按以下步驟操作即可:
(1)取消管理備用數(shù)據(jù)庫Redo Apply
SQL> alter database recover managed standby database cancel;
(2)然后以只讀方式打開數(shù)據(jù)庫
SQL> alter database open;
(3)重新開始Redo Apply
SQL> alter database recover managed standby database disconnect from session;
(4)查看備庫打開模式
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY WITH APPLY