export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
export ORACLE_PATH=$ORACLE_BASE/common/oracle/sql:.:$ORACLE_HOME/rdbms/admin
export ORACLE_SID=orcl
一 主库jchdg1配置,放在第一台机器上
1.启动归档模式并打开force logging
export ORACLE_SID=jchdg1
sqlplus / as sysdba
archive log list
shutdown immediate
startup mount
alter database archivelog;
alter database open;
archive log list
alter database force logging;
select force_logging from v$database;
2.添加standby logfile 用于接收来自主库的redo日志
select group# from v$log;
alter database add standby logfile group 4 ('/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog4a.log','/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog4b.log') size 100m;
alter database add standby logfile group 5 ('/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog5a.log','/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog5b.log') size 100m;
alter database add standby logfile group 6 ('/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog6a.log','/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog6b.log') size 100m;
alter database add standby logfile group 7 ('/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog7a.log','/u01/app/oracle/product/10.2.0/oradata/jchdg1/standbylog7b.log') size 100m;
select group#,status from v$standby_log;
3.配置网络
在tnsnames.ora文件中添加dg2连接串
DG2=
(DESCRIPTION=
(ADDRESS=(PROTOCOL=tcp)(HOST=dg2)(PORT=1521))
(CONNECT_DATA=
(SERVER=DEDICATED)
(SERVICE_NAME=jchdg2)
)
4.修改主库dg1参数文件
sqlplus / as sysdba
create pfile from spfile;
exit
在第一台机器上创建jchdg1库的归档目录
mkdir -p /home/oracle/arch/jchdg1
在第二台机器上创建jchdg2库的归档目录
mkdir -p /home/oracle/arch/jchdg2
调整主数据库pfile参数文件内容
(1)删除local_listener参数,确保jchdg2动态注册到1521端口上
*.local_listener='LISTENER2'
(2)保留其他参数的同时添加如下参数,添加主库角色参数
##parameter for Primary Database
db_name='jchdg1' 数据库名字,主库与备库必须一致,这个参数已经有了
db_unique_name=jchdg1 数据库实例名
log_archive_format=%t_%s_%r.arc 归档日志格式
log_archive_config='DG_CONFIG=(LEO1,LEO3)' 罗列一个DG中所以实例名
log_archive_dest_1='location=/home/oracle/arch/LEO1 valid_for=(all_logfiles,all_roles)
db_unique_name=LEO1' 本地LEO1归档日志目录和实例名
log_archive_dest_2='service=LEO3 lgwr sync valid_for=(online_logfiles,primary_role)
db_unique_name=LEO3' 备库 LGWR 同步
log_archive_dest_state_1=enable 是否启动归档->enable
log_archive_dest_state_2=enable 是否启动归档->enable
remote_login_passwordfile=exclusive 远程登录密码,要求DG中所有sys用户密码一致
##parameter for Standby Database
(3)添加备库角色参数
fal_server=LEO3 fetch_archive_log_server=抓取服务端,指向主库连接串
fal_client=LEO1 fetch_archive_log_client=抓取客户端,指向备库连接串
standby_file_management=auto 备库是否动态关联主库的动作
db_file_name_convert='LEO3','LEO1' 主备库数据文件转换目录
log_file_name_convert='LEO3','LEO1' 主备库日志文件转换目录
(4)通过pfile重建spfile
sqlplus / as sysdba
shutdown immediate
create spfile from pfile;
二 备库dg2配置,在第二台机器上操作
1.创建密码文件
注意密码文件需要保持与主库一致,我们采用复制主库密码文件到备库的方式,如果用重建的方式可能引起两边不一致报错
cd /u01/app/oracle/product/10.2.0/db_1/dbs
scp orapwjchdg1 dg2:/u01/app/oracle/product/10.2.0/db_1/dbs/
mv orapwjchdg1 orapwjchdg2
2.创建转储目录
mkdir -p /u01/app/oracle/product/10.2.0/db_1/admin/jchdg2/adump
mkdir -p /u01/app/oracle/product/10.2.0/db_1/admin/jchdg2/bdump
mkdir -p /u01/app/oracle/product/10.2.0/db_1/admin/jchdg2/cdump
mkdir -p /u01/app/oracle/product/10.2.0/db_1/admin/jchdg2/udump
3.冷备主库
(1)主库冷备tar包
cd $ORACLE_BASE/product/10.2.0/oradata
tar -czvf jchdg1.tar jchdg1
(2)拷贝jchdg1.tar 和 initjchdg1.ora文件到备库并重命名
scp jchdg1.tar dg2:$ORACLE_BASE/product/10.2.0/oradata
scp initjchdg1.ora dg2:/u01/app/oracle/product/10.2.0/db_1/dbs/
mv initjchdg1.ora initjchdg2.ora
(3)解开tar包还原jchdg1主库数据文件
tar -xzvf jchdg1.tar
mv jchdg1 jchdg2
(4)删除jchdg2中原主库控制文件
cd /u01/app/oracle/product/10.2.0/oradata/jchdg2
rm control01.ctl
rm control02.ctl
rm control03.ctl
4.生成备库的控制文件
jchdg1
sqlplus / as sysdba
startup mount
alter database create standby controlfile as '/home/oracle/control01.ctl'; 创建具有备库属性的控制文件
scp control01.ctl dg2:/u01/app/oracle/product/10.2.0/oradata/jchdg2/
cp control01.ctl /u01/app/oracle/product/10.2.0/oradata/jchdg2/control02.ctl
cp control01.ctl /u01/app/oracle/product/10.2.0/oradata/jchdg2/control03.ctl
5.准备备库的参数文件initjchdg2.ora
vim initjchdg2.ora
(1)修改控制文件内容
*.control_files='/u01/app/oracle/product/10.2.0/oradata/jchdg2/control01.ctl','/u01/app/oracle/product/10.2.0/oradata/jchdg2/control02.ctl','/u01/app/oracle/product/10.2.0/oradata/jchdg2/control03.ctl'
(2)需要修改jchdg1->jchdg2
DB_NAME='jchdg1'
DB_UNIQUE_NAME=jchdg2
LOG_ARCHIVE_CONFIG='DG_CONFIG=(jchdg1,jchdg2)'
LOG_ARCHIVE_DEST_1=
'LOCATION=/home/oracle/arch/jchdg2
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=jchdg2'
LOG_ARCHIVE_DEST_2=
'SERVICE=LEO1 LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=jchdg1'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
FAL_SERVER=jchdg1
FAL_CLIENT=jchdg2
DB_FILE_NAME_CONVERT='jchdg1','jchdg2'
LOG_FILE_NAME_CONVERT='jchdg1','jchdg2'
STANDBY_FILE_MANAGEMENT=AUTO
6.启动备库
export ORACLE_SID=jchdg2
sqlplus / as sysdba
create spfile from pfile;
startup nomount
tail -20f alert_jchdg2.log
alter database mount;
startup mount
主库
alter database open;
可以看到, 已经在向备库传输日志:
注:备库只有在mount状态的时候可以应用日志,read only状态不能应用日志
应用日志调用2个MRP进程启动jchdg2库日志应用并后台运行
alter database recover managed standby database disconnect from session parallel 2;
或前台运行
alter database recover managed standby database parallel 2;
7.检查备库归档日志应用情况和主备库状态
select sequence#,applied from v$archived_log;
查看jchdg1库状态
select database_role,switchover_status from v$database;
查看jchdg2库状态
select database_role,switchover_status from v$database;
检查tnsnames.ora和监听状态,重启主库来重新探测备库传输日志
alter system switch logfile;
三 第一次切换
换前jchdg1库上创建swtich_tbs表空间 -> 切换 -> 检查表空间和对应的数据文件是否应用到了jchdg2库
创建表空间
create tablespace swtich_tbs datafile '/u01/app/oracle/product/10.2.0/oradata/jchdg1/swtich_tbs.dbf' size 10m;
这时jchdg2的tablespace:
切换日志并完成归档
alter system archive log current;
jchdg1的alert日志:
jchdg2的alert日志:
时刻打开alert日志观察jchdg2库的恢复状况
cd /u01/app/oracle/product/10.2.0/db_1/admin/jchdg2/bdump
tail -20f alert_jchdg2.log
select * from v$tablespace;
查看jchdg2的tablespace:发现多了jchdg1库新建的switch_tbs表空间:
查看jchdg1库状态
select database_role,switchover_status from v$database;
查看jchdg2库状态
select database_role,switchover_status from v$database;
主库切换成备库,并终止存活的会话..必须先主库切换到 备库,再备库切换到主库。
alter database commit to switchover to physical standby with session shutdown;
主库日志:
select status,instance_name from v$instance; 主库处于异常关闭状态
shutdown immediate
startup mount; --要切换成备库,所以是mount状态
select database_role,switchover_status from v$database;
alter database recover managed standby database disconnect from session parallel 2;
主库切换成了备库:
备库切换成主库,并终止存活的会话
【alter database recover managed standby database cancel;】
select database_role,switchover_status from v$database;
alter database commit to switchover to primary with session shutdown;
alter database open;
select database_role,switchover_status from v$database;
select sequence#,applied from v$archived_log;
四 第二次切换
切换前jchdg2库上创建一张表 -> 切换 -> 检查表和记录是否应用到jchdg1库上
create table jchdg2 as select * from dba_objects;
select count(*) from jchdg2;
日志切换并完成归档
alter system archive log current;
备库jchdg1的alert中记录着恢复的过程
cd /u01/app/oracle/product/10.2.0/db_1/admin/jchdg1/bdump
tail -20f alert_jchdg1.log
jchdg2的日志:
jchdg1的日志:
jchdg1上检查jchdg2表,如果可以查到说明我们传递成功
select count(*) from jchdg2;
jchdg2库切换成备库状态
select database_role,switchover_status from v$database;
alter database commit to switchover to physical standby with session shutdown;
shutdown immediate
startup mount;
alter database recover managed standby database disconnect from session parallel 2;
select database_role,switchover_status from v$database;
jchdg1库切换成主库状态
【alter database recover managed standby database cancel;】
select database_role,switchover_status from v$database;
alter database commit to switchover to primary with session shutdown;
alter database open;
select database_role,switchover_status from v$database;
jchdg2备库还可以取消日志应用,启动到read only状态(只可读不可写,可以对外提供报表服务)
alter database recover managed standby database cancel;
alter database open read only;
select open_mode from v$database;
select database_role,switchover_status from v$database;
小结:到此我们的DG实验讲解完成,大家在学习的过程中重点理解DG应用场景和实现原理以及优缺点,在完成实验之后你应该可以在遇到故障时知道如何处理。