摘要:oracle 12c dg搭建切换以及故障诊断
同样,如果创建或更改数据库以启用FORCE LOGGING,则数据库中的任何更改(临时段和临时表空间除外)都将在重 做日志中用于恢复。
desc vdatabaseselectforceloggingfromvdatabaseselectforceloggingfromvdatabase;
alter database force logging;
(2)启用归档,查看的方式两种,11g需要开启,12c默认开启
archive log list;
select log_mode from v$database;
两个参数
log_archive_format
alter system set log_archive_format=’%t_%s_%r.arc’ scope=spfile;( 静态变量,要重启数据库)
log_archive_dest (下一步中修改_1 和_2)
shutdown immediate
startup mount
alter database archivelog;
alter database open;
(3)静态监听
listener.ora文件配置
LISTENER1=
(
DESCRIPTION_LIST=
(
DESCRIPTION=
(
ADDRESS_LIST=
(
ADDRESS=(PORTOCOL=TCP)(HOSTNAME=oracle12cpri)(PORT=1522)
)
)
)
)
SID_LIST_LISTENER1=
(
SID_LIST=
(
SID_DESC=
(GLOBAL_DBNAME=prod)
(SID_NAME=prod)
(ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1)
)
)
由配置文件可以看出:sid_name实例名prod,global_dbname对外的服务名prod。
文件三个注意点
标红的地方命名要一致
global_dbname 对应 (lsnrctl status listener)services
sid_name 对应 (lsnrctl status listener) instance
(4)初始化参数
db_unique_name
alter system set db_unique_name=’prod’ scope=spfile; prod要加引号,不加引号就是大写
log_archive_config
alter system set log_archive_config=’DG_CONFIG=(prod,sbdb)’ scope=both;
log_archive_dest_1
alter system set log_archive_dest_1=’LOCATION=/u01/arch VALID_FOR=
(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=prod’ scope=both;
Log_archive_dest_2
alter system set log_archive_dest_2=’SERVICE=tnssbdb LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=sbdb’ scope=both;
配置tnsnames.ora文件
复制prod ()里面代码,修改prod 为tnssbdb, host=oracle12csta
Port =1522, service_name 不变,因为主备机是同一访问名称。
db_file_name_convert 数据文件 (select * from vdbfile;) alter system set db_file_name_convert=’/u01/app/oracle/oradata/sbdb’,’/u01/app/oracle/oradata/prod’ scope=spfile; log_file_name_convert 重做日志文件 (select * from vlogfile)
alter system set db_file_name_convert=’/u01/app/oracle/oradata/sbdb’,’/u01/app/oracle/oradata/prod’ scope=spfile;
standby_file_management
alter system set standby_file_management=auto; 不加引号就是大写
fal_client 本端作为客户端
alter system set fal_client=’tnsprod’ scope=both;
fal_server 对端备机作为服务端
alter system set fal_server=’tnssbdb’ scope=both;
tnsprod =
(
DESCRIPTION=
(
ADDRESS=(PROTOCOL=TCP)(HOST=oracle12cpri)(PORT=1522)
)
(
CONNECT_DATA=
(SERVER=DEDICATED)
(SERVICE_NAME=prod)
)
)
备机把主库的文件scp,只留上面的tnsprod tndsbdb
并做 tnsping 测试
tnsping tnsprod tnsping tnssbdb
主备库都连接下
sqlplus sys/ora123@tnsprod as sysdba
sqlplus sys/ora123@tnssbdb as sysdba
参数检查
db_unique_name
compatible
log_archive_config
log_archive_dest_1
log_archive_dest_2
log_archive_dest_state_2: enable-启用 defer-禁用
db_file_name_convert
log_file_name_convert
standby_file_management
log_archive_format
至此,准备工作做好,备库处于 unmount 状态
select status from v$instance;
started 已经启动的状态
6. 使用duplicate创建物理standby
主库
rman target sys/ora123@tnsprod 必须用这种输入密码的方式
connect auxiliary sys/ora123@tnssbdb
duplicate target database for standby from active database nofilenamecheck;
此时检查下,/u01/app/oracle/oradata/sbdb 会有数据,和主库prod 下面一致
备库:此时备库要处于mount状态,
startup nomount alter database mount;
select * from v$instance; mounted
alter database add standby logfile group 4 (‘/u01/app/oracle/oradata/sbdb/stredo04.log’) size 50M;
alter database add standby logfile group 5 (‘/u01/app/oracle/oradata/sbdb/stredo05.log’) size 50M;
alter database add standby logfile group 6 (‘/u01/app/oracle/oradata/sbdb/stredo06.log’) size 50M;
alter database add standby logfile group 7 (‘/u01/app/oracle/oradata/sbdb/stredo07.log’) size 50M;
如果 目录写错了,执行下面
alter database drop logfile group 4;
开启同步:
备库:
alter database open;
开启实时同步
alter database recover managed standby database using current logfile disconnect from session;
日志切换才同步
alter database recover managed standby database disconnect from session;
停止同步
alter database recover managed standby database cancel;
select open_mode,database_role,protection_mode,protection_level from v$database;
打开实时同步,open_mode 是read only with apply
不打开,是 read only
查看主库的 是 read write
如果同步没有成功,再检查下主备库的参数是否正确,可能会是
log_archive_dest_state_2: enable-启用 没有开启
然后shutdown immediate startup主备库,备库开启实时同步
alter database recover managed standby database using current logfile disconnect from session;
到此dg搭建完成
下面是查看dataguard 的状态,解决一些dg遇到的问题
9. 启动顺序
先监听后实例,先备库后主库
关闭顺序
先关主库,再关备库
视图:
(1)vdatabase open_mode read only,read and write,read only with apply,mount database_role physical standby,logical standby,primary,snapshot standby protection_mode maximize availability,maximize protection,maximize performance, resynchronization(重新同步模式),unprotected (2)vmanaged_standby (备库执行)
process ( ps – ef | grep ora 可以看到) ARCH 归档,MRPO日志实时同步,RFS远程日志接收进程
select process,pid,status,sequence# from vmanaged_standby; process: ARCH:归档进程 MRP0:用来应用接收的日志media recovery process(开启了实时应用或者非实时应用) RFS:remote file server,接收远程日志文件 pid: 操作系统进程号 status: CONNECTED:和主库建立了网络连接 CLOSING:进程已经完成归档,并且关闭了归档日志文件 WRITING:进程正在写redo数据到归档文件。 APPLYING_LOG:正在应用日志到备库,开启实时应用 WAIT_FOR_LOG:等待归档日志完成(开启非实时应用,或者主备之间不通) alter database recover managed standby database disconnect; sequence#:归档日志序列号 archive log list sequence 142 alter system switch logfile; sequence 会增加1 (3)vstandby_log 备库查询
select group#,sequence#,archived,status from vstandby_log; 对比: select * from vlog;
select * from vlogfile;select∗fromvlogfile;select∗fromvstandby_log;
(4) varchive_dest_status status: VALID:有效 INACTIVE:非活动的 DEFERRED:手工禁用(可用临时禁用同步) alter system set log_archive_dest_state_2=’defer’ 临时进用同步的命令 alter system set log_archive_dest_state_2=’enable’ 开启 type: LOCAL:主库 PHYSICAL:物理standby LOGICAL:逻辑standby SNAPSHOT:快照standby error,synchronization_status,synchronized 这三个字段可以帮助查看dg不同步的故障 (5) vdataguard_status
备库查询
select * from v$dataguard_status t order by t.“TIMESTAMP” desc
message字段,可以查看DG的一些报错
数据库新建用户,分权限
create user test identified by test;
grant sysoper,connect,resource to test;
日志GAP
模拟故障:
主库:log archive list; sequence 会和备库不一样,主库的protection_level = ‘重新同步模式’
备库:select * from vmanaged_standby MRPO 进程 会显示:wait_for_log, 说明现在没有应用日志(正常应该是applying_log) 主库: 主库的squence# 会多于备库 Select t.”sequence#”,t.”applied”, from varchived_log t where id=1;
备库:
Select t.”sequence#”,t.”archived” from varchivedlogtwhereid=1;Select∗fromvarchivedlogtwhereid=1;Select∗fromvarchive_dest_status;
Error 字段会出现报错,像改成defer 的话,不会报错
主库不可用,需要自己手动操作
把gap掉的日志文件,archive log list 主库路径下的文件拷过去
备库没有注册过去rman target / list archive all;看到没有主库的那几个日志文件,需要手工注册
少量的话:alter database register logfile ‘/u01/arch/***’;
大量的话:rman>catalog start with ‘/u01/arch/’;
查看备库alert文件
/u01/app/oracle/diag/rdbms/sbdb/sbdb/trace/alert_sbdb.log
自动应用,不行的话,重新开始备库日志同步即可
备库:
select t.“NAME”,t.“DATABASE_ROLE”,t.“SWITCHOVER_STATUS” from vdatabase t; SWITCHOVER_STATUS: to primary session active alter database commit to switchover to primary; alter database commit to switchover to primary with session shutdown; shutdown immediate; startup insert 测试下 ,查看下模式 select open_mode,database_role,protection_mode,protection_level from vdatabase;
注意:生产环境的话 ,库运行时间长的话,直接切换会花费很长时间
主备库执行:alter system flush buffer_cache;
alter system checkpoint;
shutdown immediate
然后重启主备库,备库打开实时同步,在进行切换
(2) failover故障转移主库坏了,备库转主库,最大保护和最大可用不丢失数据(相对switchover)
步骤:
停止备库日志应用
alter database recover managed standby database cancel;
关闭备库的日志传输
注意要先解决GAP,解决后执行下面这条语句
alter database recover managed standby database finish force;
若解决不了,执行下面,结果就是会丢失数据
alter database active physical standby database;
切换主库
alter database commit to switch to primary with session shutdown;
检查:
select open_mode,database_role from v$database;
read write database_role
更多Oracle精品文章:https://www.modb.pro/db?cyn