Oracle Data Guard 是针对企业数据库的最有效和最全面的数据可用性、数据保护和灾难恢复解决方案。它提供管理、监视和自动化软件基础架构来创建和维护一个或多个同步备用数据库,从而保护数据不受故障、灾难、错误和损坏的影响。一台主数据库最多可以配备9个备数据库。
1) 主数据库服务器和备数据库服务器的操作系统版本必须一致。
2) 主数据库服务器和备数据库服务器的数据库版本必须一致。
3) 主数据库服务器和备数据库服务器的安装目录必须一致。
数据库安装完后,主库和备库都配置为MTS方式,创建PI用户前进行配置。配置完成后在主库创建PI用户,备库会自动创建PI用户。
以下步骤主库和备库的操作完全相同:
sqlplus / as sysdba
shutdown immediate;
startup mount;
alter database archivelog;
alter system setcontrol_file_record_keep_time=30 scope=both;
alter system setlog_archive_dest_1='location=/home/oracle/archivelog' scope=both;
注:/home/oracle/archivelog为归档重做日志存放目录
alter database open;
alter system archivelog start;
exit;
查看闪回目录
show parameterdb_recovery_file_dest
查看闪回目录可用大小
show parameterdb_recovery_file_dest_size
修改闪回目录位置:
alter system setdb_recovery_file_dest='newpath';
修改闪回目录大小
alter system setdb_recovery_file_dest_size=400G;
查看闪回功能是否启用,默认不启用
select flashback_onfrom v$database;
启用闪回功能:
alter databaseflashback on;
select * fromv$option where parameter = 'Oracle Data Guard';
如果是true表示已经安装可以配置,否则需要安装相应组件
alter database forcelogging;
检查状态:selectname,force_logging from v$database;
alter system set standy_file_management='AUTO';
建立standby如要注意以下几点:
<1>standby log files的大小和redo logfiles一样。
查询redo logfiles文件大小(默认50M,3个):select group#,bytes/1024/1024 as M from v$log
<2>一般而言, standbyredo 日志文件组数要比 primary 数据库的 online redo日志文件组数至少多一个。
有一个推荐的公式可以做参考:(每线程的日志组数+1)*最大线程数
假设现在节点是1个,则=(3+1)*1=4
如果是双节点 则=(3+1)*2=8
这里我们创建4个standbylogfile:
另:不建议组号group#紧挨着redo,因为后续redo有可能调整,这里我们从建立从11到14的standby logfile
alter database add standby logfile group 11'/opt/oracle/oradata/orcl/dg/standby11.log' size 50M;
alter database add standby logfile group 12'/opt/oracle/oradata/orcl/dg/standby12.log' size 50M;
alter database add standby logfile group 13'/opt/oracle/oradata/orcl/dg/standby13.log' size 50M;
alter database add standby logfile group 14'/opt/oracle/oradata/orcl/dg/standby14.log' size 50M;
注:/opt/oracle/oradata/orcl/dg/standby14.log 可以自定义文件名,但是目录必须存在,文件名必须不存在
=========================
以上配置主库备库一致
查看db_unique_name
showparameter db_unique_name
主库:alter system set db_unique_name=paidbscope=spfile;
备库:alter system set db_unique_name=paidb27scope=spfile;
alter system set FAL_SERVER='paidb27';
alter system set log_archive_config ='dg_config=(paidb,paidb27)';
alter system set FAL_SERVER='paidb';
alter system set log_archive_config ='dg_config=(paidb,paidb27)';
alter systemset log_archive_dest_2='service=paidb27lgwr sync affirm valid_for=(online_logfile,primary_role)db_unique_name=paidb27’;
注:paidb27为备库的db_unique_name
alter systemset log_archive_dest_2='service=paidblgwr sync affirm valid_for=(online_logfile,primary_role)db_unique_name=paidb’;
注:paidb为备库的db_unique_name
将主库的密码文件复制到备库
关闭主库和备库:shutdownimmediate
scp$OARCLE_HOME/dbs/orapwpaidb 备库:$ORACLE_HOME/dbs/
关闭主库和备库shutdownimmediate;
主库执行:
startup mount;
alter database createstandby controlfile as ‘/home/oracle/standby_control01.ctl’;
shutdown immediate;
关闭备库shutdownimmediate;
scp /home/oracle/standby_control01.ctl 备库:$ORACLE_HOME/../../../oradata/paidb/
scp /home/oracle/standby_control01.ctl 备库:$ORACLE_HOME/../../../fast_recovery_area/paidb/
vi $ORACLE_HOME/network/admin/listener.ora
SID_LIST_LISTENER=
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME =paidb)
(ORACLE_HOME =/data1/oracle/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = paidb)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL =TCP)(HOST = 192.168.20.28)(PORT = 1521))
)
)
SUBSCRIBE_FOR_NODE_DOWN_EVENT_LISTENER=OFF
PAIDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL =TCP)(HOST = 192.168.20.28)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = paidb)
(SID = paidb)
)
)
PAIDB27 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST= 192.168.20.27)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = paidb27)
(SID = paidb)
)
)
SID_LIST_LISTENER=
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME =paidb27)
(ORACLE_HOME =/data1/oracle/app/oracle/product/11.2.0.4/dbhome_1)
(SID_NAME = paidb)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL =TCP)(HOST = 192.168.20.27)(PORT = 1521))
)
)
PAIDB27 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL =TCP)(HOST = 192.168.20.27)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = paidb27)
(SID = paidb)
)
)
PAIDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL =TCP)(HOST = 192.168.20.28)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = paidb)
(SID = paidb)
)
)
重启监听
先重启备库:
lsnrctl stop
lsnrctl start
后重启主库:
lsnrctl stop
lsnrctl start
主库和备库都关闭:
shutdown immediate;
scp $ORACLE_HOME/../../../oradata/paidb/* 备库:$ORACLE_HOME/../../../oradata/paidb/
startup nomount;
alter database mount standby database;
启动实时应用:
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENTLOGFILE DISCONNECT FROM SESSION;
//暂停redo应用:
//ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
startup
shutdown immediate;
startup nomount;
alter database mount standby database;
alter database recover managed standby database disconnectfrom session;
SQL>shutdown immediate;
SQL>startup nomount;
SQL>alter database mount standby database;
SQL>alter database open read only;
SQL> recover managed standby database cancel;
SQL> alter database open read only;
SQL> recover managed standby database disconnect from session;
select SEQUENCE#, FIRST_TIME, NEXT_TIME,APPLIED, ARCHIVED from V$ARCHIVED_LOG where name = 'JED2' order by FIRST_TIME;
如果归档和日志应用均正常,APPLIED 和 ARCHIVED 列都应该是 YES。(如果没有应用redo,applied应该是NO)
主库上查看日志传送情况:
sql>select dest_name,status,error from v$archive_dest;
应该log_archive_dest_1和2状态应该是valid
切换几次日志:
sql>alter system switch logfile;
查看日志序号:
sql>select sequence# from v$archived_log;
备库验证:
sql>select sequence#,applied from v$archived_log;
先启备库再起主库
#lsnrctl start
先启备库:
sql>startup nomount
sql>alter database mount standbydatabase;
sql>alter database recover managedstandby database using current logfile disconnect from session;
在启主库
sql>startup
先关主库:
sql>shutdown immediate
再关从库:
sql>alter database recovermanaged standby database cancel;
sql>shutdownimmediate;
最大保护:这种模式能够确保绝无数据丢失。要实现这一步当然是有代价的,它要求所有的
事务在提交前其 redo 不仅被写入到本地的 online redo log,还要同时提交到
standby 数据库的 standbyredo log,并确认 redo数据至少在一个 standby数据
库可用(如果有多个的话),然后才会在 primary数据库上提交。如果出现了什么
故障导致 standby 数据库不可用的话,primary 数据库会被 shutdown。
最高性能:这种模式提供在不影响 primary 数据库性能前提下最高级别的数据保护策略。
事务可以随时提交,当前 primary 数据库的 redo 数据也需要至少写入一个
standby 数据库,不过这种写入可以是不同步的。如果网络条件理想的话,这
种模式能够提供类似最高可用性的数据保护而仅对 primary 数据库有轻微的性
能影响。
最高可用性:这种模式提供在不影响 primary 数据库可用前提下最高级别的数据保护策略。
其实现方式与最大保护模式类似,也是要求所有事务在提交前必须保障 redo
数据至少在一个 standby 数据库可用,不过与之不同的是,如果出现故障导
入无法同时写入 standby 数据库 redo log,primary数据库并不会 shutdown,
而是自动转为最高性能 模式,等 standby 数据库恢复正常之后,它又会再自
动转换成最高可用性模式。
查看保护模式:
select protection_mode,protection_level from v$database;
--默认是最大性能模式(maximum performance)
查看身份:
selectdatabase_role from v$database; --主库是primary备库是physical standby
切换保护模式:
alterdatabase set standby database to maximize protection; --最大保护
alter database set standbydatabase to maximize availability; --最高可用性
alter database set standbydatabase to maximize performance; --最高性能