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;
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;
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;