Oracle DataGuard学习笔记(2)物理Standby的配置
DataGuard是Oracle数据库软件集成的功能,不需要另外安装软件,只需要配置即可.
物理Standby的配置,可以参考Oracle官方的在线文档:
http://docs.oracle.com/cd/E11882_01/server.112/e41134/create_ps.htm#SBYDB4718
1.创建数据库
首先需要安装Oracle数据库软件,可以参考其它安装教程.
(1).Primary主机创建数据库(主库).
$ export ORACLE_UNQNAME=sales
$ dbca
数据库名: sales
指定归档格式: {DB_UNIQUE_NAME}_%s_%t_%r.arc
指定归档位置: {ORACLE_BASE}/oradata/{DB_UNIQUE_NAME}/archive
(2).Standby主机创建数据库(备库).
$ export ORACLE_UNQNAME=sales_dg
$ dbca
数据库名: sales
指定归档格式: {DB_UNIQUE_NAME}_%s_%t_%r.arc
指定归档位置: {ORACLE_BASE}/oradata/{DB_UNIQUE_NAME}/archive
**** 备库创建的数据库和主库一致,以便后面设置时可以少修改一些参数.
2.主库开启归档模式
$ sqlplus / as sysdba;
alter system set log_archive_dest_1='LOCATION=/u01/app/oracle/oradata/sales/archive mandatory' scope=spfile;
alter system set log_archive_format='sales_%s_%t_%r.arc' scope=spfile;
shutdown immediate;
startup mount;
alter database archivelog;
alter database force logging; -- 强制记录日志
alter database open;
3.将主库备份恢复到备库
$ sqlplus / as sysdba;
shutdown immeidate;
startup mount;
在主库获取Standby控制文件,此Standby控制文件将作为备库的控制文件.
alter database create standby controlfile as '/u01/app/oracle/oradata/sales/standby01.ctl';
备份数据文件,参考数据库备份的教程,冷备份可以直接复制数据文件.
将密码文件,Standby控制文件,备份的数据文件,归档日志文件复制到备库,Standby控制文件需改名为备库的控制文件名,并且需替换备库所有的控制文件.
select name from v$controlfile; -- 查看所有的控制文件
4.修改备库参数
db_unique_name: 设置数据库唯一性参数为sales_dg.
log_archive_config: 配置DG归档参数,DG_CONFIG指定组成DataGuard的数据库唯一名称(db_unique_name)列表.
log_archive_dest_1: 指定本地归档位置
log_archive_dest_2: 指定DataGuard远程归档参数,切换为主库时使用
standby_archive_dest: 指定standby日志的归档目的位置,过期参数,通常不用设置.
如果备库利用standby redo log在备库端自动归档,那么归档日志将会被放置到log_archive_dest_n.
如果备库是利用主库的arch进程传输过来的归档,那么将会被放置到standby_archive_dest.
standby_file_management: 指定文件自动管理方式,例如文件大小变更,AUTO会自动管理.
log_archive_max_processes: 指定归档进程数.
fal_server: 指定PrimaryDB(Oracle Net service name),作为备库处理裂隙时使用.
fal_client: 指定StandbyDB(Oracle Net service name),作为备库处理裂隙时使用.
$ sqlplus / as sysdba;
startup mount;
alter system set db_unique_name='sales_dg' scope=spfile;
alter system set log_archive_config='dg_config=(sales,sales_dg)' scope=spfile;
alter system set log_archive_dest_1='LOCATION=/u01/app/oracle/oradata/sales/archive mandatory' scope=spfile;
alter system set log_archive_dest_2='SERVICE=sales LGWR async valid_for=(online_logfile,primary_role) db_unique_name=sales' scope=both;
--alter system set standby_archive_dest='/u01/app/oracle/oradata/sales/standby_arc' scope=both;
alter system set standby_file_management=auto scope=both;
alter system set log_archive_max_processes=5 scope=both;
alter system set fal_server=sales scope=both;
alter system set fal_client=sales_dg scope=both;
alter system set remote_login_passwordfile=exclusive scope=spfile;
修改服务名称,客户端tns连接一般是通过service_names连接,服务名和主库保持一致.
alter system set service_names='sales' scope=both;
创建standby重做日志文件:
alter database add standby logfile group 4 ('/u01/app/oracle/oradata/sales/standby0401.log','/u01/app/oracle/oradata/sales/standby0402.log') size 50M;
alter database add standby logfile group 5 ('/u01/app/oracle/oradata/sales/standby0501.log','/u01/app/oracle/oradata/sales/standby0502.log') size 50M;
alter database add standby logfile group 6 ('/u01/app/oracle/oradata/sales/standby0601.log','/u01/app/oracle/oradata/sales/standby0602.log') size 50M;
alter database add standby logfile group 7 ('/u01/app/oracle/oradata/sales/standby0701.log','/u01/app/oracle/oradata/sales/standby0702.log') size 50M;
文件大小与主库的重做日志大小保持一致:
select m.group#,m.member,g.bytes/1024/1024 file_size from v$log g join v$logfile m on m.group#=g.group# order by m.group#,m.member;
standby重做日志文件组数至少比在线重做日志文件组数多一组,参考公式: (每实例的日志组数 + 1) * 实例数.
5.修改主库参数
db_unique_name: 设置数据库唯一性参数为sales_dg.
log_archive_config: 配置DG归档参数,DG_CONFIG指定组成DataGuard的数据库唯一名称(db_unique_name)列表.
log_archive_dest_1: 指定本地归档位置
log_archive_dest_2: 指定DataGuard远程归档参数
fal_server: 指定PrimaryDB(Oracle Net service name),作为备库处理裂隙时使用,在主备切换时方便使用.
fal_client: 指定StandbyDB(Oracle Net service name),作为备库处理裂隙时使用,在主备切换时方便使用.
standby_file_management: 指定文件自动管理方式,例如文件大小变更,AUTO会自动管理.
log_archive_dest_state_n: 指定归档目的可用性.
$ sqlplus / as sysdba;
startup mount;
alter system set db_unique_name='sales' scope=spfile;
alter system set log_archive_config='dg_config=(sales,sales_dg)';
alter system set log_archive_dest_1='LOCATION=/u01/app/oracle/oradata/sales/archive mandatory' scope=spfile;
alter system set log_archive_dest_2='SERVICE=sales_dg LGWR async valid_for=(online_logfile,primary_role) db_unique_name=sales_dg' scope=both;
alter system set fal_server=sales_dg scope=both;
alter system set fal_client=sales scope=both;
alter system set standby_file_management=auto scope=both;
alter system set log_archive_dest_state_1=enable;
alter system set log_archive_dest_state_2=enable;
创建standby重做日志文件,切换为备库时会用到.
alter database add standby logfile group 4 ('/u01/app/oracle/oradata/sales/standby0401.log','/u01/app/oracle/oradata/sales/standby0402.log') size 50M;
alter database add standby logfile group 5 ('/u01/app/oracle/oradata/sales/standby0501.log','/u01/app/oracle/oradata/sales/standby0502.log') size 50M;
alter database add standby logfile group 6 ('/u01/app/oracle/oradata/sales/standby0601.log','/u01/app/oracle/oradata/sales/standby0602.log') size 50M;
alter database add standby logfile group 7 ('/u01/app/oracle/oradata/sales/standby0701.log','/u01/app/oracle/oradata/sales/standby0702.log') size 50M;
-- 删除使用语句: alter database drop logfile group 4;
6.主库配置连接到备库的TNS.
SALES_DG =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.82)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = sales.gongyouming.com)
)
)
7.备库配置连接到主库的TNS.
SALES =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.81)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = sales.gongyouming.com)
)
)
8.启动与停止Standby.
主库启动
$ sqlplus / as sysdba;
alter database open;
主库启动后,备库如果监听无效或其它问题,远程归档会失效,可查看错误:
select * from v$archive_dest;
如果密码文件没有复制到备库,会报错:
ORA-16191: 主日志传送客户机没有登录到备用数据库
如果有错,归档无法传送到备库,备库的RFS进程是不会启动的.
备库打开到standby只读模式:
$ sqlplus / as sysdba;
startup nomount;
alter database mount standby database;
alter database open read only;
启动redo应用
alter database recover managed standby database disconnect from session;
启动redo实时应用
alter database recover managed standby database using current logfile disconnect from session;
停止Standby.
alter database recover managed standby database cancel;
shutdown immediate;
备库启动可能会出现警告,使用了过时的参数.
ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
可以查看过时参数处理掉.
select t.name from v$parameter t where t.isdeprecated='TRUE';
9.查看DataGuard的状态.
查看保护模式:
select database_role, protection_mode, protection_level from v$database;
查看standby的进程及状态:
select process,status,thread#,sequence#, block#, blocks from v$managed_standby;
查看standby重做日志及状态:
select group#,thread#,sequence#,archived,status from v$standby_log;
查看归档日志及状态:
select sequence#, first_time, next_time, applied from v$archived_log order by sequence#;
查看未应用的信息:
select name,creator,sequence#,applied,completion_time from v$archived_log where applied='NO';
查看dataguard的状态:
select name, value, datum_time from v$dataguard_stats; -- standby database
select thread#, low_sequence#, high_sequence# from v$archive_gap;