ORACLE 中的RAC特性保护的是实例,RMAN特性保持的是数据,而Dataguard特性则是连实例带数据一块"保"了,三个特性相互配合能够在一定程度上构建一个安全可靠高可用的ORACLE数据库平台。
之前三思笔记系列文章分别描述过RAC的创建、RMAN管理以及Dataguard特性应用等方面的内容,本文将通过实践,演示如何为RAC数据库创建Dataguard配置。
Dataguard 环境的配置就跟谈恋爱似的,不是一个人说了算,必须要两个人(Primary db+Standby db)相互配合。不过在这两个真正搭上线之前,临出门拾掇拾掇还是非常有必要的,最起码得照照镜子,看看眼神够不够犀利,腰肢够不够芙蓉,做好往前推三百年,往后推三百年,总共六百年无人超越的思想准备。
我个人认为,DBA是个很严谨的岗位,因此同样要求从事这项工作的人也尽可能严谨,准备这个事儿呢,是为了让后续的操作更稳妥。对于Dataguard环境的创建,这个准备并不复杂,就当是环境初始化吧,如果看过之前的三思笔记--一步一步学Dataguard,那么本步准备工作就可以直接跳过了。
设定环境如下:
Primary :
Db_unique_name :jssdb
IP :192.168.10.11/12
DB Version :10.2.0.4
Storage :ASM
Standby :
Db_unique_name :jssstd
IP :192.168.10.101
DB Version :10.2.0.4
Storage :local disk准备工作如下:
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------------------------------
+ASMDISK1/jssdb/datafile/system.260.703671683
+ASMDISK1/jssdb/datafile/undotbs1.259.703671695
+ASMDISK1/jssdb/datafile/sysaux.266.703671697
+ASMDISK1/jssdb/datafile/undotbs2.258.703671705
+ASMDISK1/jssdb/datafile/users.257.703671709
+ASMDISK1/jssdb/datafile/jsstbs.269.703779631
6 rows selected.
SQL> select name from v$tempfile;
NAME
----------------------------------------------------------------------------------------------------------
+ASMDISK1/jssdb/tempfile/temp.265.703671701
SQL> select member from v$Logfile;
MEMBER
---------------------------------------------------------------------------------------------------------
+ASMDISK1/jssdb/onlinelog/group_1.263.703671679
+ASMDISK1/jssdb/onlinelog/group_2.261.703671681
+ASMDISK1/jssdb/onlinelog/group_3.256.703672257
+ASMDISK1/jssdb/onlinelog/group_4.268.703672257要启动Standby端的实例,需要相应的初始化参数文件,这里我们可以通过Primary spfile创建出Standby的pfile,操作方法不需要多说了吧,最后修改的standby端pfile内容如下(注意确保相关路径的正确哟):
*.audit_file_dest=¨/data/ora10g/admin/jssstd/adump¨
*.background_dump_dest=¨/data/ora10g/admin/jssstd/bdump¨
*.compatible=¨10.2.0.1.0¨
*.control_files=¨/data1/jssstd/control01.ctl¨,¨/data1/jssstd/control02.ctl¨,¨/data1/jssstd/control03.ctl¨
*.core_dump_dest=¨/data/ora10g/admin/jssstd/cdump¨
*.db_block_size=8192
*.db_domain=¨¨
*.db_file_multiblock_read_count=16
*.db_name=¨jssdb¨
*.db_unique_name=¨jssstd¨
*.job_queue_processes=10
*.log_archive_dest_1=¨location=/data1/jssstd/arclog valid_for=(all_logfiles,all_roles) db_unique_name=jssstd¨
*.open_cursors=300
*.pga_aggregate_target=94371840
*.processes=150
*.remote_login_passwordfile=¨exclusive¨
*.sga_target=283115520
*.undo_management=¨AUTO¨
*.undo_tablespace=¨UNDOTBS1¨
*.user_dump_dest=¨/data/ora10g/admin/jssstd/udump¨
提示一点,此时尚未添加任何与dataguard相关的初始化参数,接下来就要对此进行处理,增加参数如下:
*.log_archive_config=¨dg_config=(jssdb,jssstd)¨
*.standby_file_management=¨AUTO¨
*.db_file_name_convert=¨+ASMDISK1/jssdb/datafile¨,¨/data1/jssstd¨,¨+ASMDISK1/jssdb/tempfile¨,¨/data1/jssstd¨
*.log_file_name_convert=¨+ASMDISK1/jssdb/onlinelog¨,¨/data1/jssstd¨
*.fal_server=jssdb
*.fal_client=jssstd由于asm存储文件时自动进行了分目录处理,因此这里在设置db_file_name_convert参数时,也要注意设置相应的转换,否则可能导致文件创建时出错的,切记切记。
客户端初始化参数文件创建之后,就可以通过它创建Standby数据库的spfile,并启动实例了,如下:
SQL> create spfile from pfile=¨/data/ora10g/admin/jssstd/pfile/jssstd.ora¨;
File created.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 2083368 bytes
Variable Size 88081880 bytes
Database Buffers 188743680 bytes
Redo Buffers 6303744 bytes复制文件,包括Primary端创建的最新备份、Standby控制文件。最好是能在复制到Standby端服务器时,保持与Primary端相同的目录结构,这样在恢复操作时会更简单一些,否则的话,再执行恢复前,还需要先用catalog注册这些备份,否则恢复时可能会提供找不到必要的备份。
密钥文件的密钥注意要保持与Primary端完全相同,或者直接复制Primary端的密钥文件过来,注意复制的话一定是要复制到standby端$ORACLE_HOME/dbs目录下,并注意按照命名规则修改名称!
重头戏来了,创建Standby数据库!这里三思不是通过备份恢复哟,而是决定通过rman的duplicate命令来实施创建,操作相当简单!
我们在Primary端执行创建,连接到RMAN命令行模式,执行命令如下:
[oracle@jssdbn2 ~]$ rman target / auxiliary sys/123456@jssstd
Recovery Manager: Release 10.2.0.4.0 - Production on Thu Apr 15 13:59:51 2010
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: JSSDB (DBID=953576437)
connected to auxiliary database: JSSDB (not mounted)接下来只需要一条命令:
RMAN> duplicate target database for standby;
Starting Duplicate Db at 15-APR-10
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=156 devtype=DISK
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: sid=155 devtype=DISK
contents of Memory Script:
{
restore clone standby controlfile;
sql clone ¨alter database mount standby database¨;
}
executing Memory Script
Starting restore at 15-APR-10
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /data/backup/0glb6gts_1_1
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/data/backup/0glb6gts_1_1 tag=TAG20100415T133555
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:05
output filename=/data1/jssstd/control01.ctl
output filename=/data1/jssstd/control02.ctl
output filename=/data1/jssstd/control03.ctl
Finished restore at 15-APR-10
sql statement: alter database mount standby database
released channel: ORA_AUX_DISK_1
released channel: ORA_AUX_DISK_2
contents of Memory Script:
{
set newname for tempfile 1 to
"/data1/jssstd/temp.265.703671701";
switch clone tempfile all;
set newname for datafile 1 to
"/data1/jssstd/system.260.703671683";
set newname for datafile 2 to
"/data1/jssstd/undotbs1.259.703671695";
set newname for datafile 3 to
"/data1/jssstd/sysaux.266.703671697";
set newname for datafile 4 to
"/data1/jssstd/undotbs2.258.703671705";
set newname for datafile 5 to
"/data1/jssstd/users.257.703671709";
set newname for datafile 6 to
"/data1/jssstd/jsstbs.269.703779631";
restore
check readonly
clone database
;
}
executing Memory Script
executing command: SET NEWNAME
renamed temporary file 1 to /data1/jssstd/temp.265.703671701 in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 15-APR-10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=155 devtype=DISK
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: sid=156 devtype=DISK
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /data1/jssstd/system.260.703671683
restoring datafile 00002 to /data1/jssstd/undotbs1.259.703671695
restoring datafile 00006 to /data1/jssstd/jsstbs.269.703779631
channel ORA_AUX_DISK_1: reading from backup piece /data/backup/0alb5tv2_1_1
channel ORA_AUX_DISK_2: starting datafile backupset restore
channel ORA_AUX_DISK_2: specifying datafile(s) to restore from backup set
restoring datafile 00003 to /data1/jssstd/sysaux.266.703671697
restoring datafile 00004 to /data1/jssstd/undotbs2.258.703671705
restoring datafile 00005 to /data1/jssstd/users.257.703671709
channel ORA_AUX_DISK_2: reading from backup piece /data/backup/0blb5tvj_1_1
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/data/backup/0alb5tv2_1_1 tag=TAG20100415T081218
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:05
channel ORA_AUX_DISK_2: restored backup piece 1
piece handle=/data/backup/0blb5tvj_1_1 tag=TAG20100415T081218
channel ORA_AUX_DISK_2: restore complete, elapsed time: 00:01:09
Finished restore at 15-APR-10
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
datafile 1 switched to datafile copy
input datafile copy recid=7 stamp=716401106 filename=/data1/jssstd/system.260.703671683
datafile 2 switched to datafile copy
input datafile copy recid=8 stamp=716401106 filename=/data1/jssstd/undotbs1.259.703671695
datafile 3 switched to datafile copy
input datafile copy recid=9 stamp=716401106 filename=/data1/jssstd/sysaux.266.703671697
datafile 4 switched to datafile copy
input datafile copy recid=10 stamp=716401106 filename=/data1/jssstd/undotbs2.258.703671705
datafile 5 switched to datafile copy
input datafile copy recid=11 stamp=716401106 filename=/data1/jssstd/users.257.703671709
datafile 6 switched to datafile copy
input datafile copy recid=12 stamp=716401106 filename=/data1/jssstd/jsstbs.269.703779631
Finished Duplicate Db at 15-APR-10Standby 创建完成!注意,我说的是Standby创建完成,没说Dataguard配置完成哟!接下来还有工作要做,首先要做的是检查Standby数据库的状态,开个sqlplus连接到Standby数据库,此时Standby应已处于mount模式:
[oracle@jssnode1 ~]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Apr 15 16:22:07 2010
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> set line 150 pages 1000
SQL> select database_role,protection_level,open_mode from v$database;
DATABASE_ROLE PROTECTION_LEVEL OPEN_MODE
---------------- -------------------- ----------
PHYSICAL STANDBY MAXIMUM PERFORMANCE MOUNTED
SQL> select name from v$datafile;
NAME
------------------------------------------------------------------------------------------------------------------------------------------------------
/data1/jssstd/system.260.703671683
/data1/jssstd/undotbs1.259.703671695
/data1/jssstd/sysaux.266.703671697
/data1/jssstd/undotbs2.258.703671705
/data1/jssstd/users.257.703671709
/data1/jssstd/jsstbs.269.703779631
6 rows selected.
SQL> select name from v$tempfile;
NAME
------------------------------------------------------------------------------------------------------------------------------------------------------
/data1/jssstd/temp.265.703671701
SQL> select member from v$logfile;
MEMBER
------------------------------------------------------------------------------------------------------------------------------------------------------
/data1/jssstd/group_1.263.703671679
/data1/jssstd/group_2.261.703671681
/data1/jssstd/group_3.256.703672257
/data1/jssstd/group_4.268.703672257可上述结果可以看出,文件路径也均已被转换,其实这个结果从duplicate的日志就可以被得知,执行的查询更坚定的证实了这一点。对于正式环境,建议创建适当组数的standby redolog,以提高日志的应用效率。Standby redolog的创建和管理方式在一步一步学Dataguard中有详细描述,这里不再举例说明。
接下来要修改Primary数据库,以发送日志到Standby端,具体操作如下,连接到RAC任意结点,然后执行下列语句:
SQL> alter system set log_archive_config=¨dg_config=(jssdb,jssstd)¨;
System altered.
SQL> alter system set log_archive_dest_2=¨service=jssstd lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=jssstd¨;
System altered.Primary 端切换一下日志:
SQL> alter system switch logfile;
System altered.
SQL> select thread#,max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1 295
2 127转到Standby端执行查询:
SQL> select thread#,max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
2 127
SQL> select process,status,client_process,thread#,sequence# from v$managed_standby;
PROCESS STATUS CLIENT_P THREAD# SEQUENCE#
--------- ------------ -------- ---------- ----------
ARCH CONNECTED ARCH 0 0
ARCH CONNECTED ARCH 0 0
RFS IDLE UNKNOWN 0 0
RFS IDLE LGWR 2 128尝试启动应用,然后再次执行查询:
SQL> alter database recover managed standby database disconnect from session;
Database altered.
SQL> select process,status,client_process,thread#,sequence# from v$managed_standby;
PROCESS STATUS CLIENT_P THREAD# SEQUENCE#
--------- ------------ -------- ---------- ----------
ARCH CONNECTED ARCH 0 0
ARCH CONNECTED ARCH 0 0
RFS IDLE UNKNOWN 0 0
RFS IDLE LGWR 2 128
MRP0 WAIT_FOR_LOG N/A 1 291稍等片刻,standby即会开始寻找日志,就缺少的日志文件向主库请求获取,并逐个应用了!
SQL> select process,status,client_process,thread#,sequence# from v$managed_standby;
PROCESS STATUS CLIENT_P THREAD# SEQUENCE#
--------- ------------ -------- ---------- ----------
ARCH CONNECTED ARCH 0 0
ARCH CONNECTED ARCH 0 0
RFS IDLE UNKNOWN 0 0
RFS IDLE LGWR 2 128
MRP0 APPLYING_LOG N/A 1 292
RFS IDLE LGWR 1 296
RFS IDLE UNKNOWN 0 0Ok ,至此RAC数据库的Standby算是弄起来了,当然,这只是一个雏形,如果是生产环境,接下来需要考虑的因素还比较多,比如容灾策略、备份策略、归档发送策略等等。
将一套单实例数据库转换成RAC,最快捷的方式是什么,假设条件允许,我觉着首选就应当是Dataguard的switchover,如果一切顺利,甚至只需要短短几分钟就能够完成转换的操作。由于环境所限,这里我们来实际演示一个案例,将一套RAC数据库切换为单实例,嘻嘻,仅做示例,这个会了,那单实例转换RAC也没啥问题的!
进行切换前要对当前环境进行检查,一个是要确保切换的正确执行,再一个也要确保切换后数据库的Dataguard配置不会被破坏。有朋友可能会问,当前Dataguard跑的好好的,难道切换下角色就有可能导致Dataguard不正常吗?
老实讲这太可能了,就像谈恋爱的俩人关系再好,要是有其中一人搬了家,新地址却没让另一半知道,那这两人的关系肯定就断了,起码是要中断一段时间(细心品味的话,这段描述也说明,即使准备不好导致Dataguard出现了一些问题也表怕,还是会有补救措施的,比如,赶紧找找相熟的打听打听新地址)。因此无论做什么样的切换,最起码也要保证双方还能互动吧,这里我们要做的检查,也正是确保不论角色怎么换,两方的紧(亲)密联系不会被中断。
角色切换后,原Primary变成了Standby,而原Standby变成了新的Primary(假设该Dataguard配置为一主一备的前提下),因此这里首先检查两端log_archive_dest_n参数的设置,首先来看Primary数据库:
SQL> set sqlprompt "JSSDB> "
JSSDB> show parameter log_archive_dest
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest string
log_archive_dest_1 string location=/data/oradata/jssdbn1
/archivelog
log_archive_dest_10 string
log_archive_dest_2 string service=jssstd lgwr async vali
d_for=(online_logfiles,primary
_role) db_unique_name=jssstd
log_archive_dest_3 string
log_archive_dest_4 string
log_archive_dest_5 string
log_archive_dest_6 string
log_archive_dest_7 string
log_archive_dest_8 string
log_archive_dest_9 string
log_archive_dest_state_1 string enable
log_archive_dest_state_10 string enable
log_archive_dest_state_2 string enable
log_archive_dest_state_3 string enable
log_archive_dest_state_4 string enable
log_archive_dest_state_5 string enable
log_archive_dest_state_6 string enable
log_archive_dest_state_7 string enable
log_archive_dest_state_8 string enable
log_archive_dest_state_9 string enable
JSSDB> show parameter fal;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
fal_client string
fal_server string
JSSDB> show parameter file_name_convert;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string
log_file_name_convert string
JSSDB> show parameter standby_file_management;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
standby_file_management string MANUAL再来看看Standby:
SQL> set sqlprompt "JSSSTD> "
JSSSTD> show parameter log_archive_dest
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest string
log_archive_dest_1 string location=/data1/jssstd/arclog
valid_for=(all_logfiles,all_ro
les) db_unique_name=jssstd
log_archive_dest_10 string
log_archive_dest_2 string
log_archive_dest_3 string
log_archive_dest_4 string
log_archive_dest_5 string
log_archive_dest_6 string
log_archive_dest_7 string
log_archive_dest_8 string
log_archive_dest_9 string
log_archive_dest_state_1 string enable
log_archive_dest_state_10 string enable
log_archive_dest_state_2 string enable
log_archive_dest_state_3 string enable
log_archive_dest_state_4 string enable
log_archive_dest_state_5 string enable
log_archive_dest_state_6 string enable
log_archive_dest_state_7 string enable
log_archive_dest_state_8 string enable
log_archive_dest_state_9 string enable
JSSSTD> show parameter fal
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
fal_client string JSSSTD
fal_server string jssdbn1, jssdbn2
JSSSTD> show parameter file_name_convert
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_file_name_convert string +ASMDISK1/jssdb/datafile, /dat
a1/jssstd, +ASMDISK1/jssdb/tem
pfile, /data1/jssstd
log_file_name_convert string +ASMDISK1/jssdb/onlinelog, /da
ta1/jssstd
JSSSTD> show parameter standby_file_management
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
standby_file_management string AUTO其实根本不用看,肯定不合格,因为前面就没配过,从上述返回信息可以看出,Standby没有任何向Primary发送归档的设置,Primary端也没有fal相关的设置,像*_file_name_convert这类参数也未设置,另外standby redo等也不用看了,前面也根本未创建(不过对于测试环境,这个问题倒也不影响),接下来就是要改这些初始化参数,操作如下:
JSSDB> alter system set fal_server=jssstd;
System altered.
JSSDB> alter system set fal_client=jssdb;
System altered.
JSSDB> alter system set db_file_name_convert=¨/data1/jssstd¨,¨+ASMDISK1/jssdb¨ scope=spfile;
System altered.
JSSDB> alter system set log_file_name_convert=¨/data1/jssstd¨,¨+ASMDISK1/jssdb¨ scope=spfile;
System altered.
JSSSTD> alter system set log_archive_dest_2=¨service=jssdb lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=jssdb¨;
System altered.参数修改完毕,接下来开始进行角色转换。就本例而言,需要先到Primary端关闭其它实例仅保留一台实例,然后执行下列语句:
JSSDB> alter database commit to switchover to physical standby with session shutdown;
Database altered.with session shutdown 子句专门用来处理,执行转换操作时仍有用户在连接的情况,如果附加了该子句,Primary数据库执行switchover,就会自动断开仍在连接该实例的无关会话。
重启原Primary,重启过后原Primary就是在以Standby身份运行了:
JSSDB> shutdown immediate
ORA-01507: database not mounted
ORACLE instance shut down.
JSSDB> startup mount
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 2083368 bytes
Variable Size 176162264 bytes
Database Buffers 100663296 bytes
Redo Buffers 6303744 bytes
Database mounted.
JSSDB> select DATABASE_ROLE,open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- ----------
PHYSICAL STANDBY MOUNTED转到原Standby端操作:
JSSSTD> alter database commit to switchover to primary;
Database altered.注意,此时Standby要确保已启动REDO应用,否则转换时有可能失败。
重新启动原Standby:
JSSSTD> shutdown immediate
ORA-01507: database not mounted
ORACLE instance shut down.
JSSSTD> startup
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 2083368 bytes
Variable Size 88081880 bytes
Database Buffers 188743680 bytes
Redo Buffers 6303744 bytes
Database mounted.
Database opened.
JSSSTD> select DATABASE_ROLE,open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- ----------
PRIMARY READ WRITE新的Primary诞生了,切换下日志,看看Standby端的接收是否正常:
JSSSTD> select thread#,max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1 311
2 144
JSSSTD> alter system switch logfile;
System altered.
JSSSTD> select thread#,max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1 312
2 144由于新的Primary只有一个实例,因此其实我们只需要关注thread#1生成的日志即可,转到新的Standby看看:
JSSDB> select thread#,max(sequence#) from v$archived_log group by thread#;
THREAD# MAX(SEQUENCE#)
---------- --------------
1 312
2 144成功接收,角色转换基本成功鸟!