开始创建备库:
首先创建备库的目录结构,这里我们以主库位于同一目录:
主库:/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;
在备库窗口:
<!--[if !supportLists]-->1) <!--[endif]-->指定以standby实例打开我们的备库:
- export ORACLE_SID=standby
<!--[if !supportLists]-->2)
- [oracle@localhost admin]$ sqlplus sys/oracle@standby as sysdba
<!--[if !supportLists]-->3) <!--[endif]-->使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,上文有不正确的地方,还请多多批评赐教!
