搭建环境:
Red Hat Linux Enterprise 5.4
Oracle 10g R2
主机操作系统主要信息为192.168.199.128.
搭建准备:
已存在主库(生产库)DB_NAME=mustang SID=april
开始搭建Data Guard
首先需要准备好参数文件,设定一些必要的参数。在主库,先通过spfile生成pfile文件:
SQL>create pfile=’/home/oracle/product/10.2initapril.ora’ from spfile;
然后修改主库新生成的initapril.ora参数文件。在原有的基础上添加如下内容:
db_unique_name=mustang
log_archive_config='DG_CONFIG=(mustang,standby)'
log_archive_dest_2='service=standby arch async valid_for=(online_logfiles,primary_role)
db_unique_name=standby'
log_archive_dest_1='location=/home/oracle/archive valid_for=(all_logfiles,all_roles)
db_unique_name=mustang'
log_archive_dest_state_1=enable
log_archive_dest_state_2=enable
log_archive_format=%t_%s_%r.arc
log_archive_max_processes=30
fal_server=standby
fal_client=mustang
db_file_name_convert='standby','mustang'
log_file_name_convert='/home/oracle/','/home/oracle/standby/'
standby_file_management=auto
修改完参数文件之后,要重新打开主库,然后通过iniapril.ora来创建spfile。如下:
SQL>create spfile=’/home/oracle/product/10.2.0/db_1/dbs/spfileapril.ora’ from pfile=’
/home/oracle/product/10.2.0/db_1/dbs/initapril.ora’;
接下来的工作就是要通过主库的参数文件,创建备库需要的参数文件,在主库上运行:
SQL>create pfile=’ /home/oracle/product/10.2.0/db_1/dbs/initstandby.ora’ from spfile;
编辑initstandby.ora。如下所示:
standby.__db_cache_size=213909504
standby.__large_pool_size=4194304
*.control_files='/home/oracle/standby/standby.ctl'
*.db_file_multiblock_read_count=8
*.db_file_name_convert='standby','mustang'
*.db_files=80
*.db_name='mustang'
*.db_unique_name='standby'
*.fal_client='standby'
*.fal_server='mustang'
*.global_names=TRUE
*.instance_name='standby'
*.log_archive_config='DG_CONFIG=(mustang,standby)'
*.log_archive_dest_1='location=/home/oracle/standby/archive'
*.log_archive_dest_2='service=mustang arch async valid_for=(online_logfiles,primary_role)
db_unique_name=mustang'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.arc'
*.log_archive_max_processes=30
*.log_buffer=32768
*.log_checkpoint_interval=10000
*.log_file_name_convert='/home/oracle/','/home/oracle/standby/'
*.max_dump_file_size='10240'
*.parallel_max_servers=5
*.processes=50
*.service_names='standby'
*.sga_max_size=300M
*.sga_target=300M
*.standby_file_management='auto'
*.undo_management='auto'
*.undo_tablespace='undotbs'
保存initstandby.ora参数文件。通过initstandby.ora生成动态文件spfilestandby.ora:
SQL>create spfile=’/home/oracle/product/10.2.0/db_1/dbs/spfilestandby.ora’ from pfile=’
/home/oracle/product/10.2.0/db_1/dbs/initstandby.ora’;
在主库上创建我们的密码文件:
[oracle@localhost dbs]$orapwd file=’ /home/oracle/product/10.2.0/db_1/dbs/orapwstandby’ password=oracle entries=10
通过主库的控制文件生成我们备库的控制文件:
SQL>alter database create standby controlfile as ‘/home/oracle/standby.ctl’;
开始创建备库:
首先创建备库的目录结构,这里我们以主库位于同一目录:
主库:/home/oracle/mustang 主库归档文件:/home/oracle/archive
于是我们在/home/oracle下创建我们的备库目录:
mkdir standby
cd standby
mkdir archive
mkdir bdump
mkdir udump
目录结构完成。
下一步就是要配置监听(如果没有监听standby是启动不了的)。在配置监听之前,首先要进行一步就是要生成备库的密码文件:
[oracle@localhost dbs]$orapwd file=’ /home/oracle/product/10.2.0/db_1/dbs/orapwstandby’ password=oracle entries=10
将路径切换到/home/oracle/product/10.2.0/db_1/network/admin下。通过ls命令可以看到:
[oracle@localhost admin]$ ls
listener.ora shrept.lst sqlnet.log tnsnames.ora
再配置监听之前,要先将我们的监听服务停止:lsnrctl stop。
开始配置监听
首先配置tnsnames.ora文件。设定服务名(因为我们这里是单机,所以配置在本机上配置就行了,如果是两台机的话,就要在主备库的tnsnames.ora同时修改):
april=
(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521))
(CONNECT_DATA=
(SERVER=DEDICATED)
(SERVICE_NAME=mustang)
)
)
standby=
(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1522))
(CONNECT_DATA=
(SERVER=DEDICATED)
(SERVICE_NAME=standby)
)
)
接下来配置listener.ora文件(建议用途netmgr配置,比较方便,因为手工配置容易出错)。配置如下:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = mustang)
(ORACLE_HOME = /home/oracle/product/10.2.0/db_1)
(SID_NAME = april)
)
(SID_DESC =
(GLOBAL_DBNAME = standby)
(ORACLE_HOME = /home/oracle/product/10.2.0/db_1)
(SID_NAME = standby)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(PORT = 1521))
)
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(PORT = 1522))
)
)
TRACE_LEVEL_LISTENER = USER
至此我们的监听配置就完成,启动监听器:
[oracle@localhost admin]$ lsnrctl start
出现如下内容说明,已经配置成功:
LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 26-JUL-2012 20:59:37
Copyright (c) 1991, 2005, Oracle. All rights reserved.
Starting /home/oracle/product/10.2.0/db_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 10.2.0.1.0 - Production
System parameter file is /home/oracle/product/10.2.0/db_1/network/admin/listener.ora
Log messages written to /home/oracle/product/10.2.0/db_1/network/log/listener.log
Trace information written to /home/oracle/product/10.2.0/db_1/network/trace/listener.trc
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1522)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date 26-JUL-2012 20:59:37
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level user
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /home/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File /home/oracle/product/10.2.0/db_1/network/log/listener.log
Listener Trace File /home/oracle/product/10.2.0/db_1/network/trace/listener.trc
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1522)))
Services Summary...
Service "mustang" has 1 instance(s).
Instance "april", status UNKNOWN, has 1 handler(s) for this service...
Service "standby" has 1 instance(s).
Instance "standby", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
我们也可以通过tnsping来检查,是否配置成功:
[oracle@localhost admin]$ tnsping standby
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 26-JUL-2012 21:00:45
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1522)) (CONNECT_DATA= (SERVER=DEDICATED) (SERVICE_NAME=standby)))
OK (20 msec)
[oracle@localhost admin]$ tnsping april
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 26-JUL-2012 21:00:51
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521)) (CONNECT_DATA= (SERVER=DEDICATED) (SERVICE_NAME=mustang)))
OK (30 msec)
看到以上提示,说明我们的监听配置完全正确。
复制相关文件到备库的目录下:
[oracle@localhost mustang]$cp /home/oracle/mustang/*.dbf /home/oracle/standby
[oracle@localhost mustang]$cp /home/oracle/standby.ctl /home/oracle/standby/
接下来,开始启动我们的主库和备库,最好打开两个窗口进行实验:
在主库窗口:
1)指定以april实例开始我们的主库:
export ORACLE_SID=april
2)
[oracle@localhost admin]$ sqlplus sys/oracle@april as sysdba
3)开启主库的归档模式和force logging:
SQL>alter database archivelog;
SQL>alter database force logging;
在备库窗口:
1) 指定以standby实例打开我们的备库:
export ORACLE_SID=standby
2)
[oracle@localhost admin]$ sqlplus sys/oracle@standby as sysdba
3) 使standby数据库处于恢复状态,主备库归档日志同步
SQL>alter database recover managed standby database disconnect from session:
接下来就要查看我们的Data Guard是否正常。
我们在主库环境下,查看当前的归档日志情况:
SQL>select sequence#,first_time,next_time from v$archived_log order by sequence#;
然后切换到备库环境下,查看当前的归档日志情况:
SQL>select sequence#,first_time,next_time from v$archived_log order by sequence#;
接着我们再次切换到主库环境下,进行一次日志切换:
SQL>alter system switch logfile;
再次查看主库的归档情况:
SQL>select sequence#,first_time,next_time from v$archived_log order by sequence#;
这时,你会发现多了2个归档文件。
我们再切到备库,查看:
SQL>select sequence#,first_time,next_time from v$archived_log order by sequence#;
发现备库的归档文件也同样多了。
至此,可以说明我们的归档同步了。
在主库下:我们新建一个表,然后往表中插入数据,先不要commit。我们发现备库中也同样有了这个表,但是还没有数据,如果我们commit掉主库的事务,备库中该表就有了数据了。
我们可以查看主库和备库的switchover参数。
SQL>select switchover_status,database_role from v$database;
DG搭建工作至此完成。刚刚学习Data Guard,上文有不正确的地方,还请多多批评赐教!
©著作权归作者所有:来自51CTO博客作者郑宏辉的原创作品,如需转载,请注明出处,否则将追究法律责任
oracle单机DGOracle