环境
rac 环境 (主)byoradbrac
系统版本:Red Hat Enterprise Linux Server release 6.5
软件版本:Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit
byoradb1:172.17.38.44
byoradb2:172.17.38.45
单机环境 (备)byoradbdg
系统版本:CentOS Linux release 7.9.2009
软件版本:Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit
byoradbdg:172.17.38.55
主库操作(byoradb1)
主库开启归档模式
#关闭数据库,启动到mount状态
SQL> shutdown immediate
SQL> startup mount
#修改数据库为归档模式,因为DG是通过传送归档日志到备库然后应用来保证主备库一致的。
SQL> alter database archivelog;
#查看归档日志状态
SQL> archive log list;
可参考 Oracle database 开启归档日志 archivelog
主库开启force logging
#修改数据库为强制记日志,这是必须的操作,主库的每一步操作都得记录到日志中去。
SQL> alter database force logging;
#查看是否为强制日志
SQL> select force_logging from v$database;
创建standby redo log
#查看Redo和Standby Redo
SQL> select * from v$logfile;
SQL> select group#,thread#,sequence#,archived,status from v$standby_log;
#仅仅显示Online Redo,不显示Standby Redo
#查看redo大小
SQL> select thread#,group#,bytes/1024/1024/1024 from v$log ;
#新增一组大小为500M的Standby Redo,这里的group号不得与Online redo重复
SQL> alter database add standby logfile group 5 '+DATADG' size 500M;
SQL>alter database add standby logfile group 6 '+DATADG' size 500M;
SQL>alter database add standby logfile group 7 '+DATADG' size 500M;
SQL>alter database add standby logfile group 8 '+DATADG' size 500M;
#查看是否添加成功
SQL> select GROUP#,TYPE,MEMBER from v$logfile;
创建pfile
这里创建pfile是为了做一些主库参数的配置,并且还得拷贝到备库再次修改成备库的配置。(如果是RAC到单机,则单机单独创建pfile)
SQL> create pfile='/u01/app/oracle/product/12c/dbhome_1/dbs/initbyoradb1.ora' from spfile;
create pfile='/nob/initrac.ora' from spfile;
SQL> shutdown immediate
修改主库pfile文件
# 编辑/u01/app/oracle/product/12c/dbhome_1/dbs/initbyoradb1.ora文件,追加
cat >> /u01/app/oracle/product/12c/dbhome_1/dbs/initbyoradb1.ora << "EOF"
*.db_unique_name='byoradbrac'
*.fal_server='byoradbdg'
*.log_archive_config='dg_config=(byoradbrac,byoradbdg)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=byoradbrac'
*.log_archive_dest_2='service=byoradbdg lgwr async valid_for=(online_logfile,primary_role) db_unique_name=byoradbdg'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='/oracledata/app/oracle/oradata','+DATA'
*.log_file_name_convert='/oracledata/app/oracle/oradata','+ARCH'
EOF
此时由于备库是单机、 所以需注意
*.db_file_name_convert='/oracledata/app/oracle/oradata','+DATA'
*.log_file_name_convert='/oracledata/app/oracle/oradata','+DATA'
如果备库也是rac
*.db_file_name_convert='+DATA','+DATA'
*.log_file_name_convert='+DATA','+DATA'
用pfile启动主库,并创建spfile
SQL> shutdown immediate
SQL> startup nomount pfile='/u01/app/oracle/product/12c/dbhome_1/dbs/initbyoradb.ora'
SQL> create spfile from pfile;
用创建的spfile启动主库
SQL> shutdown immediate
SQL> startup
复制主库的密码文件到备库
#密码文件一般存放在$ORALCE_HOME/dbs
cd $ORALCE_HOME/dbs
scp orapwbyoradb1 byoradb:/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs
#如果主库密码文件不存在则需要重新创建
#注意此步会修改sys用户的密码
orapwd file=/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs/orapwbyoradb1 password=zyz123123 entries=10 ignoreCase=Y force=y
cd $ORALCE_HOME/dbs
scp orapwbyoradb1 byoradb:/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs
配置主库监听listener.ora / tnsnames.ora
#查看监听文件位置
lsnrctl status
#grid 用户操作
vi /u01/app/12.1.0/grid/network/admin/listener.ora
#添加以下内容
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = byoradbrac)
(ORACLE_HOME = /u01/app/oracle/product/12c/dbhome_1)
(SID_NAME = byoradb1)
)
)
#静态监听中,GLOBAL_DBNAME 为 service_name ORACLE_HOME为oracle用户的
#配置tnsnames.ora 路径一般在$ORACLE_HOME/network/admin/
vi $ORACLE_HOME/network/admin/tnsnames.ora
#添加以下内容
byoradbdg =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.17.38.55)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = byoradb)
)
)
#测试是否能连通
sqlplus sys/zyz123123@byoradbdg as sysdba
备库操作(byoradb)
配置静态监听
#查看监听文件位置
lsnrctl status
#oracle 用户操作
vi /oracledata/app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora
#添加以下内容
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = byoradbdg)
(ORACLE_HOME = oracledata/app/oracle/product/12.1.0/dbhome_1)
(SID_NAME = byoradbdg)
)
)
#静态监听中,GLOBAL_DBNAME 为 service_name ORACLE_HOME为oracle用户的
配置tnsnames.ora
vi $ORACLE_HOME/network/admin/tnsnames.ora
#增加以下内容
byoradbrac =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.17.38.44)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = byoradb)
)
)
#测试是否能连通
sqlplus sys/zyz123123@byoradbdg as sysdba
修改备库pfile参数
如是空实例,可手动创建一个pfile文件
SQL> create pfile='/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs/initbyoradbdg.ora' from spfile;
#编辑/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs/initbyoradb.ora文件,修改备库初始化参数
cat >> /oracledata/app/oracle/product/12.1.0/dbhome_1/dbs/initbyoradbdg.ora << "EOF"
*.db_unique_name='byoradbdg'
*.fal_server='byoradb'
*.log_archive_config='dg_config=(byoradb,byoradbdg)'
*.log_archive_dest_1='location=use_db_recovery_file_dest valid_for=(all_logfiles, all_roles) db_unique_name=byoradbdg'
*.log_archive_dest_2='service=byoradb lgwr async valid_for=(online_logfile,primary_role) db_unique_name=byoradb'
*.log_archive_dest_state_1=ENABLE
*.log_archive_dest_state_2=ENABLE
*.standby_file_management='AUTO'
*.db_file_name_convert='+DATA','oracledata/app/oracle/oradata'
*.log_file_name_convert='+DATA','oracledata/app/oracle/oradata'
EOF
# 用修改后的pfile启动,并创建spfile,再用spfile启动
SQL> shutdown immediate
SQL> startup nomount pfile='/oracledata/app/oracle/product/12.1.0/dbhome_1/dbs/initbyoradbdg.ora'
SQL> create spfile from pfile;
SQL> shutdown immediate
启动备库到nomount
SQL> startup nomount
开始使用RMAN进行ADG
首先RMAN连接到目标数据库和辅助数据库
[oracle@ora-dg admin]$ rman target sys/zyz123123@byoradbrac auxiliary sys/zyz123123@byoradbdg
使用RMAN的duplicate命令进行复制,两边目录结构相同,需要添加nofilenamecheck参数
RMAN> duplicate target database for standby from active database nofilenamecheck;
复制成功后,备库自动被加载为mount模式,进入sqlplus查看
SQL> select instance_name,status from v$instance;
打开备库并开启apply service
[oracle@ST dbs]$ sqlplus / as sysdba
SQL> alter database open;
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY
#开启日志实时应用
SQL> alter database recover managed standby database disconnect from session;
检验是否成功
#主备库角色状态查询
SQL> select switchover_status,database_role from v$database;
1–主库显示:TO STANDBY/PRIMARY,如果显示SESSION ACTIVE表示还有活动的会话,需要关闭活动的会话再检查
2–备库显示:NOT ALLOWED/PHYSICAL STANDBY
可进行切换日志或插入数据效验是否同步完成。