分类: Oracle
2017-04-25 10:08:05
1.环境:
操作系统 Oracle linux 5.8
数据库版本:11.2.0.3
主库ip:192.168.169.133 dg1
备库ip:192.168.169.134 dg2
DB版本:
Database DB_UNIQUE_NAME Primary lei_pd Physical standby lei_st
备库只需要安装database软件即可,不需要建库。
2.主库启动归档Force logging
2.1
SQL> alter database force logging; Database altered. SQL> select force_logging from v$database; FOR --- YES
2.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 3 Current log sequence 5 SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 413372416 bytes Fixed Size 2228904 bytes Variable Size 310381912 bytes Database Buffers 92274688 bytes Redo Buffers 8486912 bytes Database mounted. SQL> alter database archivelog; Database altered. SQL> alter system set log_archive_dest_1='location=/u01/archive/' scope=spfile; SQL> alter database open; 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 3 Next log sequence to archive 5 Current log sequence 5
3添加主库 standby redo log----后面会自动同步到备库,省事很多。
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/lei/redo04.log') size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/lei/redo05.log') size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/lei/redo06.log') size 50M; SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/lei/redo07.log') size 50M;
--验证:
SQL> col member for a50 SQL> select group#,type, member from v$logfile; GROUP# TYPE MEMBER ---------- ------- -------------------------------------------------- 3 ONLINE /u01/app/oracle/oradata/lei/redo03.log 2 ONLINE /u01/app/oracle/oradata/lei/redo02.log 1 ONLINE /u01/app/oracle/oradata/lei/redo01.log 4 STANDBY /u01/app/oracle/oradata/lei/stdbyredo01.log 5 STANDBY /u01/app/oracle/oradata/lei/stdbyredo02.log 6 STANDBY /u01/app/oracle/oradata/lei/stdbyredo03.log 7 STANDBY /u01/app/oracle/oradata/lei/stdbyredo04.log
4在主备库创建监听
最好使用工具去创建
[root@dg1 admin]# 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 = lei) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = lei) ) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = dg1)(PORT = 1521)) ) ADR_BASE_LISTENER = /u01/app/oracle
这里是通过netca和netmgr配置的。因为文件里对空格很敏感,所以手动配置容易出问题。
查看监听状态
[oracle@dg1 ~]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 22-OCT-2015 09:27:28 Copyright (c) 1991, 2011, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dg1)(PORT=1521))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 11.2.0.3.0 - Production Start Date 22-OCT-2015 09:14:59 Uptime 0 days 0 hr. 12 min. 29 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/dg1/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dg1)(PORT=1521))) Services Summary... Service "lei" has 1 instance(s). Instance "lei", status READY, has 1 handler(s) for this service... Service "leiXDB" has 1 instance(s). Instance "lei", status READY, has 1 handler(s) for this service... The command completed successfully
5主库添加Net Service
[root@dg1 admin]# 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. SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = lei) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = lei) ) ) LEI_PD = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.169.133)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = lei) ) ) LEI_ST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.169.134)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = lei) ) )
测试一下
[oracle@dg1 ~]$ tnsping lei_pd TNS Ping Utility for Linux: Version 11.2.0.3.0 - Production on 22-OCT-2015 09:39:47 Copyright (c) 1997, 2011, Oracle. All rights reserved. Used parameter files: /u01/app/oracle/product/11.2.0/db_1/network/admin/sqlnet.ora Used TNSNAMES adapter to resolve the alias Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.169.133)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = lei))) OK (10 msec)
6在备库创建新的目录---用来恢复
[root@dg2 /]# mkdir -p /u02/oradata [root@dg2 /]# chown -R oracle.oinstall /u02 [oracle@dg2 /]$ cd /u02 [oracle@dg2 u02]$ ls oradata [oracle@dg2 u02]$ ll total 4 drwxr-xr-x 2 oracle oinstall 4096 Apr 28 19:46 oradata [oracle@dg2 u02] $
7在主库创建pfile并修改它
SQL> create pfile from spfile; File created.
添加如下内容-红色标注
*.db_unique_name='lei_pd' *.log_archive_config='dg_config=(lei_pd,lei_st)' *.log_archive_dest_1='location=/u01/archivelog valid_for=(all_logfiles,all_roles) db_unique_name='lei_pd' *.log_archive_dest_2='service=lei_st reopen=120 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=lei_st' *.log_archive_dest_state_1=enable *.log_archive_dest_state_2=enable *.standby_file_management='auto' *.fal_server='lei_st' *.log_file_name_convert='/u02/oradata','/u01/app/oracle/oradate/lei' *.db_file_name_convert='/u02/oradata','/u01/app/oracle/oradate/lei'
--注意这 2 个参数。 这个 2 个参数,只在数据库为 standby_role 时才生效,但我们在主库还是配置这 2 个
参数,就是为了减少以后做 switchover 的时间。 注意 2 个目录的结构。 前面是发送数据的,后面的接收
数据的。
8备库创建相关目录,因为备库没有创建实例
[root@dg2 oracle]# mkdir admin [root@dg2 oracle]# cd admin/ [root@dg2 admin]# mkdir lei [root@dg2 admin]# cd .. [root@dg2 oracle]# chown -R oracle:oinstall admin/ [root@dg2 oracle]# ll total 16 drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:09 admin drwxr-xr-x 2 oracle oinstall 4096 Oct 21 17:00 checkpoints drwxrwxr-x 11 oracle oinstall 4096 Oct 21 16:59 diag drwxr-xr-x 3 oracle oinstall 4096 Oct 21 16:51 product [root@dg2 oracle]# cd admin/ [root@dg2 admin]# ll total 4 drwxr-xr-x 2 oracle oinstall 4096 Oct 22 11:09 lei [root@dg2 admin]# mkdir fast_recovery_area [root@dg2 admin]# cd fast_recovery_area/ [root@dg2 fast_recovery_area]# mkdir lei [root@dg2 fast_recovery_area]# cd .. [root@dg2 admin]# chown -R oracle:oinstall fast_recovery_area/ [root@dg2 admin]# [root@dg2 admin]# ll total 8 drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:10 fast_recovery_area drwxr-xr-x 2 oracle oinstall 4096 Oct 22 11:09 lei [root@dg2 admin]# cd .. [root@dg2 oracle]# ll total 16 drwxr-xr-x 4 oracle oinstall 4096 Oct 22 11:10 admin drwxr-xr-x 2 oracle oinstall 4096 Oct 21 17:00 checkpoints drwxrwxr-x 11 oracle oinstall 4096 Oct 21 16:59 diag drwxr-xr-x 3 oracle oinstall 4096 Oct 21 16:51 product [root@dg2 oracle]# cd admin/ [root@dg2 admin]# mv fast_recovery_area/ /u01/app/oracle/ [root@dg2 admin]# cd .. [root@dg2 oracle]# ll total 20 drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:11 admin drwxr-xr-x 2 oracle oinstall 4096 Oct 21 17:00 checkpoints drwxrwxr-x 11 oracle oinstall 4096 Oct 21 16:59 diag drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:10 fast_recovery_area drwxr-xr-x 3 oracle oinstall 4096 Oct 21 16:51 product [root@dg2 oracle]# mkdir oradata [root@dg2 oracle]# cd oradata/ [root@dg2 oradata]# mkdir lei [root@dg2 oradata]# cd .. [root@dg2 oracle]# chown -R oracle:oinstall oradata/ [root@dg2 oracle]# ll total 24 drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:11 admin drwxr-xr-x 2 oracle oinstall 4096 Oct 21 17:00 checkpoints drwxrwxr-x 11 oracle oinstall 4096 Oct 21 16:59 diag drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:10 fast_recovery_area drwxr-xr-x 3 oracle oinstall 4096 Oct 22 11:11 oradata drwxr-xr-x 3 oracle oinstall 4096 Oct 21 16:51 product [root@dg2 oracle]# ls admin checkpoints diag fast_recovery_area oradata product
9将主库的参数文件copy到standby并修改
[root@dg1 dbs]# scp initlei.ora 192.168.169.134:/u01/app/oracle/product/11.2.0/db_1/dbs/ reverse mapping checking getaddrinfo for bogon failed - POSSIBLE BREAK-IN ATTEMPT! root@192.168.169.134's password: initlei.ora 100% 1595 1.6KB/s 00:00 *.db_unique_name='lei_st' *.log_archive_config='dg_config=(lei_pd,lei_st)' *.log_archive_dest_1='location=/u01/archive valid_for=(all_logfiles,all_roles) db_unique_name='lei_st' *.log_archive_dest_2='service=lei_pd reopen=120 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=lei_pd' *.log_archive_dest_state_1=enable *.log_archive_dest_state_2=enable *.standby_file_management='auto' *.fal_server='lei_pd' *.log_file_name_convert='/u01/app/oracle/oradate/lei','/u02/oradata' *.db_file_name_convert='/u01/app/oracle/oradate/lei','/u02/oradata' 10用 spfile 将备库启动到 nomount 状态 [oracle@dg2 dbs]$ orz sp SQL*Plus: Release 11.2.0.3.0 Production on Sun Apr 28 20:14:09 2013 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to an idle instance. SQL> startup nomount ORACLE instance started. Total System Global Area 814227456 bytes Fixed Size 2232760 bytes Variable Size 478154312 bytes Database Buffers 331350016 bytes Redo Buffers 2490368 bytes SQL>
11 开始进行 duplicate
这个操作主备库都可以,只要在 RMAN 连接时没写错就可以了。
[oracle@dg2 ~]$ rman target sys/oracle@lei_pd auxiliary sys/oracle@lei_st; Recovery Manager: Release 11.2.0.3.0 - Production on Sun Apr 28 20:18:28 2013 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. connected to target database: lei (DBID=856896964) connected to auxiliary database: lei (not mounted) RMAN> RMAN> duplicate target database for standby from active database; --如果路径相同, 使用这个: duplicate target database for standby from active database nofilenamecheck; Starting Duplicate Db at 28-APR-13 using target database control file instead of recovery catalog allocated channel: ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: SID=20 device type=DISK contents of Memory Script: { backup as copy reuse targetfile '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwlei' auxiliary format '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwlei' ; } --将口令文件 copy 到备库 executing Memory Script Starting backup at 28-APR-13 allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=41 device type=DISK Finished backup at 28-APR-13 contents of Memory Script: { backup as copy current controlfile for standby auxiliary format '/u01/app/oracle/oradata/lei/control01.ctl'; restore clone controlfile to '/u01/app/oracle/fast_recovery_area/lei/control02.ctl' from '/u01/app/oracle/oradata/lei/control01.ctl'; } executing Memory Script --将控制文件 copy 到备库。 Starting backup at 28-APR-13 using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile copy copying standby control file output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_lei.f tag=TAG20130428T202225 RECID=8 STAMP=813961346 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:02 Finished backup at 28-APR-13 Starting restore at 28-APR-13 using channel ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: copied control file copy Finished restore at 28-APR-13 contents of Memory Script: { sql clone 'alter database mount standby database'; } executing Memory Script --讲备库启动到 mount standby 的状态 sql statement: alter database mount standby database contents of Memory Script: { set newname for tempfile 1 to "/u02/oradata/temp01.dbf"; switch clone tempfile all; set newname for datafile 1 to "/u02/oradata/system01.dbf"; set newname for datafile 2 to "/u02/oradata/sysaux01.dbf"; set newname for datafile 3 to "/u02/oradata/undotbs01.dbf"; set newname for datafile 4 to "/u02/oradata/users01.dbf"; backup as copy reuse datafile 1 auxiliary format "/u02/oradata/system01.dbf" datafile 2 auxiliary format "/u02/oradata/sysaux01.dbf" datafile 3 auxiliary format "/u02/oradata/undotbs01.dbf" datafile 4 auxiliary format "/u02/oradata/users01.dbf" ; sql 'alter system archive log current'; } executing Memory Script --改变数据文件的位置 executing command: SET NEWNAME renamed tempfile 1 to /u02/oradata/temp01.dbf in control file executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME Starting backup at 28-APR-13 using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile copy input datafile file number=00001 name=/u01/app/oracle/oradata/lei/system01.dbf output file name=/u02/oradata/system01.dbf tag=TAG20130428T202236 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:12 channel ORA_DISK_1: starting datafile copy input datafile file number=00002 name=/u01/app/oracle/oradata/lei/sysaux01.dbf output file name=/u02/oradata/sysaux01.dbf tag=TAG20130428T202236 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:02 channel ORA_DISK_1: starting datafile copy input datafile file number=00003 name=/u01/app/oracle/oradata/lei/undotbs01.dbf output file name=/u02/oradata/undotbs01.dbf tag=TAG20130428T202236 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15 channel ORA_DISK_1: starting datafile copy input datafile file number=00004 name=/u01/app/oracle/oradata/lei/users01.dbf output file name=/u02/oradata/users01.dbf tag=TAG20130428T202236 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03 Finished backup at 28-APR-13 --开始复制数据文件 sql statement: alter system archive log current contents of Memory Script: { switch clone datafile all; } executing Memory Script --将数据文件的修改刷新到控制文件 datafile 1 switched to datafile copy input datafile copy RECID=8 STAMP=813961967 file name=/u02/oradata/system01.dbf datafile 2 switched to datafile copy input datafile copy RECID=9 STAMP=813961967 file name=/u02/oradata/sysaux01.dbf datafile 3 switched to datafile copy input datafile copy RECID=10 STAMP=813961967 file name=/u02/oradata/undotbs01.dbf datafile 4 switched to datafile copy input datafile copy RECID=11 STAMP=813961967 file name=/u02/oradata/users01.dbf Finished Duplicate Db at 28-APR-13
--完成 duplicate 操作。
12 启动备库
--完成 duplicate 之后,备库就是 mount 状态:
SQL> select open_mode from v$database; OPEN_MODE -------------------- MOUNTED SQL> alter database open; Database altered. SQL> select open_mode from v$database; OPEN_MODE -------------------- READ ONLY
13 启动 MRP 进程
SQL> alter database recover managed standby database disconnect from session; Database altered. SQL> select open_mode from v$database; OPEN_MODE -------------------- READ ONLY WITH APPLY SQL>
14 验证 DG
14.1 主库
SQL> col error for a10 SQL> col dest_name for a20 SQL> alter system switch logfile; System altered. SQL> select max(sequence#) from v$archived_log; MAX(SEQUENCE#) -------------- 43
14.2备库
SQL> select max(sequence#) from v$archived_log; MAX(SEQUENCE#) -------------- 43 SQL> select sequence#,applied from v$archived_log; SEQUENCE# APPLIED ---------- --------- 40 YES 41 YES 42 YES 43 YES