ORACLE 12C DATAGUARD環境搭建和主從切換
環境說明:
目的:
(1)在linux 操作系統下,創建oracle 12c 單實例的dataguard環境
(2)測試dataguard主從切換
主庫:10.50.115.12 (DB_UNIQUE_NAME=cdbtest,service_name=cdbtest,db_name=cdbtest)
從庫:10.50.115.20 (DB_UNIQUE_NAME=cdbtest1,service_name=cdbtest,db_name=cdbtest)
備注:DB_UNIQUE_NAME不能一樣
-------------------------------------------------------------------------------
一、創建oracle 12c 單實例的dataguard環境
主庫:
mkdir -p /u01/app/oracle/arch/
chown oracle.oinstall /u01/app/oracle/arch/
1. 開啟歸檔模式
sqlplus / as sysdba
startup mount
alter database archivelog;
alter database open;
2. 配置TNS,增加連接到從庫的tns:
cat >> $ORACLE_HOME/network/admin/tnsnames.ora << EOF
cdbtest1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.50.115.20)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = cdbtest)
)
)
EOF
3. 修改主庫的dataguard相關參數
alter system set standby_file_management='auto' scope=both;
alter system set log_archive_config='dg_config=(cdbtest,cdbtest1)' scope=both;
alter system set LOG_ARCHIVE_DEST_1= 'LOCATION=/u01/app/oracle/arch/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=cdbtest' scope=both;
alter system set LOG_ARCHIVE_DEST_2= 'SERVICE=cdbtest1 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=cdbtest1' scope=both;
# 這些參數僅當主庫切換成備庫時生效
alter system set FAL_SERVER=cdbtest1 scope=both;
alter system set DB_FILE_NAME_CONVERT='cdbtest','cdbtest' scope=spfile;
alter system set LOG_FILE_NAME_CONVERT= '/u01/app/oracle/oradata/cdbtest/','/u01/app/oracle/oradata/cdbtest/' scope=spfile;
4. 增加standby log
select member from v$logfile;
alter database add standby logfile '/u01/app/oracle/oradata/cdbtest/stdredo01.log' size 10M;
alter database add standby logfile '/u01/app/oracle/oradata/cdbtest/stdredo02.log' size 50M;
alter database add standby logfile '/u01/app/oracle/oradata/cdbtest/stdredo03.log' size 50M;
從庫:
mkdir -p /u01/app/oracle/arch/
chown oracle.oinstall /u01/app/oracle/arch/
1. 創建與主庫一樣的數據庫(主要目的是創建相關目錄,數據文件隨后會覆蓋,不用創建pdb數據庫)
2. 配置連接到主庫的TNS:
cat >> $ORACLE_HOME/network/admin/tnsnames.ora << EOF
cdbtest_pri =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.50.115.12)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cdbtest)
)
)
EOF
3. 創建靜態監聽器
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = cdbtest)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
4. 修改相關參數
alter system set log_archive_config='dg_config=(cdbtest,cdbtest1)' scope=both;
alter system set STANDBY_FILE_MANAGEMENT=AUTO scope=both;
alter system set DB_FILE_NAME_CONVERT='cdbtest','cdbtest' scope=spfile;
alter system set LOG_FILE_NAME_CONVERT= '/u01/app/oracle/oradata/cdbtest/','/u01/app/oracle/oradata/cdbtest/' scope=spfile;
alter system set FAL_SERVER=cdbtest_pri scope=both;
alter system set log_archive_dest_state_1='enable' scope=both;
alter system set log_archive_dest_state_2='enable' scope=both;
alter system set LOG_ARCHIVE_DEST_1= 'LOCATION=/u01/app/oracle/arch/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=cdbtest1' scope=both;
alter system set LOG_ARCHIVE_DEST_2= 'SERVICE=cdbtest_pri ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=cdbtest'scope=both;
# 創建數據庫后,db_unique_name將不能改
alter system set db_unique_name='cdbtest1' scope=spfile;
alter system set service_names=cdbtest scope=spfile;
5. 復制密碼文件:
scp /u01/app/oracle/product/12.1.0/dbhome_1/dbs/orapwcdbtest 10.50.115.20://u01/app/oracle/product/12.1.0/dbhome_1/dbs/
6. 建pdb數據文件的目錄(參考主庫)
cd /u01/app/oracle/oradata/cdbtest
mkdir -p pdb1 pdb2
7. rman復制數據庫
備庫:
rman target sys/biostime123@cdbtest_pri auxiliary sys/biostime123@cdbtest1 nocatalog <<eof
duplicate target database for standby from active database nofilenamecheck;
EOF
8.啟動日志應用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE cancel;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
二、主從切換測試
主從切換:
主庫:
1. 如果該列值為"TO STANDBY"則表示primary 數據庫支持轉換為standby
select switchover_status from v$database;
2. 首先將primary 轉換為standby 的角色
alter database commit to switchover to physical standby;
(執行完后,數據庫down掉)
3. 重啟動到mount(原primary)
shutdown immediate
startup mount
select switchover_status from v$database;
說明:PRIMARY進行轉換完畢后,查看狀態會變成RECOVERY NEEDED;
從庫:
備庫切換成主庫的操作
1、檢查備庫的狀態
select switchover_status from v$database;
主庫關閉前為“NOT ALLOWED”,關閉后,從庫變為“TO_PRIMARY”
2. 確認沒有問題后,可以進行切換轉換standby 到primary 角色
alter database commit to switchover to primary;
3. 完成轉換,打開新的primary 數據庫
alter database open;
4.檢查狀態
select name,open_mode,database_role,protection_mode,SWITCHOVER_STATUS From v$database;
5. 在原主庫啟動日志應用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
ALTER DATABASE OPEN;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
</eof