10G 物理DG搭建流程
1、查看数据库归档状态
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oracle/oradata/archive/
Oldest online log sequence 1679
Next log sequence to archive 1681
Current log sequence 1681
2、查看数据库force_logging模式
alter database force logging;
SQL> select open_mode,database_role,DB_UNIQUE_NAME,FORCE_LOGGING from v$database;
OPEN_MODE DATABASE_ROLE DB_UNIQUE_NAME FOR
---------- ---------------- ------------------------------ ---
READ WRITE PRIMARY pnrdb1 YES
3、确认数据库为企业版本
select * from v$VERSION;
BANNER Oracle Database 10g Enterprise Edition
4、RMAN全备主库
[oracle@mydg rman]$ rman target /
Recovery Manager: Release 10.2.0.4.0 - Production on Thu Jul 4 09:28:06 2013
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: PNRDB (DBID=4147734520)
RMAN> backup database format '/oracle/oradata/rman/full_backup_%U.bak' tag='full_bak';
5、创建standby 数据库控制文件
alter database create standby controlfile as '/oracle/oradata/rman/std.ctl';
6、创建pfile
create spfile from pfile='/home/oracle/pnrdbpfile.ora';
7、拷贝密码文件到standby库
可以直接通过nfs挂载
mount -t nfs -o tcp,rsize=32768,wsize=32768,hard 192.168.4.70:/oracle/oradata/rman /oracle/oradata/rman
8、备库通过RMAN还原数据库
o、确认主库备库时间同步
a、先创建相关路径
mkdir -pv /oracle/admin/pnrdb/adump
mkdir -pv /oracle/admin/pnrdb/bdump
mkdir -pv /oracle/admin/pnrdb/cdump
mkdir -pv /oracle/admin/pnrdb/udump
mkdir -pv /oracle/oradata/archive
mkdir -pv /oracle/oradata/flash_recovery_area
mkdir -pv /oracle/oradata/pnrdb
b、将主库拷贝过来的密码文件放置到$ORACLE_HOME/dbs/ 目录中
c、通过主库生成的pfile文件创建spfile文件
create spfile from pfile='/oracle/oradata/rman/pfilepnrdb.ora';
d、拷贝主库生成的standby 控制文件,重命令为pfile中的控制文件名
cp std.ctl /oracle/oradata/pnrdb/control01.ctl
cp std.ctl /oracle/oradata/pnrdb/control02.ctl
e、设置好oracle用户环境变量,写入.bashrc文件中
export ORACLE_BASE=/oracle
export ORACLE_HOME=$ORACLE_BASE/10g
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=pnrdb
f、使配置生效
source .bashrc
g、将数据库启动到mount状态
h、使用rman还原数据库
restore database;
9、修改db_unique_name
alter system set db_unique_name='pypnrdb' scope=spfile;
修改service_names
alter system set service_names='pnrdb';
10、关闭备库,重启备库的lisnter,再启动备库到mount状态。
11、配置tnsnames.ora
12、在备库上添加standby log
alter database add standby logfile group 4 ('/oracle/oradata/pnrdb/stdby_redo04_a.log','/oracle/oradata/pnrdb/stdby_redo04_b.log') size 256m, group 5 ('/oracle/oradata/pnrdb/stdby_redo05_a.log','/oracle/oradata/pnrdb/stdby_redo05_b.log') size 256m, group 6 ('/oracle/oradata/pnrdb/stdby_redo06_a.log','/oracle/oradata/pnrdb/stdby_redo06_b.log') size 256m, group 7 ('/oracle/oradata/pnrdb/stdby_redo07_a.log','/oracle/oradata/pnrdb/stdby_redo07_b.log') size 256m;
13、修改DG相关配置
主库配置修改如下
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(pnrdb1,pypnrdb)';
alter system set log_archive_dest_1='LOCATION=/oracle/oradata/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=pnrdb1';
alter system set LOG_ARCHIVE_DEST_2='SERVICE=pypnrdb LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=pypnrdb';
备库配置
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(pnrdb1,pypnrdb)';
alter system set LOG_ARCHIVE_DEST_1='LOCATION=/oracle/oradata/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=pypnrdb';
alter system set standby_file_management='AUTO';
alter system set fal_server='pnrdbmaster';
alter system set fal_client='pypnrdb';
14、备库开启关闭日志应用
a、开启日志应用
alter database recover managed standby database using current logfile disconnect from session;
b、关闭日志应用
alter database recover managed standby database cancel;
15、查看日志应用情况
select to_char(START_TIME,'yyyymmdd hh24:mi:ss') start_time ,to_char(TIMESTAMP,'yyyymmdd hh24:mi:ss') timestamp from v$recovery_progress;
select round((sysdate-max(timestamp))*86400) from v$recovery_progress;
查看是否使用Real-Time apply:
Select recovery_mode from v$archive_dest_status;
日志切换(主库)
select max(sequence#) from v$archived_log;
select max(sequence#) from v$log_history;
select group#,sequence#,archived,status from v$log;
select name,sequence#,applied from v$archived_log;
select sequence#,applied from v$archived_log;
物理备库转逻辑备库
a、物理备库先停止日志应用
alter database recover managed standby database cancel;
b、主库生产logstandby 元数据
EXECUTE DBMS_LOGSTDBY.BUILD;
c、备库转换成逻辑DG
ALTER DATABASE RECOVER TO LOGICAL STANDBY LGDG;
d、关闭备库并且重启到mount状态
shutdown immeidate
startup mount
e、已经resetlogs方式打开逻辑DG
alter database open resetlogs;
f、开启实时日志应用
alter database start logical standby apply immediate;
g、关闭日志应用
alter database stop logical standby apply;
16、逻辑dg
select round((sysdate - applied_time) * 86400) from v$logstdby_progress;
主备库切换
一、 Switchover
一般SWITCHOVER切换都是计划中的切换,特点是在切换后,不会丢失任何的数据,而且这个过程是可逆的,整个DATA GUARD环境不会被破坏,原来DATA GUARD环境中的所有物理和逻辑STANDBY都可以继续工作。
在进行DATA GUARD的物理STANDBY切换前需要注意:
1)确认主库和从库间网络连接通畅;
2)确认没有活动的会话连接在数据库中;
3)PRIMARY数据库处于打开的状态,STANDBY数据库处于MOUNT状态;
4)确保STANDBY数据库处于ARCHIVELOG模式;
5)如果设置了REDO应用的延迟,那么将这个设置去掉;
6)确保配置了主库和从库的初始化参数,使得切换完成后,DATA GUARD机制可以顺利的运行。
主库:
1. 查看switchover 状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO STANDBY
附: A:switchover_status出现session active/not allowed
当出现session active的时候表示还有活动的session,则运行
Alter database commit to switchover to physical standby with session shutdown;
当出现not allowed时,在官方文档说转换会不成功,但是我测试的时候成功了。
B.ora- 01153: an incompatible media recovery is active
运行下面代码
Alter database recover managed standby database finish;
或者Alter database recover managed standby database finish force;
Alter database recover managed standby database disconnect from session;
2 切换成备库
SQL>Alter database commit to switchover to physical standby with session shutdown;
或者
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY;
Database altered.
3 启动到mount和应用日志状态
SQL> SHUTDOWN IMMEDIATE
SQL> startup nomount;
SQL> alter database mount standby database;
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
4. 查看数据库模式
SQL>select dest_name,status,database_mode,recovery_mode,protection_mode from v$archive_dest_status;
SQL>select status,database_mode from v$archive_dest_status;
备库:
1.查看switchover状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
TO PRIMARY
附:若不是用此语句切换:ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY with session shutdown
补充:若出现:ORA-16139: media recovery required
是因为没有执行:alter database recover managed standby database disconnect from session;
2. 切换成主库
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
Database altered.
SQL> shutdown immediate;
SQL> startup;
SQL> alter system switch logfile;
3. 查看数据库模式
SQL>select dest_name,status,database_mode,recovery_mode,protection_mode from v$archive_dest_status;
SQL>select status,database_mode from v$archive_dest_status;
验证同步:
SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
78
二. Failovers:
FAILOVER切换一般是PRIMARY数据库发生故障后的切换,这种情况是STANDBY数据库发挥其作用的情况。这种切换发生后,可能会造成数据的丢失。而且这个过程不是可逆的,DATA GUARD环境会被破坏。
由于PRIMARY数据库已经无法启动,所以FAILOVER切换所需的条件并不多,只要检查STANDBY是否运行在最大保护模式下,如果是的话,需要将其置为最大性能模式,否则切换到PRIMARY角色也无法启动。
1. 查看是否有日志GAP,没有应用的日志:
SQL> SELECT UNIQUE THREAD#, MAX(SEQUENCE#) OVER(PARTITION BY THREAD#) LAST FROM V$ARCHIVED_LOG;
SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
如果有,则拷贝过来并且注册
SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE '路径';
重复查看直到没有应用的日志:
2. 然后停止应用归档:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
Database altered.
3. 下面将STANDBY数据库切换为PRIMARY数据库:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH;
或 SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH FORCE;
Database altered.
SQL> SELECT DATABASE_ROLE FROM V$DATABASE;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
Database altered.
SQL> ALTER DATABASE OPEN; 或者 shutdown immediate+startup
Database altered.
检查数据库是否已经切换成功:
SQL> SELECT DATABASE_ROLE FROM V$DATABASE;
DATABASE_ROLE
----------------
PRIMARY
至此,FAILOVER切换完成。这个时候应该马上对新的PRIMARY数据库进行备份。
One Response