环境描述:有两台机器,11gdg1、11gdg2 。11gdg1上安装了数据库dgtst 。 11gdg2上只安装了Oracle软件,并没有安装数据库。以下描述如何在11gdg2上建立一个standby 数据库。
一、 standby 数据库准备
1.1 创建从库的静态监听。
11gdg2-> cat /u01/app/oracle/product/11.2.0/db_1/network/admin/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 = dgtst) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = dgtst) ) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11gdg2.localdomain.com)(PORT = 1521)) ) ADR_BASE_LISTENER = /u01/app/oracle
1.2 建立一个最简单的init.ora参数文件,DB_NAME的值可以任意。
11gdg2-> echo 'DB_NAME=WHATEVER' > $ORACLE_HOME/dbs/initdgtst.ora
1.3 创建口令文件
11gdg2-> orapwd file=/u01/app/oracle/product/11.2.0/db_1/dbs/orapwdgtst password=oracle
密码的值要与主数据库中sys用户的密码相同。
1.4 启动数据库到nomount
11gdg2-> sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Thu Jul 9 22:47:43 2015 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to an idle instance. SQL> startup nomount ORACLE instance started. Total System Global Area 238034944 bytes Fixed Size 2227136 bytes Variable Size 180356160 bytes Database Buffers 50331648 bytes Redo Buffers 5120000 bytes
1.5 建立指向11gdg1的TNSNAME .
11gdg2-> 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. 11GDG1 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.216)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = dgtst) ) ) 11GDG2 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.217)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = dgtst) ) )
1.6 创建数据库必须的目录
11gdg2-> mkdir -p /u01/app/oracle/fast_recovery_area/dgtst 11gdg2-> mkdir -p /u01/app/oracle/admin/dgtst/adump 11gdg2-> mkdir -p /u01/app/oracle/oradata/dgtst/
二、Primary数据库准备工作
2.1 将主库改为FORCE LOGGING
SQL> alter database force logging; Database altered.
2.2 创建4组standby log
SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/dgtst/sredo01.log' SIZE 50M; Database altered. SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/dgtst/sredo02.log' SIZE 50M; Database altered. SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/dgtst/sredo03.log' SIZE 50M; Database altered. SQL> ALTER DATABASE ADD STANDBY LOGFILE '/u01/app/oracle/oradata/dgtst/sredo04.log' SIZE 50M; Database altered.
2.3 修改DB_UNIQUE_NAME、SERVICE_NAME
SQL> ALTER SYSTEM SET db_unique_name='11gdg1' scope=spfile; System altered. SQL> alter system set service_names=dgtst; System altered. SQL> startup force; 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.
2.4 建立指向11gdg2的TNSNAME
[root@11gdg1 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. 11GDG1 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.216)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = 11gdg1) ) ) 11GDG2 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.199.217)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = 11gdg2) ) )
三、创建standby数据库 。
以下脚本在primary数据库,或者standby数据库上执行都是可以的。
11gdg1-> rman Recovery Manager: Release 11.2.0.3.0 - Production on Fri Jul 10 00:08:16 2015 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. RMAN> CONNECT TARGET sys/oracle@11gdg1; connected to target database: DGTST (DBID=353611471) RMAN> CONNECT AUXILIARY sys/oracle@11gdg2; connected to auxiliary database: WHATEVER (not mounted) RMAN> run { allocate channel prmy1 type disk; allocate channel prmy2 type disk; allocate channel prmy3 type disk; allocate channel prmy4 type disk; allocate channel prmy5 type disk; allocate auxiliary channel stby1 type disk; duplicate target database for standby from active database spfile set 'db_unique_name'='11gdg2' set control_files='/u01/app/oracle/oradata/dgtst/control01.ctl' set db_recovery_file_dest='/u01/app/oracle/fast_recovery_area' set DB_RECOVERY_FILE_DEST_SIZE='4G' nofilenamecheck; }9> 10> 11> 12> 13> 14> 15> using target database control file instead of recovery catalog allocated channel: prmy1 channel prmy1: SID=37 device type=DISK allocated channel: prmy2 channel prmy2: SID=44 device type=DISK allocated channel: prmy3 channel prmy3: SID=41 device type=DISK allocated channel: prmy4 channel prmy4: SID=45 device type=DISK allocated channel: prmy5 channel prmy5: SID=49 device type=DISK allocated channel: stby1 channel stby1: SID=20 device type=DISK Starting Duplicate Db at 2015/07/10 00:08:48 contents of Memory Script: { backup as copy reuse targetfile '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwdgtst' auxiliary format '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwdgtst' targetfile '/u01/app/oracle/product/11.2.0/db_1/dbs/spfiledgtst.ora' auxiliary format '/u01/app/oracle/product/11.2.0/db_1/dbs/spfiledgtst.ora' ; sql clone "alter system set spfile= ''/u01/app/oracle/product/11.2.0/db_1/dbs/spfiledgtst.ora''"; } executing Memory Script Starting backup at 2015/07/10 00:08:48 Finished backup at 2015/07/10 00:08:50 sql statement: alter system set spfile= ''/u01/app/oracle/product/11.2.0/db_1/dbs/spfiledgtst.ora'' contents of Memory Script: { sql clone "alter system set db_unique_name = ''11gdg2'' comment= '''' scope=spfile"; sql clone "alter system set control_files = ''/u01/app/oracle/oradata/dgtst/control01.ctl'' comment= '''' scope=spfile"; sql clone "alter system set db_recovery_file_dest = ''/u01/app/oracle/fast_recovery_area'' comment= '''' scope=spfile"; sql clone "alter system set DB_RECOVERY_FILE_DEST_SIZE = 4G comment= '''' scope=spfile"; shutdown clone immediate; startup clone nomount; } executing Memory Script sql statement: alter system set db_unique_name = ''11gdg2'' comment= '''' scope=spfile sql statement: alter system set control_files = ''/u01/app/oracle/oradata/dgtst/control01.ctl'' comment= '''' scope=spfile sql statement: alter system set db_recovery_file_dest = ''/u01/app/oracle/fast_recovery_area'' comment= '''' scope=spfile sql statement: alter system set DB_RECOVERY_FILE_DEST_SIZE = 4G comment= '''' scope=spfile Oracle instance shut down connected to auxiliary database (not started) 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 allocated channel: stby1 channel stby1: SID=18 device type=DISK contents of Memory Script: { backup as copy current controlfile for standby auxiliary format '/u01/app/oracle/oradata/dgtst/control01.ctl'; } executing Memory Script Starting backup at 2015/07/10 00:09:00 channel prmy1: starting datafile copy copying standby control file output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_dgtst.f tag=TAG20150710T000900 RECID=3 STAMP=884650140 channel prmy1: datafile copy complete, elapsed time: 00:00:01 Finished backup at 2015/07/10 00:09:01 contents of Memory Script: { sql clone 'alter database mount standby database'; } executing Memory Script sql statement: alter database mount standby database RMAN-05538: WARNING: implicitly using DB_FILE_NAME_CONVERT contents of Memory Script: { set newname for tempfile 1 to "/u01/app/oracle/oradata/dgtst/temp01.dbf"; switch clone tempfile all; set newname for datafile 1 to "/u01/app/oracle/oradata/dgtst/system01.dbf"; set newname for datafile 2 to "/u01/app/oracle/oradata/dgtst/sysaux01.dbf"; set newname for datafile 3 to "/u01/app/oracle/oradata/dgtst/undotbs01.dbf"; set newname for datafile 4 to "/u01/app/oracle/oradata/dgtst/users01.dbf"; backup as copy reuse datafile 1 auxiliary format "/u01/app/oracle/oradata/dgtst/system01.dbf" datafile 2 auxiliary format "/u01/app/oracle/oradata/dgtst/sysaux01.dbf" datafile 3 auxiliary format "/u01/app/oracle/oradata/dgtst/undotbs01.dbf" datafile 4 auxiliary format "/u01/app/oracle/oradata/dgtst/users01.dbf" ; sql 'alter system archive log current'; } executing Memory Script executing command: SET NEWNAME renamed tempfile 1 to /u01/app/oracle/oradata/dgtst/temp01.dbf in control file executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME Starting backup at 2015/07/10 00:09:07 channel prmy1: starting datafile copy input datafile file number=00001 name=/u01/app/oracle/oradata/dgtst/system01.dbf channel prmy2: starting datafile copy input datafile file number=00002 name=/u01/app/oracle/oradata/dgtst/sysaux01.dbf channel prmy3: starting datafile copy input datafile file number=00003 name=/u01/app/oracle/oradata/dgtst/undotbs01.dbf channel prmy4: starting datafile copy input datafile file number=00004 name=/u01/app/oracle/oradata/dgtst/users01.dbf output file name=/u01/app/oracle/oradata/dgtst/undotbs01.dbf tag=TAG20150710T000907 channel prmy3: datafile copy complete, elapsed time: 00:00:03 output file name=/u01/app/oracle/oradata/dgtst/users01.dbf tag=TAG20150710T000907 channel prmy4: datafile copy complete, elapsed time: 00:00:03 output file name=/u01/app/oracle/oradata/dgtst/sysaux01.dbf tag=TAG20150710T000907 channel prmy2: datafile copy complete, elapsed time: 00:00:36 output file name=/u01/app/oracle/oradata/dgtst/system01.dbf tag=TAG20150710T000907 channel prmy1: datafile copy complete, elapsed time: 00:00:46 Finished backup at 2015/07/10 00:09:53 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=3 STAMP=884650194 file name=/u01/app/oracle/oradata/dgtst/system01.dbf datafile 2 switched to datafile copy input datafile copy RECID=4 STAMP=884650194 file name=/u01/app/oracle/oradata/dgtst/sysaux01.dbf datafile 3 switched to datafile copy input datafile copy RECID=5 STAMP=884650194 file name=/u01/app/oracle/oradata/dgtst/undotbs01.dbf datafile 4 switched to datafile copy input datafile copy RECID=6 STAMP=884650194 file name=/u01/app/oracle/oradata/dgtst/users01.dbf Finished Duplicate Db at 2015/07/10 00:09:58 released channel: prmy1 released channel: prmy2 released channel: prmy3 released channel: prmy4 released channel: prmy5 released channel: stby1
standby数据库创建起来啦,但是现在还不能实现数据同步。我们还有很多的配置工作要做。
四、配置主从数据库的相关参数。
4.1 在主库上配置日志传输参数
SQL> ALTER SYSTEM SET LOG_ARCHIVE_CONFIG='DG_CONFIG=(11gdg1,11gdg2)'; System altered. SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='service=11gdg2 ASYNC DB_UNIQUE_NAME=11gdg2 VALID_FOR=(primary_role,online_logfile)'; System altered. SQL>
4.2 在主库上配置standby数据库属性(日后当主库切换成standby时,这些参数才用的到)
SQL> ALTER SYSTEM SET FAL_SERVER='11gdg2'; System altered. SQL> ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=AUTO; System altered. SQL>
4.3 在从库上配置standby数据库属性
SQL> ALTER SYSTEM SET FAL_SERVER='11gdg1'; System altered. SQL> ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=AUTO; System altered.
4.4 在从库上配置primary数据库属性(日后当从库切换成主库时,这些参数才用的到)
SQL> ALTER SYSTEM SET LOG_ARCHIVE_CONFIG='DG_CONFIG=(11gdg1,11gdg2)'; SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='service=11gdg1 ASYNC DB_UNIQUE_NAME=11gdg1 VALID_FOR=(primary_role,online_logfile)';
五、从库启动日志应用,并验证数据是否能同步。
5.1 同步数据
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT; Database altered.
5.2 我们在主库中,修改表中的数据库,看看从库是否会同步数据。
SQL> conn scott/tiger Connected. SQL> create table t1 as select * from emp; Table created. SQL> select count(*) from t1; COUNT(*) ---------- 14
5.3 从库只读打开
SQL> alter database open read only; alter database open read only * ERROR at line 1: ORA-10456: cannot open standby database; media recovery session may be in progress
需要先取消日志应用
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
再打开数据库并验证数据
SQL> alter database open read only; Database altered. SQL> conn scott/tiger Connected. SQL> select count(*) from t1; COUNT(*) ---------- 14
可见数据已经同步成功。
至此,我们的standby数据库装好啦 ~~