本文档针对初级DBA
此时的环境应该是:主库已经正常使用,有数据;备库操作系统和数据库软件已经安装完毕。
1、设置 prod库
1.1、用系统oracle用户创建归档目录
mkdir /u01/archivelog
ls -l /u01/
1.2、指定归档路径及设置为归档模式
alter system set log_archive_dest_1='location=/u01/archivelog';
archive log list;
shutdown immediate;
startup mount;
alter database archivelog;
archive log list;
alter database open;
1.3、启用强制日志功能
alter database force logging;
select log_mode,force_logging from v$database;
1.4、创建pfile
create pfile from spfile;
1.5、用oracle用户把pfile文件、口令文件传输到standby
cd $ORACLE_HOME/dbs
scp orapwprod [email protected]:/u01/oracle/db_1/dbs
scp initprod.ora [email protected]:/u01/oracle/db_1/dbs
2、设置 standby数据库
2.1、把口令文件和pfile名字改成standby的
cd $ORACLE_HOME/dbs
mv orapwprod orapwstandby
mv initprod.ora initstandby.ora
10.2、修改 initstandby.ora
把上面标注的prod 都改成standby
注意db_name的prod 值不要修改
在最后添加以下参数
*.db_unique_name='standby' 确认为备库的标识名称
*.db_file_name_convert='/u01/oradata/prod','/u01/oradata/standby' 转换数据文件目录
*.log_file_name_convert='/u01/oradata/prod','/u01/oradata/standby' 转换日志文件目录
*.log_archive_config='dg_config=(prod,standby)' 归档日志配置(主,备唯一标识名称)
*.log_archive_dest_1='location=/u01/oradata/standby/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=standby' 备库归档本地位置
*.log_archive_dest_2='service=prod lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=prod' 备库归档远程目标库位置
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.log_archive_max_processes=10
*.fal_server=prod
*.fal_client=standby
*.standby_file_management=auto
2.3、创建一些目录
根据参数文件里提到的目录进行创建,否则启动到nomount时会报错。
下面是oracle10g举例。11g数据库目录减少。注意使用oracle用户进行创建。
mkdir -p /u01/admin/standby/adump
mkdir -p /u01/admin/standby/bdump
mkdir -p /u01/admin/standby/cdump
mkdir -p /u01/admin/standby/udump
mkdir -p /u01/admin/standby/dpdump
ls -l /u01/admin/standby
mkdir -p /u01/oradata/standby/archivelog
ls -l /u01/oradata/standby
2.4、配置连接字符串,即创建tnsname
netmgr
2.5standby操作
用pfile 启动到nomount 状态
sqlplus / as sysdba
create spfile from pfile='?/dbs/initstandby.ora';
startup nomount
3、配置 prod数据库
再次回到prod库的设置
3.1配置prod库参数
sqlplus / as sysdba
alter system set log_archive_config='dg_config=(prod,standby)';
alter system set log_archive_dest_1='location=/u01/oradata/prod/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=prod';
alter system set log_archive_dest_2='service=standby lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=standby';
alter system set log_archive_dest_state_1=enable;
alter system set log_archive_dest_state_2=enable;
alter system set log_archive_max_processes=10;
alter system set fal_server=standby;
alter system set fal_client=prod;
alter system set standby_file_management=auto;
还有三需要重启才生效的参数也设置一下
alter system set db_unique_name='prod'scope=spfile;
alter system set db_file_name_convert='/u01/oracle/standby','/u01/oracle/prod' scope=spfile;
alter system set log_file_name_convert='/u01/oracle/standby','/u01/oracle/prod' scope=spfile;
3.2备份prod库
创建备份目录(主备操作系统都要创建)
mkdir -p /u01/backupset
ls -l /u01
登陆到rman开始备份
rman target /
run {
allocate channel d1 type disk;
backup format '/backup/df_t%t_s%s_p%p' database;
sql 'alter system archive log current';
backup format '/backup/al_t%t_s%s_p%p' archivelog all;
release channel d1;
}
3.3创建controlfile
alter database create standby controlfile as '/u01/standby.ctl';
3.4把备份集目录传输到standby的/u01目录下
scp -r /u01/backupset [email protected]:/u01/backupset
scp /u01/standby.ctl 192.168.2.131:/u01/oracle
4、备库进行操作
4.1恢复控制文件
restore standby controlfile from '/backup/ctlc-2687307724-20170214-00';
4.2恢复数据文件
run {
restore database;
switch datafile all;
recover database;
}
4.3配置 standby数据库
查询standby库状态
select status from v$instance;
现在数据库为mount 状态
创建备库的standby日志,日志大小和redo大小一致,数量比redo日志多一组。
alter database add standby logfile('/u01/oracle/standby/standby01.log') size 50m;
alter database add standby logfile('/u01/oracle/standby/standby02.log') size 50m;
alter database add standby logfile('/u01/oracle/standby/standby03.log') size 50m;
alter database add standby logfile('/u01/oracle/standby/standby04.log') size 50m;
4.4在prod库切换几个日志
sqlplus / as sysdba
alter system switch logfile;
在standby 库查看一下是否有归档日志了
exit
ls -l /u01/oradata/standby/archivelog
日志可以传输了说明通道配置没有问题,如果没有日志,说明tnsname或者pfile参数配置有问题。
4.5备库执行日志应用同步语句,开始运行物理ADG
sqlplus / as sysdba
alter database open;
alter database recover managed standby database using current logfile disconnect from session;
因为有备重做日志,所以可以加using current logfile 语句,实现实时应用
select sequence#, applied from v$archived_log where applied='YES' order by sequence#;
yes 说明日志同步了
4.6进一步验证数据是否同步
在prod库上执行:
conn scott/tiger
create table t(i int);
insert into t values(1);
commit;
conn / as sysdba
alter system switch logfile;
在standby 库上执行:
conn scott/tiger
select * from t;
数据已经同步,物理ADG 搭建成功!
看一下数据库的open 状态
conn / as sysdba
select open_mode from v$database;
注意:物理备库open 是默认read only模式的
启动数据同步语句后,数据库又变为mounted状态
注意:在测试数据同步的时候请不要用sys 用户,不然可能同步不了,这里测试都是用scott用户
注意:oracle仅在11g数据库中支持ADG功能,11g数据库要想验证数据是否同步,建议创建数据文件,查看备库是否也同步创建。
===========================================================================================
5、切换物理备库
5.1在prod库上执行:
select switchover_status from v$database;
alter database commit to switchover to physical standby;
这个命令要执行蛮长时间,请耐心等待
注意:上面switchover_status 的值如果是TO STANDBY,可以直接switchover,如果是sessions active,则需要在switchover 的命令后面加上with session shutdown,比如
alter database commit to switchover to physical standby with session shutdown;
select status from v$instance;
检查一下数据库状态
shutdown immediate
startup nomount
alter database mount standby database;
这条命令就是把主库切成备库
5.2在standby库上执行:
select sequence#, applied from v$archived_log where applied='YES' order by sequence#;
已经和刚才的主库同步完日志
select switchover_status from v$database;
alter database commit to switchover to primary;这条命令就是把备库切成主库
select status from v$instance;查看一下数据库状态
shutdown immediate
startup
5.3在prod库上执行:
创建备日志
alter database add standby logfile('/u01/oracle/prod/standby01.log') size 50m;
alter database add standby logfile('/u01/oracle/prod/standby02.log') size 50m;
alter database add standby logfile('/u01/oracle/prod/standby03.log') size 50m;
alter database add standby logfile('/u01/oracle/prod/standby04.log') size 50m;
5.4执行同步语句
alter database recover managed standby database using current logfile disconnect from session;
select sequence#, applied from v$archived_log where applied='YES'order by sequence#;
5.5进一步验证数据是否同步,注意11g数据库验证方法和上面一样,下面验证步骤是oracle10g步骤。
在standby 库上执行:
conn scott/tiger
insert into t values(2);
commit;
conn / as sysdba
alter system switch logfile;
在prod库上执行:
alter database recover managed standby database cancel;
alter database open;
conn scott/tiger
select * from t;
物理ADG切换成功!
conn / as sysdba
select open_mode from v$database;
alter database recover managed standby database using current logfile disconnect from session;
select open_mode from v$database;
恢复成同步模式
===============================================================================
6、再切换一次,切换来原来的状态,步骤是一样的
这里只是为了验证是否ok
6.1在standby 库上执行:
select switchover_status from v$database;
alter database commit to switchover to physical standby;
shutdown immediate
startup nomount
alter database mount standby database;
6.2在prod库上执行:
select sequence#, applied from v$archived_log where
applied='YES' order by sequence#;
select switchover_status from v$database;
alter database commit to switchover to primary;
select status from v$instance;
查看一下数据库状态
shutdown immediate
startup
6.3在standby 库上执行:
alter database recover managed standby database using current logfile disconnect from session;
6.4在prod库上执行:
conn scott/tiger
insert into t values(3);
commit;
conn / as sysdba
alter system switch logfile;
在standby 库上执行:
alter database recover managed standby database cancel;
alter database open;
conn scott/tiger
select * from t;
conn / as sysdba
alter database recover managed standby database using current logfile disconnect from session;
select open_mode from v$database;
恢复到了原来的状态,即prod 是主库,standby是备库