规划

主库 备库
ip 192.168.5.21 192.168.5.22
监听 动态/静态 静态
omf 启用 启用
db_recovery 启用 启用

主库配置

1.启动强制日志

  1. SQL> alter database force logging;

2.启动归档

修改归档文件格式

  1. SQL> alter system set log_archive_format='%t_%s_%r.arc' scope=spfile;

3.修改归档路径

  1. mkdir /opt/oracle/arc
  2. SQL> alter system set log_archive_dest_1='location=/opt/oracle/arc' scope=spfile;
  3. -----------------------------------------
  4. 使用快速闪回去存放归档
  5. alter system set log_archive_dest_1='LOCATION=USE_DB_RECOVERY_FILE_DEST';

4.重启数据库

  1. shutdown immediate;
  2. startup mount;
  3. SQL> alter database archivelog ;
  4. SQL> alter database open;

5.设置初始化参数

转换参数可以先不添加

  1. alter system set log_archive_config='DG_CONFIG=(fx,sfx)' scope=spfile;
  2. alter system set log_archive_dest_1='location=/opt/oracle/arc VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=fx' scope=spfile;
  3. alter system set log_archive_dest_2='SERVICE=sfx LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=sfx' scope=both;
  4. alter system set standby_file_management='AUTO' scope=both;
  5. alter system set fal_client='fx' scope=both;
  6. alter system set fal_server='sfx' scope=both;
  7. alter system set db_file_name_convert='/opt/oracle/oradata/SFX/datafile','/opt/oracle/oradata/FX/datafile' scope=spfile;
  8. alter system set log_file_name_convert='/opt/oracle/oradata/SFX/onlinelog','/opt/oracle/oradata/FX/onlinelog','/opt/oracle/fast_recovery_area/SFX/onlinelog','/opt/oracle/fast_recovery_area/FX/onlinelog' scope=spfile;

备库配置

1.创建初始化参数文件initsfx.ora

  1. 在主库设置了omf的情况下,备库如果不设置。就算指定了oradata中转换日志文件的参数也不会生效。<br /> 所以备库需要添加db_create_file_dest='' 参数。
  1. *.audit_file_dest='/opt/oracle/admin/SFX/adump'
  2. *.compatible='19.0.0'
  3. *.control_files='/opt/oracle/oradata/SFX/controlfile/o1_mf_gyhdym0y_.ctl','/opt/oracle/fast_recovery_area/SFX/controlfile/o1_mf_gyhdym26_.ctl'#Restore Controlfile
  4. *.db_create_file_dest='/opt/oracle/oradata'
  5. *.db_file_name_convert='/opt/oracle/oradata/FX/datafile','/opt/oracle/oradata/SFX/datafile'
  6. *.db_name='fx'
  7. *.db_recovery_file_dest='/opt/oracle/fast_recovery_area'
  8. *.db_recovery_file_dest_size=20g
  9. *.db_unique_name='sfx'
  10. *.fal_client='sfx'
  11. *.fal_server='fx'
  12. *.log_archive_config='DG_CONFIG=(fx,sfx)'
  13. *.log_archive_dest_1='LOCATION=/opt/oracle/arc VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=sfx'
  14. *.log_archive_dest_2='SERVICE=fx LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=fx'
  15. *.log_file_name_convert='/opt/oracle/oradata/FX/onlinelog','/opt/oracle/oradata/SFX/onlinelog','/opt/oracle/fast_recovery_area/FX/onlinelog','/opt/oracle/fast_recovery_area/SFX/onlinelog'
  16. *.pga_aggregate_target=850m
  17. *.remote_login_passwordfile='EXCLUSIVE'
  18. *.sga_target=2600m
  19. *.standby_file_management='AUTO'
  20. *.log_archive_format='%t_%s_%r.arc'

2.创建目录

  1. [oracle@12cdg dbs]$ mkdir /opt/oracle/arc
  2. [oracle@12cdg dbs]$ mkdir /opt/oracle/oradata/SFX -p
  3. [oracle@12cdg dbs]$ mkdir /opt/oracle/admin/SFX/adump -p
  4. [oracle@12cdg SFX]$ mkdir /opt/oracle/oradata/SFX/datafile
  5. [oracle@12cdg SFX]$ mkdir /opt/oracle/oradata/SFX/onlinelog
  6. [oracle@12cdg ~]$ mkdir /opt/oracle/oradata/SFX/onlinelog -p
  7. [oracle@12cdg ~]$ mkdir /opt/oracle/fast_recovery_area/SFX/controlfile/ -p

3.启动到nomount阶段

注意:以spfile启动实例,不要用pfile启动

  1. create spfile from pfile;
  2. startup nomount;

4.创建监听

测试监听

  1. tnsping fx
  2. tnsping sfx

5.主备库参数检查

  1. db_unique_name 需重启数据库
  2. compatible='12.2.0'
  3. log_archive_config='DG_CONFIG=(fx,sfx)' 是指的DB_UNIQUE_NAME
  4. log_archive_dest_1
  5. log_archive_dest_2
  6. log_archive_dest_state_1
  7. log_archive_dest_state_2
  8. ENABLE(默认)
  9. DEFER:禁用redo传输
  10. ALTERNATE:其他的传输失败,这个将启用。
  11. db_file_name_convert
  12. log_file_name_convert
  13. standby_file_management
  14. fal_client tnsname
  15. fal_server

6.备库复制主库密码文件

重命名

  1. scp oracle@192.168.5.21:/opt/oracle/product/19c/dbhome_1/dbs/orapwfx orapwfx
  2. mv orapwfx orapwsfx

7.使用Duplicate创建物理standby

  1. rman备份集
  2. 备库此时处于nomount状态
  3. rman target sys/Oracle123@fx (必须显示指定密码,否则后面会报密码错误的提示)
  4. RMAN> connect auxiliary sys/Oracle123@sfx
  5. rman target sys/Oracle123@fx auxiliary sys/Oracle123@sfx
  6. duplicate target database for standby from active database nofilenamecheck;

如果失败:
备库:
shutdown->startup nomount
数据文件都删除

主库rman链接备库

  1. rman target /
  2. RMAN> connect auxiliary sys/Oracle123@sfx
  3. RMAN> duplicate target database for standby from active database nofilenamecheck;

8.添加Standby日志组

Standby日志组=日志组+1

  1. alter database add standby logfile group 4 size 200m;
  2. alter database add standby logfile group 5 size 200m;
  3. alter database add standby logfile group 6 size 200m;
  4. alter database add standby logfile group 7 size 200m;
  5. alter database drop standby logfile group 4;
  6. alter database drop standby logfile group 5;
  7. alter database drop standby logfile group 6;
  8. alter database drop standby logfile group 7;
  9. 视图:
  10. v$logfile
  11. v$standby_log
  12. --------------------------------------------------------------
  13. select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#,
  14. decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#,
  15. decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES,
  16. decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS,
  17. decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED,
  18. --decode(g.GROUP#,null,'STANDBY REDO','REDO') type,
  19. gf.TYPE,
  20. gf.MEMBER
  21. from v$logfile gf
  22. left join v$log g
  23. on gf.GROUP# = g.GROUP#
  24. left join v$standby_log vs
  25. on gf.GROUP# = vs.GROUP#
  26. order by 1, 2;

9.开起实时同步

  1. 开启redo应用(实时同步)commit
  2. Oracle Database 12.1起,USING CURRENT LOGFILE子句已弃用
  3. alter database recover managed standby database using current logfile disconnect from session;弃用
  4. alter database recover managed standby database using current logfile disconnect;弃用
  5. alter database recover managed standby database disconnect;
  6. 停止redo应用
  7. alter database recover managed standby database cancel;
  8. 开启redo应用(日志切换才会同步)
  9. alter database recover managed standby database using archived logfile disconnect;
  10. alter system switch logfile;
  11. 查看状态:
  12. select name,open_mode,database_role,protection_mode,protection_level from v$database;

10.更改保护模式:

主库备库都需要执行 3 选 1

  1. alter database set standby database to maximize performance;最大性能
  2. alter database set standby database to maximize availability;最大可用
  3. alter database set standby database to maximize protection;最大保护

11.【DG启停顺序】

  1. 启动:
  2. 先备库,后主库
  3. 关闭:
  4. 先主库,后备库
  5. 前提:干净的关闭

12.解决同步故障

  1. 1.查下主库2节点,备库的配置
  2. 2.检查tns配置
  3. 3.检查fal_client,fal_server
  4. 4.查下备库是否在实时应用模式
  5. 5.查询相关视图
  6. v$managed_standby(备库查询)
  7. RFS:传输进程
  8. MRP0:日志应用进程
  9. v$archive_dest(主库)
  10. v$archive_dest_status(主库)
  11. select dest_name,status,error from v$archive_dest_status where dest_id = 2;
  12. select dest_name,status,error from v$archive_dest;

角色转换之Switchover主备互换

查看角色:primary和standby

  1. select database_role from v$database;

切换:
1.switchover主备互换
2.failover故障转移
switchover:主库和备库相互转换角色。不会丢失数据。
failover:当主库不可用,将standby转换为primary。最大保护模式和最大可用模式下,可以保证不丢失数据。
switchover操作步骤:
1阶段:主库->备库
在此阶段,dg临时具有2个备用数据库(过渡状态)
2阶段:备库->主库

1.主库检查配置文件和standby redo log

  1. redo +1
  2. v$standby_log
  3. alter DATABASE add standby logfile size 200m;
  4. alter DATABASE add standby logfile size 200m;
  5. alter DATABASE add standby logfile size 200m;
  6. alter DATABASE add standby logfile size 200m;
  7. ------------------------------------------------------
  8. select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#,
  9. decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#,
  10. decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES,
  11. decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS,
  12. decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED,
  13. --decode(g.GROUP#,null,'STANDBY REDO','REDO') type,
  14. gf.TYPE,
  15. gf.MEMBER
  16. from v$logfile gf
  17. left join v$log g
  18. on gf.GROUP# = g.GROUP#
  19. left join v$standby_log vs
  20. on gf.GROUP# = vs.GROUP#
  21. order by 1, 2;

2.主库检查是否可以进行转换

SWITCHOVER_STATUS:TO STANDBY 则可以进行转换,其它值则需要进行排查异常

  1. SQL> select name,database_role,switchover_status from v$database;
  2. NAME DATABASE_ROLE SWITCHOVER_STATUS
  3. --------- ---------------- --------------------
  4. FX PRIMARY TO STANDBY

备库为

  1. SQL> select name,database_role,switchover_status from v$database;
  2. NAME DATABASE_ROLE SWITCHOVER_STATUS
  3. --------- ---------------- --------------------
  4. FX PHYSICAL STANDBY NOT ALLOWED

验证是否有gap:

  1. select status, gap_status from v$archive_dest_status where dest_id = 2;

最保险的还要执行,insert

  1. create table t (id int);
  2. insert into t values (100);

3.主库执行切换

12c新语法:

  1. alter database switchover to sfx verify;
  2. alter database switchover to sfx ;
  3. alter database switchover to sfx force; #强制切换
  4. 原主库状态为:shutdown
  5. 原备库状态为:mount
  6. 旧语法新功能:
  7. swithover切换
  8. alter database commit to switchover to physical standby with session shutdown;(会话连接着)
  9. 执行完毕实例关闭了
  10. alter database commit to switchover to physical standby;(旧语法新功能)

4.启动数据库开启同步

  1. startup
  2. 查询状态:
  3. select open_mode,database_role from v$database;
  4. 开启实时应用:
  5. alter database recover managed standby database disconnect;
  6. --------------------------------------------------------------------

5.注意:

如果是生产环境,数据库运行时间很长,切换的时候时间会很长。
主备库:

  1. alter system flush buffer_cache;
  2. alter system checkpoint;
  3. shutdown immediate

重启启动实例做切换

6.重新初始化备库

  1. 进入备库rman
  2. rman target /
  3. alter database recover managed standby database cancel;
  4. RMAN> RECOVER STANDBY DATABASE FROM SERVICE orcl;
  5. shutdown immediate;
  6. startup
  7. alter database recover managed standby database disconnect;

报错解决

ORA-01103: database name ‘FX’ in control file is not ‘SFX’

没有添加db_unique_name=’sfx’参数

ORA-10456:cannot open standby database;media recovery session may be in progress

在搭dataguard练习中,主库和从库已经配置好,主库已经执行过rman的duplicate操作,要把从库起到open状态时,报了上面错误:
解决方法:

  1. 先在备库停止standby
  2. SQL>alter database recover managed standby database cancel;
  3. 这时再执行
  4. SQL>alter database open;
  5. 库就正常open
  6. 然后再启动日志应用
  7. SQL>alter database recover managed standby database using current logfile disconnect;

报ORA-16086: Redo data cannot be written to the standby redo log

  1. standby日志文件大小跟主库日志文件大小不一致,取消同步,删除standby文件重建
  2. alter database add standby logfile group 4 '/opt/oracle/oradata/SFX/onlinelog/standby01.log' size 300m;
  3. alter database add standby logfile group 5 '/opt/oracle/oradata/SFX/onlinelog/standby02.log' size 300m;
  4. alter database add standby logfile group 6 '/opt/oracle/oradata/SFX/onlinelog/standby03.log' size 300m;
  5. alter database add standby logfile group 7 '/opt/oracle/oradata/SFX/onlinelog/standby04.log' size 300m;
  6. alter database drop standby logfile group 4;
  7. alter database drop standby logfile group 5;
  8. alter database drop standby logfile group 6;
  9. alter database drop standby logfile group 7;

ORA-01665: control file is not a standby control file

rman 进入备库直接进行主库到备库恢复
注意此语句在19c 测试支持,12c 未测试

  1. rman target /
  2. RMAN> RECOVER STANDBY DATABASE FROM SERVICE orcl;
  3. 可能遇到MAN-05535: warning: All redo log files were not defined properly. 错误
  4. 直接忽略可以不处理
  5. 最后开启同步
  6. alter database recover managed standby database disconnect;

最大保护模式主数据库不能启动

  1. 报错信息:
  2. SQL> startup mount;
  3. ORACLE instance started.
  4. Total System Global Area 2466249672 bytes
  5. Fixed Size 8899528 bytes
  6. Variable Size 536870912 bytes
  7. Database Buffers 1912602624 bytes
  8. Redo Buffers 7876608 bytes
  9. Database mounted.
  10. SQL> alter database set standby database to maximize protection;
  11. Database altered.
  12. SQL> alter database open;
  13. alter database open
  14. *
  15. ERROR at line 1:
  16. ORA-03113: end-of-file on communication channel
  17. Process ID: 43659
  18. Session ID: 9 Serial number: 16405
  19. 数据库状态:
  20. select database_role,protection_mode,protection_level from v$database;
  21. 开启最大可用模式,protection_levelRESYNCHRONIZATION
  22. DG的配置存在问题:
  23. 检查点:
  24. 1log_archive_dest_2参数是否配置正确
  25. 2standby log是否创建成功
  26. 3)主库是否开启了闪回
  27. select flashback_on from v$database;
  28. alter database flashback on;
  29. #如果3点均满足,但是状态依然不对,建议重建standby log