程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> 使用dataguard將單實例數據庫轉換為rac數據庫

使用dataguard將單實例數據庫轉換為rac數據庫

編輯:Oracle教程

使用dataguard將單實例數據庫轉換為rac數據庫


我們經常將oracle rac數據庫通過dataguard容災到一個單實例數據庫中,當生產庫出現故障後可以將生產庫切換到dg服務器上。而當生產庫rac修復完成後,我們同樣可以通過dg將數據回切到生產庫中。

此次測試將單實例數據庫通過dg轉換為rac數據庫,這個單實例是剛創建的,並不是由rac通過dg生成的。

在做dg之前需要安裝完成grid,不需要創建數據庫實例。在日志應用的時候必須只能是一個實例應用。

具體環境如下:

單實例庫:

數據庫版本:11.2.0.4.0

sid:suq

db_unique_name=suq

ip:192.168.56.35

hostname:11g1

rac數據庫:

 

數據庫版本:11.2.0.4.0

sid:suq1、suq2

db_unique_name=suq1

ip地址規劃:

#PUBLIC
192.168.56.110 suq1
192.168.56.112 suq2
#VIP
192.168.56.111 suq1-vip
192.168.56.113 suq2-vip
#PRIV
172.168.1.21 suq1-priv
172.168.1.23 suq2-priv
#SCAN
192.168.56.25 suq-cluster-scan

具體步驟如下:

1.配置一些rac和dg的基本參數,包括:

修改主庫force logging、創建standby logfile、創建密碼文件並且拷貝到rac下、添加主庫和備庫的服務命名:

 

alter database force logging;

alter database add standby logfile group 4 ('/opt/oracle/oradata/suq/stand_redo04.log') size 50m;

alter database add standby logfile group 5 ('/opt/oracle/oradata/suq/stand_redo05.log') size 50m;

alter database add standby logfile group 6 ('/opt/oracle/oradata/suq/stand_redo06.log') size 50m;

alter database add standby logfile group 7 ('/opt/oracle/oradata/suq/stand_redo07.log') size 50m;


##
primary=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.35)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = suq)
    )
  )

standby=
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.111)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = suq1)
      (UR = A)
    )
  )
  

注意:standby端即rac端的ip地址寫的是suq1的vip地址,而且需要加上(UR=A)選項,否則通過sqlplus連接不到rac實例。

 

2.生成standby端的oracle 參數文件,這一步參數裡面的內容及其重要,必須寫正確,特別是rac和dg的參數,目前我的rac有三個磁盤組

 

[grid@suq2 ~]$ asmcmd lsdg
State    Type    Rebal  Sector  Block       AU  Total_MB  Free_MB  Req_mir_free_MB  Usable_file_MB  Offline_disks  Voting_files  Name
MOUNTED  EXTERN  N         512   4096  1048576      3435     3329                0            3329              0             N  BACKUP/
MOUNTED  EXTERN  N         512   4096  1048576      4305     1988                0            1988              0             N  DATADG/
MOUNTED  EXTERN  N         512   4096  1048576      1458     1058                0            1058              0             Y  SYSTEMDG/

 

下面是我寫好的一個參數文件:

 

suq.__db_cache_size=335544320
suq.__java_pool_size=4194304
suq.__large_pool_size=4194304
suq.__oracle_base='/u01/app'
suq.__pga_aggregate_target=335544320
suq.__sga_target=503316480
suq.__shared_io_pool_size=0
suq.__shared_pool_size=146800640
suq.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/suq'
*.audit_trail='none'
*.compatible='11.2.0.4.0'
*.control_files='+DATADG/suq/control.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='suq'
*.db_recovery_file_dest='+BACKUP'
*.db_recovery_file_dest_size=5218762752
*.diagnostic_dest='/u01/app/oracle'
*.memory_target=838860800
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
#### rac parameter 
suq1.local_listener='(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.56.111)(PORT=1521))'
suq2.local_listener='(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.56.113)(PORT=1521))'
suq1.undo_tablespace='UNDOTBS1'
suq2.undo_tablespace='UNDOTBS2'
suq1.remote_listener='suq-cluster-scan:1521'
suq2.remote_listener='suq-cluster-scan:1521'
suq1.instance_name=suq1
suq2.instance_name=suq2
suq1.instance_number=1
suq2.instance_number=2
*.cluster_database=true
*.cluster_database_instances=2
*.db_create_file_dest='+DATADG'
*.db_create_online_log_dest_1='+DATADG'
suq1.thread=1
suq2.thread=2
### dg parameter 
*.db_unique_name=suq1
*.log_archive_config='dg_config=(suq,suq1)'
*.standby_file_management='AUTO'
*.db_file_name_convert='/opt/oracle/oradata/suq','+DATADG/suq/datafile'
*.log_file_name_convert='/opt/oracle/oradata/suq','+DATADG/suq/datafile'
*.log_archive_dest_1='location=+datadg/suq/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=suq1'
*.log_archive_dest_2='service=primary lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=suq'
*.fal_server=primary
*.fal_client=standby


 

注意:suq1、suq2的local_listener和remote_listener必須配置正確,否則打開庫之後scan ip可能失效

3.配置主庫的dg參數:

 

alter system set log_archive_config='dg_config=(suq,suq1)' scope=both;  

alter system set log_archive_dest_1='location=/opt/arch valid_for=(all_logfiles,all_roles) db_unique_name=suq' scope=both;

alter system set log_archive_dest_2='service=standby lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=suq1' scope=both;

alter system set fal_server=suq1 scope=both;

alter system set fal_client=suq scope=both;


 

4.在保證所有需要的目錄都已經創建完成的情況下,將備庫啟動到nomount,並且測試tnsnames的連通性:

 

SQL>startup nomount pfile='/home/oracle/initsuq1.ora'
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size		    2257840 bytes
Variable Size		  553651280 bytes
Database Buffers	  272629760 bytes
Redo Buffers		    6565888 bytes
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
[oracle@suq1 ~]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 26-JUL-2015 21:27:45

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                26-JUL-2015 18:58:27
Uptime                    0 days 2 hr. 29 min. 17 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/11.2.0/grid/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/suq1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=LISTENER)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.56.110)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.56.111)(PORT=1521)))
Services Summary...
Service "+ASM" has 1 instance(s).
  Instance "+ASM1", status READY, has 1 handler(s) for this service...
Service "suq1" has 1 instance(s).
  Instance "suq1", status BLOCKED, has 1 handler(s) for this service...
The command completed successfully


[oracle@11g1 ~]$  sqlplus sys/manager@standby as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Sun Jul 26 21:48:08 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> exit

[oracle@suq1 ~]$ sqlplus sys/manager@primary as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Sun Jul 26 21:50:25 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> exit

5.使用duplicate 創建standby database:

 

 

[oracle@11g1 arch]$ rman target /

Recovery Manager: Release 11.2.0.4.0 - Production on Sun Jul 26 22:28:11 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: SUQ (DBID=1469685490)

RMAN> connect auxiliary sys/manager@standby

connected to auxiliary database: SUQ (not mounted)

RMAN> duplicate target database for standby from active database; 

Starting Duplicate Db at 26-JUL-15
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=141 instance=suq1 device type=DISK

contents of Memory Script:
{
   backup as copy reuse
   targetfile  '/opt/oracle/product/OraHome11204/dbs/orapwsuq' auxiliary format 
 '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwsuq1'   ;
}
executing Memory Script

Starting backup at 26-JUL-15
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=147 device type=DISK
Finished backup at 26-JUL-15

contents of Memory Script:
{
   backup as copy current controlfile for standby auxiliary format  '+DATADG/suq/control.ctl';
}
executing Memory Script

Starting backup at 26-JUL-15
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/opt/oracle/product/OraHome11204/dbs/snapcf_suq.f tag=TAG20150726T222820 RECID=3 STAMP=886112900
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 26-JUL-15

contents of Memory Script:
{
   sql clone 'alter database mount standby database';
}
executing Memory Script

sql statement: alter database mount standby database

contents of Memory Script:
{
   set newname for tempfile  1 to 
 "+DATADG/suq/datafile/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to 
 "+DATADG/suq/datafile/system01.dbf";
   set newname for datafile  2 to 
 "+DATADG/suq/datafile/sysaux01.dbf";
   set newname for datafile  3 to 
 "+DATADG/suq/datafile/undotbs01.dbf";
   set newname for datafile  4 to 
 "+DATADG/suq/datafile/users01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format 
 "+DATADG/suq/datafile/system01.dbf"   datafile 
 2 auxiliary format 
 "+DATADG/suq/datafile/sysaux01.dbf"   datafile 
 3 auxiliary format 
 "+DATADG/suq/datafile/undotbs01.dbf"   datafile 
 4 auxiliary format 
 "+DATADG/suq/datafile/users01.dbf"   ;
   sql 'alter system archive log current';
}
executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to +DATADG/suq/datafile/temp01.dbf in control file

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting backup at 26-JUL-15
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/opt/oracle/oradata/suq/system01.dbf
output file name=+DATADG/suq/datafile/system01.dbf tag=TAG20150726T222829
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/opt/oracle/oradata/suq/sysaux01.dbf
output file name=+DATADG/suq/datafile/sysaux01.dbf tag=TAG20150726T222829
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/opt/oracle/oradata/suq/undotbs01.dbf
output file name=+DATADG/suq/datafile/undotbs01.dbf tag=TAG20150726T222829
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=/opt/oracle/oradata/suq/users01.dbf
output file name=+DATADG/suq/datafile/users01.dbf tag=TAG20150726T222829
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 26-JUL-15

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=886113015 file name=+DATADG/suq/datafile/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=4 STAMP=886113015 file name=+DATADG/suq/datafile/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=886113015 file name=+DATADG/suq/datafile/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=886113015 file name=+DATADG/suq/datafile/users01.dbf
Finished Duplicate Db at 26-JUL-15

RMAN> exit


Recovery Manager complete.

6.standby開啟實時應用:

 

 

SQL> alter database recover managed standby database disconnect from session;

SQL> alter database recover managed standby database cancel;

SQL> alter database open read only;

Database altered.

SQL> alter database recover managed standby database using current logfile disconnect from session;

Database altered.

注意:我在做到這一步的時候一開始發現主庫怎麼也不向備庫發送歸檔文件,也沒有任何報錯信息,搞了很長時間,後來將log_archive_dest_state_2重新設置為enable後就正常了。

 

7.switch over,將備庫轉換為主庫,分別在主庫我備庫進行角色轉換:

 

主庫:

SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;

SWITCHOVER_STATUS
--------------------
SESSIONS ACTIVE

注:當SWITCHOVER_STATUS的值為TO STANDBY or SESSIONS ACTIVE的時候表示主庫可以支持SWITCHOVER.

SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;

Database altered.



備庫:

SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;

SWITCHOVER_STATUS
--------------------
SESSIONS ACTIVE

注:只有SWITCHOVER_STATUS的值為TO PRIMARY or SESSIONS ACTIVE的狀態時,表示備庫當前支持switch over操作。

SQL>  ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY with session shutdown;

Database altered.

SQL> select status from v$instance;

STATUS
------------
MOUNTED

SQL> alter database open;

Database altered.


 

8.到這一步,rac的一個節點已經可以正常打開數據庫了,但是節點2需要的redo,undo表空間都還沒有,需要手工創建:

 

SQL> alter database add logfile thread 2 group 8 ('+DATADG/suq/datafile/redo08.log') size 50m;

Database altered.

SQL> alter database add logfile thread 2 group 9 ('+DATADG/suq/datafile/redo09.log') size 50m;

Database altered.

SQL> alter database add logfile thread 2 group 10 ('+DATADG/suq/datafile/redo10.log') size 50m;

Database altered.

SQL> create undo tablespace undotbs2 datafile '+DATADG/suq/datafile/undotbs02.dbf' size 20m;

Tablespace created.

SQL> alter database enable public thread 2; 

Database altered.


SQL> @?/rdbms/admin/catclust.sql

Package created.


Package body created.


PL/SQL procedure successfully completed.


View created.


Synonym created.


Grant succeeded.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


View created.


Grant succeeded.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


Synonym created.


View created.


Grant succeeded.


Synonym created.


View created.


Grant succeeded.


Synonym created.


PL/SQL procedure successfully completed.


 

節點2打開數據庫,測試數據庫是否正常:

 

[oracle@suq2 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Mon Jul 27 09:54:00 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size		    2257840 bytes
Variable Size		  553651280 bytes
Database Buffers	  272629760 bytes
Redo Buffers		    6565888 bytes
Database mounted.
Database opened.
SQL> 

SQL> select * from v$active_instances;

INST_NUMBER INST_NAME
----------- --------------------------------------------------
	  1 suq1:suq1


	  2 suq2:suq2
	  
SQL> select * from v$Log;

    GROUP#    THREAD#  SEQUENCE#      BYTES  BLOCKSIZE	  MEMBERS ARC STATUS	       FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME
---------- ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- ------------ ---------
	 1	    1	      35   52428800	   512		1 NO  CURRENT		      288331 27-JUL-15	 2.8147E+14
	 2	    1	      33   52428800	   512		1 YES INACTIVE		      281944 27-JUL-15	     282047 27-JUL-15
	 3	    1	      34   52428800	   512		1 YES ACTIVE		      282047 27-JUL-15	     288331 27-JUL-15
	 8	    2	       4   52428800	   512		1 NO  CURRENT		      288227 27-JUL-15	 2.8147E+14
	 9	    2	       2   52428800	   512		1 YES INACTIVE		      286810 27-JUL-15	     286815 27-JUL-15
	10	    2	       3   52428800	   512		1 YES ACTIVE		      286815 27-JUL-15	     288227 27-JUL-15

8.此時的數據庫是可以正常打開使用了,一般還需將參數文件寫到asm中,而且此時的數據庫都是本地管理,並沒有注冊到grid中,還需要手工注冊:

 

 

[grid@suq1 admin]$ srvctl config database
[grid@suq1 admin]$ 
[grid@suq1 admin]$ crsctl stat res -t
--------------------------------------------------------------------------------
NAME           TARGET  STATE        SERVER                   STATE_DETAILS       
--------------------------------------------------------------------------------
Local Resources
--------------------------------------------------------------------------------
ora.BACKUP.dg
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.DATADG.dg
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.LISTENER.lsnr
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.SYSTEMDG.dg
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.asm
               ONLINE  ONLINE       suq1                     Started             
               ONLINE  ONLINE       suq2                     Started             
ora.gsd
               OFFLINE OFFLINE      suq1                                         
               OFFLINE OFFLINE      suq2                                         
ora.net1.network
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.ons
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
ora.registry.acfs
               ONLINE  ONLINE       suq1                                         
               ONLINE  ONLINE       suq2                                         
--------------------------------------------------------------------------------
Cluster Resources
--------------------------------------------------------------------------------
ora.LISTENER_SCAN1.lsnr
      1        ONLINE  ONLINE       suq2                                         
ora.cvu
      1        ONLINE  ONLINE       suq2                                         
ora.oc4j
      1        ONLINE  ONLINE       suq2                                         
ora.scan1.vip
      1        ONLINE  ONLINE       suq2                                         
ora.suq1.vip
      1        ONLINE  ONLINE       suq1                                         
ora.suq2.vip
      1        ONLINE  ONLINE       suq2  
      
[oracle@suq1 ~]$ srvctl add database -d suq -o /u01/app/oracle/product/11.2.0/db_1
[oracle@suq1 ~]$ srvctl config database
suq
[oracle@suq1 ~]$ srvctl add instance -d suq -n suq1 -i suq1
[oracle@suq1 ~]$ srvctl add instance -d suq -n suq2 -i suq2

SQL> create spfile='+datadg' from pfile;

File created.
[root@suq1 cssd]# srvctl modify database -d suq -p '+datadg/suq1/parameterfile/spfile.325.886161615' 


 

 

[oracle@suq1 dbs]$ srvctl config database -d suq -a
Database unique name: suq
Database name: 
Oracle home: /u01/app/oracle/product/11.2.0/db_1
Oracle user: oracle
Spfile: +datadg/suq1/parameterfile/spfile.325.886161615
Domain: 
Start options: open
Stop options: immediate
Database role: PRIMARY
Management policy: AUTOMATIC
Server pools: suq
Database instances: suq1,suq2
Disk Groups: 
Mount point paths: 
Services: 
Type: RAC
Database is enabled
Database is administrator managed

查看一下監聽,有可能還需要修改services_name參數:

 

 

SQL> alter system set service_names=suq scope=both;

到此,單實例已經轉換完成了rac數據庫。可以重啟一下機器,看看數據庫能否自動啟動,資源是否正常。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved