经过斤2天的摸索,终于在虚拟机上把oracle dg给做出来了,下面是我的操作步骤给大家分享一下,如有不足之处请多多指教。
说明:
1、该文档配置的为物理standby(逻辑standby是通过接收primary数据库的redo log并转换成sql语句,然后在standby数据库上执行sql语句(SQL Apply)实现同步,物理standby是通过接受并应用primary数据库的redo log以介质恢复的方式(Redo Apply)实现同步)。
2、保护模式采用最高可用性(Maximum availability),这种保护模式提供在不影响primary数据库可用前提下最高级别的数据保护策略。它要求所有事务在提交前必须保障redo数据至少在一个standby数据库可用,如果出现故障导致无法同时写入standby数据库redo log,primary数据库不会shutdown,而是自动转换为最高性能(Maximum availability,该模式事务可以随时提交,当前primary数据库的redo数据也需要至少写入一个standby数据库,不过这种写入可以是不同步的,该模式适合于网络性能差的环境)模式,等standby数据库恢复正常后,它又会再自动转换成最高可用性模式。
环境描述:
主库:
IP: 192.168.0.16
主机名:huyoung
ORACLE_SID=dgdemo
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/10.2.0/db_1
备库:
IP: 192.168.0.18
主机名:cuijie
ORACLE_SID=dgdemo
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/10.2.0/db_1
准备工作:分别在主库和备库都安装上oracle软件,不装数据库。
安装配置步骤:
1、运行dbca创建数据库(安装过程中需要注意:先关掉flash recovery,启用archive),数据库名为dgdemo
2、登陆到数据库上
[oracle@huyoung ~]$ sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 3月 11 20:31:09 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn / as sysdba;
Connected.
SQL>
3、将主库设置为 FORCE LOGGING 模式
SQL> ALTER DATABASE FORCE LOGGING;
Database altered.
4、创建一个密码文件,如果数据库是用dbca创建的则会在$ORACLE_HOME/dbs/下自动创建一个叫orapwdSID的一个密码文件。否则可以用 orapwd命令创建一个。
5、配置Standby Redo Log。创建组数至少要比主库的online redo log组数多一个。
SQL> select group#,bytes from v$log;
GROUP# BYTES
---------- ----------
1 52428800
2 52428800
3 52428800
这里创建4组
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/dgdemo/stdbyredo01.log') SIZE 52428800;
Database altered.
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/dgdemo/stdbyredo02.log') SIZE 52428800;
Database altered.
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/dgdemo/stdbyredo03.log') SIZE 52428800;
Database altered.
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/dgdemo/stdbyredo04.log') SIZE 52428800;
验证一下创建是否成功
SQL> SELECT GROUP#,THREAD#,SEQUENCE#,ARCHIVED,STATUS FROM V$STANDBY_LOG;
GROUP# THREAD# SEQUENCE# ARC STATUS
---------- ---------- ---------- --- ----------
4 0 0 YES UNASSIGNED
5 0 0 YES UNASSIGNED
6 0 0 YES UNASSIGNED
7 0 0 YES UNASSIGNED
Database altered.
如果想删除某个组可以用下面的命令
SQL> alter database drop standby logfile group 4;
如果给某个组添加成员可以用下面命令
SQL> ALTER DATABASE ADD STANDBY LOGFILE MEMBER '/u01/app/oracle/oradata/dgdemo/stdbyredo04_1.log' TO GROUP 7;
删除组中的成员用以下命令
SQL> ALTER DATABASE DROP STANDBY LOGFILE MEMBER '/u01/app/oracle/oradata/dgdemo/stdbyredo04_1.log';
Database altered.
SQL> host rm -f /u01/app/oracle/oradata/dgdemo/stdbyredo04_1.log
6、检查数据库是否处于归档状态
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/arch_log
Oldest online log sequence 1
Next log sequence to archive 2
Current log sequence 2
如果不是处于归档状态则运行下面命令
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE ARCHIVELOG;
SQL> ALTER DATABASE OPEN;
7、创建备库的控制文件(创建后数据库不能做结构性的改变)
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/u01/app/oracle/oradata/dgdemo/stdby.ctl';
8、创建pfile以便于修改配置参数
SQL> CREATE PFILE='/u01/app/oracle/oradata/dgdemo/pfile' FROM SPFILE;
9、关掉数据库开始编辑参数
SQL> shutdown immediate;
10、$ vi /u01/app/oracle/oradata/dgdemo/pfile
添加下面内容
DB_UNIQUE_NAME=huyoung
LOG_ARCHIVE_CONFIG='DG_CONFIG=(huyoung,cuijie)'
LOG_ARCHIVE_DEST_2=
'SERVICE=cuijie LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=cuijie'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_SERVER=cuijie
FAL_CLIENT=huyoung
DB_FILE_NAME_CONVERT='cuijie','huyoung'
STANDBY_FILE_MANAGEMENT=AUTO
11、进入sqlplus创建pfile(NOMOUNT状态)
SQL> CREATE SPFILE from PFILE='/u01/app/oracle/oradata/dgdemo/pfile';
12、创建tnsnames.ora和listener.ora,可以手动创建也可以用netca创建,无论哪种方法都必须保证能互相畅通。
$ cd /u01/app/oracle/10.2.0/db_1/network/admin/
$ vi tnsnames.ora
输入下面内容
CUIJIE =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.18)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dgtest)
)
)
HUYOUNG =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.16)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dgtest)
)
)
$ vi listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/10.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(SID_NAME = dgtest)
(ORACLE_HOME = /u01/app/oracle/10.2.0/db_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = huyoung)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
13、复制备份文件到备库
$ cd /u01/app/oracle/
$ scp -r arch_log/ admin/ oradata/ 192.168.0.18:/u01/app/oracle/
$ cd /u01/app/oracle/10.2.0/db_1/network/admin/
$ scp listener.ora tnsnames.ora 192.168.0.18:/u01/app/oracle/10.2.0/db_1/network/admin/
$ cd /u01/app/oracle/10.2.0/db_1/dbs/
$ scp orapwdgdemo 192.168.0.18:/u01/app/oracle/10.2.0/db_1/dbs/
14、打开备库做以下操作
$ cd /u01/app/oracle/oradata/dgdemo/
$ rm -rf control0*
$ cp stdby.ctl control01.ctl
$ cp stdby.ctl control02.ctl
$ cp stdby.ctl control03.ctl
$ vi pfile 对相应文件做修改
DB_UNIQUE_NAME=cuijie
LOG_ARCHIVE_CONFIG='DG_CONFIG=(huyoung,cuijie)'
LOG_ARCHIVE_DEST_2=
'SERVICE=huyoung LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=huyoung'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_SERVER=huyoung
FAL_CLIENT=cuijie
DB_FILE_NAME_CONVERT='cuijie','huyoung'
STANDBY_FILE_MANAGEMENT=AUTO
$ cd /u01/app/oracle/10.2.0/db_1/network/admin/
$ vi listener.ora
将huyoung修改为cuijie
15、在备库中进入sqlplus
$ sqlplus /nolog
SQL> conn / as sysdba;
SQL> CREATE SPFILE from PFILE='/u01/app/oracle/oradata/dgdemo/pfile';
16、测试oracle net是否畅通
首先启动监听服务(分别在主机和备机上进行)
$ lsnrctl stop
$ lsnrctl start
分别在主机和备机上运行下面命令来测试
$ tnsping huyoung
$ tnsping cuijie
17、启动主库(正常启动)
$ sqlplus /nolog
SQL> conn / as sysdba;
SQL> startup
18、启动备库到mount状态,并打开redo Apply
$ sqlplus /nolog
SQL> conn / as sysdba;
SQL> startup mount
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
19、在主库上做一次日志切换
SQL> ALTER SYSTEM SWITCH LOGFILE;
20、分别在主库和备库上做下列查询,检查归档日志是否由主库传送到备库
SQL> l
1* SELECT DESTINATION, STATUS, ARCHIVED_THREAD#, ARCHIVED_SEQ# FROM V$ARCHIVE_DEST_STATUS WHERE STATUS <> 'DEFERRED' AND STATUS <> 'INACTIVE'
以下语句可以查看日志应用情况
SQL> SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# APP
---------- ---
2 YES
3 YES
如果出现上面情况则说明配置成功。
21、在主库建表然后在备库查询进行验证
SQL> create table cuijie(id integer,name char(10))
SQL> insert into cuijie values(1,'huyoung');
SQL> commit;
SQL> select * from cuijie;
ID NAME
---------- ----------
1 huyoung
22、在主库上做个日志归档
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
23、在备库上取消掉redo apply
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL> ALTER DATABASE OPEN;
SQL> select * from cuijie;
ID NAME
---------- ----------
1 huyoung
测试成功
24、再次切换为备库的redo apply状态
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
主库备库互相切换
25、在主库上做如下sql语句
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;
SQL> shutdown immediate;
SQL> startup nomount;
SQL> ALTER DATABASE MOUNT STANDBY DATABASE;
SQL> RECOVER MANAGED STANDBY DATABASE DISCONNECT;(Redo Apply)
26、在备库上做switchover
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
SQL> shutdown immediate;
SQL> startup
27、分别在新的主库和备库上查看状态,验证主库备库是否切换正常。
SQL> select name,database_role from v$database;
再次创建表在切换后的主库备库上验证
28、在新的主库上创建表
SQL> create table huyoung(id integer,name char(10));
SQL> insert into huyoung values(111,'haha');
SQL> commit;
SQL> select * from huyoung;
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
29、在新的备库上做下面操作以取消redo apply
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL> ALTER DATABASE OPEN;
SQL> select * from huyoung;
30、恢复新备库为redo apply状态
以上即为我的操作步骤,其实最简单的还是参照官方文档最好,步骤非常清楚。