1. 建立一个primary数据库,db_name等都为primary。
2. 启用归档模式
SQL> archive log list Database log mode No Archive Mode Automatic archival Disabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 1 Current log sequence 2 SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 839282688 bytes Fixed Size 2233000 bytes Variable Size 494931288 bytes Database Buffers 339738624 bytes Redo Buffers 2379776 bytes Database mounted. SQL> alter database archivelog; Database altered. SQL> archive log list Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 1 Next log sequence to archive 2 Current log sequence 2 SQL> alter database open; Database altered.
3. 将主库改为Force Logging
SQL> alter database force logging; Database altered.
4. 使用RMAN备份数据库
RMAN> backup format '/u01/FULL_%U.bak' database; Starting backup at 2015/07/11 12:08:33 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=37 device type=DISK channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/primary/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/primary/sysaux01.dbf input datafile file number=00003 name=/u01/app/oracle/oradata/primary/undotbs01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/primary/users01.dbf channel ORA_DISK_1: starting piece 1 at 2015/07/11 12:08:34 channel ORA_DISK_1: finished piece 1 at 2015/07/11 12:09:20 piece handle=/u01/FULL_01qbpam2_1_1.bak tag=TAG20150711T120834 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:46 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current control file in backup set including current SPFILE in backup set channel ORA_DISK_1: starting piece 1 at 2015/07/11 12:09:21 channel ORA_DISK_1: finished piece 1 at 2015/07/11 12:09:22 piece handle=/u01/FULL_02qbpang_1_1.bak tag=TAG20150711T120834 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 2015/07/11 12:09:22
5.备份归档日志(这一步不是必须的)
RMAN> backup archivelog all format '/u01/ARC_%U.bak'; Starting backup at 2015/07/11 12:10:52 current log archived using channel ORA_DISK_1 channel ORA_DISK_1: starting archived log backup set channel ORA_DISK_1: specifying archived log(s) in backup set input archived log thread=1 sequence=2 RECID=1 STAMP=884779852 channel ORA_DISK_1: starting piece 1 at 2015/07/11 12:10:53 channel ORA_DISK_1: finished piece 1 at 2015/07/11 12:10:54 piece handle=/u01/ARC_03qbpaqc_1_1.bak tag=TAG20150711T121052 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 2015/07/11 12:10:54
6. 备份控制文件
虽说,备份全库时已经包含了控制文件。但是我们要为standby备份一个专用的controlfile。语句如下
RMAN> backup format '/u01/standby_%U.ctl' current controlfile for standby; Starting backup at 2015/07/11 12:12:46 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including standby control file in backup set channel ORA_DISK_1: starting piece 1 at 2015/07/11 12:12:47 channel ORA_DISK_1: finished piece 1 at 2015/07/11 12:12:48 piece handle=/u01/standby_04qbpatu_1_1.ctl tag=TAG20150711T121246 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 2015/07/11 12:12:48
7. 为从库创建一个参数文件
SQL> create pfile='/u01/initstandby.ora' from spfile; File created.
8. 将备份文件copy至目标数据库(本例在同台机器,略去该步骤)。
9. 修改initstandby.ora
mv /u01/initstandby.ora $ORACLE_HOME/dbs/
打开参数文件,做如下修改,将primary修改为standby(除了db_name)
standby.__db_cache_size=339738624 standby.__java_pool_size=4194304 standby.__large_pool_size=4194304 standby.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment standby.__pga_aggregate_target=339738624 standby.__sga_target=503316480 standby.__shared_io_pool_size=0 standby.__shared_pool_size=146800640 standby.__streams_pool_size=0 *.audit_file_dest='/u01/app/oracle/admin/standby/adump' *.audit_trail='db' *.compatible='11.2.0.0.0' *.control_files='/u01/app/oracle/oradata/standby/control01.ctl','/u01/app/oracle/fast_recovery_area/standby/control02.ctl' *.db_block_size=8192 *.db_domain='' *.db_name='primary' *.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area' *.db_recovery_file_dest_size=4322230272 *.diagnostic_dest='/u01/app/oracle' *.dispatchers='(PROTOCOL=TCP) (SERVICE=standbyXDB)' *.memory_target=839909376 *.open_cursors=300 *.processes=150 *.remote_login_passwordfile='EXCLUSIVE' *.undo_tablespace='UNDOTBS1'
在参数文件后追加,standby所需的参数
*.db_file_name_convert='/primary/','/standby/' *.log_file_name_convert='/primary/','/standby/' *.standby_file_management='auto' *.log_archive_config='dg_config=(primary,standby)' *.fal_server='primary' *.db_unique_name='standby'
10. 建立目录
11gdg1-> mkdir -p /u01/app/oracle/admin/standby/adump 11gdg1-> mkdir -p /u01/app/oracle/oradata/standby 11gdg1-> mkdir -p /u01/app/oracle/fast_recovery_area/standby
11. 恢复从数据库
11gdg1-> export | grep ORACLE declare -x ORACLE_BASE="/u01/app/oracle" declare -x ORACLE_HOME="/u01/app/oracle/product/11.2.0/db_1" declare -x ORACLE_HOSTNAME="11gdg1.localdomain.com" declare -x ORACLE_SID="standby" declare -x ORACLE_TERM="xterm" declare -x ORACLE_UNQNAME="standby"
数据库启动到nomount
SQL> startup nomount ORA-00845: MEMORY_TARGET not supported on this system
将/dev/shm调大。将主库关闭,并修改/etc/fstab
tmpfs /dev/shm tmpfs defaults,size=2G 0 0 #size指定大小
卸载shm并重新mount,或者重启机器。
SQL> startup nomount ORACLE instance started. Total System Global Area 839282688 bytes Fixed Size 2233000 bytes Variable Size 494931288 bytes Database Buffers 339738624 bytes Redo Buffers 2379776 bytes SQL>
恢复控制文件
11gdg1-> rman target / Recovery Manager: Release 11.2.0.3.0 - Production on Sat Jul 11 13:05:18 2015 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. connected to target database: STANDBY (not mounted) RMAN> restore standby controlfile from '/u01/standby_04qbpatu_1_1.ctl'; Starting restore at 2015/07/11 13:09:40 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=19 device type=DISK channel ORA_DISK_1: restoring control file channel ORA_DISK_1: restore complete, elapsed time: 00:00:01 output file name=/u01/app/oracle/oradata/standby/control01.ctl output file name=/u01/app/oracle/fast_recovery_area/standby/control02.ctl Finished restore at 2015/07/11 13:09:42
启动到mount,并还原数据库。(注意:不要恢复数据库)
RMAN> startup mount Oracle instance started database mounted Total System Global Area 839282688 bytes Fixed Size 2233000 bytes Variable Size 494931288 bytes Database Buffers 339738624 bytes Redo Buffers 2379776 bytes
RMAN> restore database; Starting restore at 2015/07/11 13:15:18 Starting implicit crosscheck backup at 2015/07/11 13:15:18 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=18 device type=DISK Crosschecked 3 objects Finished implicit crosscheck backup at 2015/07/11 13:15:19 Starting implicit crosscheck copy at 2015/07/11 13:15:19 using channel ORA_DISK_1 Finished implicit crosscheck copy at 2015/07/11 13:15:19 searching for all files in the recovery area cataloging files... no files cataloged using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile backup set restore channel ORA_DISK_1: specifying datafile(s) to restore from backup set channel ORA_DISK_1: restoring datafile 00001 to /u01/app/oracle/oradata/standby/system01.dbf channel ORA_DISK_1: restoring datafile 00002 to /u01/app/oracle/oradata/standby/sysaux01.dbf channel ORA_DISK_1: restoring datafile 00003 to /u01/app/oracle/oradata/standby/undotbs01.dbf channel ORA_DISK_1: restoring datafile 00004 to /u01/app/oracle/oradata/standby/users01.dbf channel ORA_DISK_1: reading from backup piece /u01/FULL_01qbpam2_1_1.bak channel ORA_DISK_1: piece handle=/u01/FULL_01qbpam2_1_1.bak tag=TAG20150711T120834 channel ORA_DISK_1: restored backup piece 1 channel ORA_DISK_1: restore complete, elapsed time: 00:00:45 Finished restore at 2015/07/11 13:16:05
12. 在从库上建立standby logfile
SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/standby/sredo01.log' size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/standby/sredo02.log' size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/standby/sredo03.log' size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/standby/sredo04.log' size 50M;
13. 启动主从库的监听,并配置相互连接的tnsname
11gdg1-> cat listener.ora # listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora # Generated by Oracle configuration tools. SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = primary) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = primary) ) (SID_DESC = (GLOBAL_DBNAME = standby) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = standby) ) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11gdg1.localdomain.com)(PORT = 1521)) ) ADR_BASE_LISTENER = /u01/app/oracle 11gdg1-> cat tnsnames.ora # tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. PRIMARY = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.216)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = primary) ) ) STANDBY = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.216)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = standby) ) )
14. 修改主库参数
SQL> alter system set log_archive_dest_2='service=standby lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=standby'; System altered. SQL> alter system set log_archive_config='dg_config=(primary,standby)'; System altered. SQL>
15. 保证两个实例的sysdba角色用户的密码一样,并都有密码文件。
11gdg1-> cp orapwprimary orapwstandby
16. 启动备库日志应用
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT ; Database altered.
17. 测试DataGuard数据同步功能
主库上创建一个t1表。
SQL> ALTER USER SCOTT ACCOUNT UNLOCK; User altered. SQL> ALTER USER SCOTT IDENTIFIED BY tiger; User altered. SQL> conn scott/tiger Connected. SQL> create table t1 as select * from emp; Table created.
查看从库是否有数据
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; Database altered. SQL> alter database open read only; Database altered. SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT ; Database altered. SQL> conn scott/tiger Connected. SQL> select count(*) from t1; COUNT(*) ---------- 14
DataGuard已经配置成功!
注:如果希望日后做switchover或者Failover。现在的primary数据库要配置从数据库所需的参数,而现在的standby数据库需要配置上主数据库所需的参数。
如下:
在primary库添加standby角色参数
ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/primary/sredo01.log' size 50M; ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/primary/sredo02.log' size 50M; ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/primary/sredo03.log' size 50M; ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/primary/sredo04.log' size 50M; alter system set db_file_name_convert='/standby/','/primary/' scope=spfile; alter system set log_file_name_convert='/standby/','/primary/' scope=spfile; alter system set standby_file_management='auto' scope=spfile; alter system set fal_server='standby' scope=spfile;
在standby库添加primary角色参数
SQL> create spfile from pfile; File created. SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ORACLE instance started. Total System Global Area 839282688 bytes Fixed Size 2233000 bytes Variable Size 494931288 bytes Database Buffers 339738624 bytes Redo Buffers 2379776 bytes Database mounted. Database opened. SQL> alter system set log_archive_dest_2='service=primary lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=primary'; System altered. SQL>