设置为写log模式:
ALTER DATABASE FORCE LOGGING;
SQL> ALTER DATABASE FORCE LOGGING;
Database altered.
生成密码文件:
orapwd file=orapwprod password=oracle
创建standby日志文件组:
ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/prod/redo04.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/prod/redo05.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/prod/redo06.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/prod/redo07.log') SIZE 50M;
确认standby日志文件组创建成功:
SELECT GROUP#,THREAD#,SEQUENCE#,ARCHIVED,STATUS FROM V$STANDBY_LOG;
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
为主库手工创建新的参数文件pfile:
db_name = prod
control_files = ('/u01/app/oracle/oradata/prod/control1.dbf',
'/u01/app/oracle/oradata/prod/control2.dbf',
'/u01/app/oracle/oradata/prod/control3.dbf')
sga_target = 400m
undo_management = AUTO
undo_tablespace = undotbs
DB_UNIQUE_NAME = pri
LOG_ARCHIVE_CONFIG = 'DG_CONFIG=(pri,stdby)'
LOG_ARCHIVE_DEST_1 = 'LOCATION=/u01/app/oracle/arch/prod/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=pri'
LOG_ARCHIVE_DEST_STATE_1 = ENABLE
REMOTE_LOGIN_PASSWORDFILE = EXCLUSIVE
LOG_ARCHIVE_FORMAT = %t_%s_%r.arc
processes = 300
FAL_SERVER = stdby
FAL_CLIENT = pri
DB_FILE_NAME_CONVERT = 'stdby','prod'
LOG_FILE_NAME_CONVERT= 'stdby','prod'
STANDBY_FILE_MANAGEMENT=AUTO
启用新参数文件:
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
create spfile from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initprod3.ora';
SQL> create spfile from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initprod3.ora';
File created.
设置归档模式:
STARTUP MOUNT;
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE OPEN;
对主库做备份:
rman target /
rman target /
Recovery Manager: Release 10.2.0.1.0 - Production on Thu Jul 26 23:25:04 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: PROD (DBID=201531499)
设置备份路径与文件格式,后面备库恢复时需要同样的路径:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/u01/app/oracle/backup/prod_%T_%s_%p.bak';
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/u01/app/oracle/backup/prod_%T_%s_%p.bak';
new RMAN configuration parameters are successfully stored
查看配置情况:
show all;
RMAN> show all;
RMAN configuration parameters are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/u01/app/oracle/backup/prod_%T_%s_%p.bak';
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/10.2.0/db_1/dbs/snapcf_prod.f'; # default
备份数据库:
backup database;
RMAN> backup database;
Starting backup at 26-JUL-12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=308 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/u01/app/oracle/oradata/prod/system01.dbf
input datafile fno=00003 name=/u01/app/oracle/oradata/prod/sysaux01.dbf
input datafile fno=00002 name=/u01/app/oracle/oradata/prod/undotbs01.dbf
input datafile fno=00004 name=/u01/app/oracle/oradata/prod/users01.dbf
channel ORA_DISK_1: starting piece 1 at 26-JUL-12
channel ORA_DISK_1: finished piece 1 at 26-JUL-12
piece handle=/u01/app/oracle/backup/prod_20120726_3_1.bak tag=TAG20120726T235210 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 26-JUL-12
channel ORA_DISK_1: finished piece 1 at 26-JUL-12
piece handle=/u01/app/oracle/backup/prod_20120726_4_1.bak tag=TAG20120726T235210 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 26-JUL-12
为从库创建控制文件:
ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/u01/app/oracle/backup/control4stdby.dbf';
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/u01/app/oracle/backup/control4stdby.dbf';
Database altered.
修改主库参数文件为从库参数文件:
db_name = prod
control_files = ('/u01/app/oracle/oradata/stdby/control1.dbf',
'/u01/app/oracle/oradata/stdby/control2.dbf',
'/u01/app/oracle/oradata/stdby/control3.dbf')
sga_target = 400m
undo_management = AUTO
undo_tablespace = undotbs
DB_UNIQUE_NAME = stdby
LOG_ARCHIVE_CONFIG = 'DG_CONFIG=(pri,stdby)'
LOG_ARCHIVE_DEST_1 = 'LOCATION=/u01/app/oracle/arch/stdby/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=stdby'
LOG_ARCHIVE_DEST_STATE_1 = ENABLE
REMOTE_LOGIN_PASSWORDFILE = EXCLUSIVE
LOG_ARCHIVE_FORMAT = %t_%s_%r.arc
processes = 300
FAL_SERVER = pri
FAL_CLIENT = stdby
DB_FILE_NAME_CONVERT = 'prod','stdby'
LOG_FILE_NAME_CONVERT= 'prod','stdby'
STANDBY_FILE_MANAGEMENT=AUTO
拷贝备份文件、控制文件、参数文件到从库:
scp prod*.bak xxxxstdby:/u01/app/oracle/backup
scp control4stdby.dbf xxxxstdby:/u01/app/oracle/backup
scp initstdby.ora xxxxstdby:/u01/app/oracle/backup
从库上生成密码文件:
orapwd file=orapwstdby password=oracle
为主库设置监听文件listener.ora:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = pri_DGMGRL)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
(SID_NAME = prod)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczpri)(PORT = 1521))
)
)
监听重启:
lsnrctl stop
lsnrctl start
查看监听服务:
lsnrctl service|grep pri
$ lsnrctl service|grep pri
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=thczpri)(PORT=1521)))
Service "pri" has 2 instance(s).
Service "pri_XPT" has 1 instance(s).
为主库设置命名文件tnsnames.ora:
db_pri =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczpri)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = pri)
)
)
db_stdby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczstdby)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = stdby)
)
)
查看命名解析情况:
tnsping db_pri
$ tnsping db_pri
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 27-JUL-2012 10:21:57
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = thczpri)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = pri)))
OK (0 msec)
从库上生成spfile:
CREATE SPFILE FROM PFILE='initstdby.ora';
从库上拷贝控制文件到参数文件指定目录:
cp control4stdby.dbf /u01/app/oracle/oradata/stdby/control1.dbf
cp control4stdby.dbf /u01/app/oracle/oradata/stdby/control2.dbf
cp control4stdby.dbf /u01/app/oracle/oradata/stdby/control3.dbf
从库上启动到mount状态:
startup mount;
从库上用rman恢复数据库:
restore database;
RMAN> restore database;
Starting restore at 27-JUL-12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=318 devtype=DISK
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/app/oracle/oradata/stdby/system01.dbf
restoring datafile 00002 to /u01/app/oracle/oradata/stdby/undotbs01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/stdby/sysaux01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/stdby/users01.dbf
channel ORA_DISK_1: reading from backup piece /u01/app/oracle/backup/prod_20120726_5_1.bak
channel ORA_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/backup/prod_20120726_5_1.bak tag=TAG20120726T235912
channel ORA_DISK_1: restore complete, elapsed time: 00:00:16
Finished restore at 27-JUL-12
为从库创建standby log 组,为之后切换做好准备:
ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/stdby/redo04.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/stdby/redo05.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/stdby/redo06.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/stdby/redo07.log') SIZE 50M;
为从库设置监听文件listener.ora:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = stdby)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
(SID_NAME = stdby)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczstdby)(PORT = 1521))
)
)
监听重启:
lsnrctl stop
lsnrctl start
查看监听服务:
lsnrctl service|grep stdby
$ lsnrctl service|grep stdby
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=thczstdby)(PORT=1521)))
Service "stdby" has 2 instance(s).
Instance "stdby", status UNKNOWN, has 1 handler(s) for this service...
Instance "stdby", status READY, has 1 handler(s) for this service...
Service "stdby_XPT" has 1 instance(s).
Instance "stdby", status READY, has 1 handler(s) for this service...
为主库设置命名文件tnsnames.ora:
db_pri =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczpri)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = pri)
)
)
db_stdby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = thczstdby)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = stdby)
)
)
查看命名解析情况:
tnsping db_stdby
$ tnsping db_stdby
TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 27-JUL-2012 10:54:31
Copyright (c) 1997, 2005, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = thczstdby)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = stdby)))
OK (0 msec)
从库启动到redo接收状态:
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
查看mrp进程是否启动:
ps -ef |grep mrp
$ ps -ef |grep mrp
oracle 13262 1 4 11:01 ? 00:00:00 ora_mrp0_stdby
oracle 13285 7982 0 11:01 pts/7 00:00:00 grep mrp
主库启动redo apply:
alter system set LOG_ARCHIVE_DEST_2 = 'SERVICE=db_stdby LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=stdby';
alter system set LOG_ARCHIVE_DEST_STATE_2 = ENABLE;
主库切换日志:
alter system switch logfile;
archive log list;
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/arch/prod/
Oldest online log sequence 10
Next log sequence to archive 12
Current log sequence 12
从库查看日志:
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/arch/stdby/
Oldest online log sequence 10
Next log sequence to archive 0
Current log sequence 12
可以看到日志已经传过来,dataguard搭建好了。