Tuesday 22 March 2016

Can I Store Datapump dumpfiles in ASM diskgroup?




Step 1:
 Go To ASM Instance and Create New Directory.


I have 2 DISKS (i.e, DATA & NEWDATA)

ASMCMD [+] > ls
DATA/
NEWDATA/

SQL> alter diskgroup data add directory '+DATA/Dataset';
Diskgroup altered.

ASMCMD [+] > cd DATA
ASMCMD [+DATA] > ls
ASM/
ASMDB/
Dataset/                                           <---- // Created using above command
MYASM/
ORCL/
ORCL1/




Step 2:
 Go to DB Instance

SQL> create or replace directory datapump_asm as '+DATA/Dataset/';
Directory created.

SQL> grant read , write on directory datapump_asm to system;
Grant succeeded.

SQL> create or replace directory logfile as '/u01/';
Directory created.

SQL> grant read , write on directory logfile to system;
Grant succeeded.

NOTE:   ‘Nikhil’ is user  &  NIK_EMP is a Table


Step 3: Doing Export using datapump

SQL> ! expdp system/sys directory=datapump_asm dumpfile=testASM.dmp schemas=nikhil.nik_emp logfile=logfile:abc.log;

Export: Release 11.2.0.1.0 - Production on Tue Mar 22 00:17:27 2016

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Starting "SYSTEM"."SYS_EXPORT_SCHEMA_01":  system/******** directory=datapump_asm dumpfile=testASM.dmp schemas=nikhil.nik_emp logfile=logfile:abc.log
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 0 KB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Master table "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_SCHEMA_01 is:
  +DATA/dataset/testasm.dmp
Job "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully completed at 00:19:58


SQL>

Step 4: Go to ASM Instance and Check the file created in ASM

SQL> select file_number,creation_date,bytes from v$asm_file where type='DUMPSET';

FILE_NUMBER CREATION_         BYTES
----------- --------- ----------
                322 22-MAR-16                 159744



Cloning of Database using RMAN in ASM In another Diskgroup


Cloning of Database includes below mentioned steps :-


1. Take a fresh backup of your OLD database of which you want to make a clone.

2. Create a pfile for OLD database.
SQL> create pfile=''/u01/asm1/initasmnew.ora' from spfile;

Modify the parameters in green colour of this pfile according to the New Database server.
*.compatible='11.2.0.0.0'
*.control_files='+NEWDATA/asmnew/controlfile/current.281.907165777'
*.db_block_size=8192
*.db_create_file_dest='+DATA'
*.db_domain=''
*.db_name='asm2'
*.db_unique_name=’asmnew
*.db_recovery_file_dest='/u01/asm1'
*.db_recovery_file_dest_size=9437184000
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=asm2XDB)'
*.open_cursors=300
*.pga_aggregate_target=104857600
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=314572800
*.undo_tablespace='UNDOTBS1'



SQL> !rman target /

RMAN> backup device type disk format '/ u01/asm1/ASM2/database_format%u' database;

RMAN> backup device type disk format '/ u01/asm1/ASM2/arch_format%u' archivelog all;



ANOTHER TERMINAL

[oracle@oracleasm1 Desktop]$ export ORACLE_SID=asmnew
[oracle@oracleasm1 Desktop]$ rlwrap sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Tue Mar 22 15:09:03 2016

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

Connected to an idle instance.


3. Use the above pfile to startup in nomount mode the NEW database. 

SQL> startup nomount pfile='/u01/asm1/initasmnew.ora' ;


4. Restore control files of OLD database to new host database.
RMAN> restore controlfile from '/u01/asm1/ASM2/autobackup/2016_03_22/o1_mf_s_907167734_ch24901d_.bkp';


5. Mount your New Database.

RMAN> alter database mount;

database mounted
released channel: ORA_DISK_1

6. Catalog all Backup pieces of OLD database server to new database server

RMAN> catalog backuppiece '/u01/asm1/ASM2/backupset/2016_03_22/o1_mf_nnndf_TAG20160322T150047_ch2467rc_.bkp';


Recovery Manager: Release 11.2.0.1.0 - Production on Tue Mar 22 15:19:55 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved. 
connected to target database: ASM2 (DBID=3723596112, not open)

7. Run the restore command to restore the datafiles.
TO run restore command you need the details of the datafiles of OLD database.

Login into the OLD database and get below mentioned details:-

SQL> Select file_id,file_name from dba_data_files

RMAN> run {
2> set newname for datafile 1 to '+NEWDATA' ;
3> set newname for datafile 2 to '+NEWDATA' ;
4> set newname for datafile 3 to '+NEWDATA' ;
5> set newname for datafile 4 to '+NEWDATA' ;
6> restore database ;
7> switch datafile all;
8> }

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting restore at 22-MAR-16
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=23 device type=DISK

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 +NEWDATA
channel ORA_DISK_1: restoring datafile 00002 to +NEWDATA
channel ORA_DISK_1: restoring datafile 00003 to +NEWDATA
channel ORA_DISK_1: restoring datafile 00004 to +NEWDATA
channel ORA_DISK_1: reading from backup piece /u01/asm1/ASM2/backupset/2016_03_22/o1_mf_nnndf_TAG20160322T150047_ch2467rc_.bkp
channel ORA_DISK_1: piece handle=/u01/asm1/ASM2/backupset/2016_03_22/o1_mf_nnndf_TAG20160322T150047_ch2467rc_.bkp tag=TAG20160322T150047
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:02:56
Finished restore at 22-MAR-16

datafile 1 switched to datafile copy
input datafile copy RECID=5 STAMP=907169129 file name=+NEWDATA/asmnew/datafile/system.264.907168953
datafile 2 switched to datafile copy
input datafile copy RECID=6 STAMP=907169129 file name=+NEWDATA/asmnew/datafile/sysaux.265.907168953
datafile 3 switched to datafile copy
input datafile copy RECID=7 STAMP=907169129 file name=+NEWDATA/asmnew/datafile/undotbs1.266.907168955
datafile 4 switched to datafile copy
input datafile copy RECID=8 STAMP=907169129 file name=+NEWDATA/asmnew/datafile/users.267.907168955

8. Recover the Database
RMAN> recover database ;

Starting recover at 22-MAR-16
using channel ORA_DISK_1

starting media recovery

archived log for thread 1 with sequence 5 is already on disk as file /u01/asm1/ASM2/archivelog/2016_03_22/o1_mf_1_5_ch248wr9_.arc
archived log for thread 1 with sequence 6 is already on disk as file +DATA/asm2/onlinelog/group_3.284.907165799
archived log file name=/u01/asm1/ASM2/archivelog/2016_03_22/o1_mf_1_5_ch248wr9_.arc thread=1 sequence=5
archived log file name=+DATA/asm2/onlinelog/group_3.284.907165799 thread=1 sequence=6
media recovery complete, elapsed time: 00:00:10
Finished recover at 22-MAR-16

RMAN> exit


Recovery Manager complete.

SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
+NEWDATA/asmnew/datafile/system.264.907168953
+NEWDATA/asmnew/datafile/sysaux.265.907168953
+NEWDATA/asmnew/datafile/undotbs1.266.907168955
+NEWDATA/asmnew/datafile/users.267.907168955

9. Rename the ONLINE redo logs file
Rename all logfiles :-

SQL> alter database rename file '+DATA/asm2/onlinelog/group_1.282.907165781' to '+NEWDATA/asmnew/redo01.log';

SQL> alter database rename file '+DATA/asm2/onlinelog/group_2.283.907165791' to
  2  '+NEWDATA/asmnew/redo02.log';

SQL> alter database rename file '+DATA/asm2/onlinelog/group_3.284.907165799' to
  2  '+NEWDATA/asmnew/redo03.log';

SQL> alter database rename file '/u01/asm1/ASM2/onlinelog/o1_mf_3_ch22dpy1_.log' to '+NEWDATA/asmnew/redo04.log';
Database altered.
SQL> alter database rename file '/u01/asm1/ASM2/onlinelog/o1_mf_2_ch22ddxp_.log' to '+NEWDATA/asmnew/redo05.log';
Database altered.

SQL> alter database rename file '/u01/asm1/ASM2/onlinelog/o1_mf_1_ch22d59l_.log' to '+NEWDATA/asmnew/redo06.log' ;
Database altered.


SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------
+NEWDATA/asmnew/redo03.log
+NEWDATA/asmnew/redo04.log
+NEWDATA/asmnew/redo02.log
+NEWDATA/asmnew/redo05.log
+NEWDATA/asmnew/redo01.log
+NEWDATA/asmnew/redo06.log

6 rows selected.


SQL> alter database open resetlogs;

Database altered.

SQL> select name from v$database;

NAME
---------
ASM2

 10. Create a New Tablespace : 

SQL> select name from v$tempfile;

NAME
--------------------------------------------------------------------------------
+DATA/asm2/tempfile/temp.285.907165833

SQL> create temporary tablespace temp1 tempfile '+NEWDATA';

Tablespace created.

SQL> alter database default temporary tablespace temp1;

Database altered.

SQL>


How to Drop a Database using RMAN

 The following Steps will Drop your DATABASE using RMAN

SQL> STARTUP MOUNT RESTRICT;

$ rman target /

RMAN> DROP DATABASE INCLUDING BACKUPS NOPROMPT;

I am using RMAN to drop all the database files including the backup which are taken for this database.

SQL> DROP DATABASE;
You can issue the above command, if there is no backup to clean on the disk or tape. This command will drop all the Datafiles; redo log files, control files, spfile


Note: when we specify “NO PROMPT” in the RMAN prompt, Oracle won’t ask for any confirmation before dropping the database & backup.

Monday 21 March 2016

How to Upgrade RMAN Catalog in 11gR2 after applying a PSU Patch

This post discuss about how to upgrade RMAN catalog in 11gR2 after applying a PSU Patch

Note:  This is not the database upgrade, this post provides information on the RMAN Catalog upgrade for an database after a PSU patch has been applied in the database server.

“The recovery catalog schema version must be greater than or equal to the RMAN client version”.

For your 11.2 database, if you are running RMAN from the same ORACLE_HOME, your RMAN Client is 11.2. The catalog schema must be 11.2. Although it can be in an 11.1 database, it would have been preferable to have it in 11.2. The better course is to upgrade the RMAN Catalog database to the highest of all the target databases and the Catalog schema to the highest RMAN client.

-- Connect to the target database on the server

SQL > ! rman target /
Recovery Manager: Release 11.2.0.2.0 - Production on Thu Jan 12 15:03:42 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
connected to target database: MYDB (DBID=5490950350)

-- Connect to the Recover catalog database

RMAN> connect catalog rman/rman@RMAN_CATALOG
connected to recovery catalog database

-- upgrade the catalog by connecting to the target database and catalog database.

RMAN> UPGRADE CATALOG;
recovery catalog owner is RMAN
enter UPGRADE CATALOG command again to confirm catalog upgrade


RMAN> UPGRADE CATALOG;
recovery catalog upgraded to version 11.02.00.02
DBMS_RCVMAN package upgraded to version 11.02.00.02
DBMS_RCVCAT package upgraded to version 11.02.00.02

RMAN>

TO DETERMINE THE CURRENT RELEASE OF THE CATALOG SCHEMA, YOU MUST RUN A SQL QUERY.


$ sqlplus rman/rman@RMAN_CATALOG

SQL*Plus: Release 11.2.0.2.0 Production on Thu Jan 12 15:19:51 2012

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


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

SQL> SELECT * FROM rcver;

VERSION
------------
11.02.00.02


Note:
1) For releases 11.2 and later, the last two digits in the ‘rcver’ output indicate patch level. For earlier releases, they are always zeros.


2)  In above Query, If multiple versions are listed, then the last row is the current version, and the rows before it are prior versions.

Sunday 20 March 2016

Oracle Data Guard (Standby DB using DUPLICATE command)

Configuring standby database from primary database (Active Database)


Primary Database = rman
Standby Database = rman2

Important  Parameter
For primary db:
1. db_name = rman (use same name for all standby databases)
2. db_unique_name = rman (a unique db name to uniquely identify primary and standby db because db_name is same)       
3 *.log_archive_dest_1='LOCATION=/u01/app/oracle/oradata/rman' (destination for local archive)
4. *.log_archive_dest_2='service=rman2   ( destination for standby database) 
5.  Standby_file_management= auto (it automatically create file at standby db site)

Note : STANDBY_FILE_MANAGEMENT is set to AUTO so that when Oracle files are added or dropped from the primary database, these changes are made to the standby databases automatically. The STANDBY_FILE_MANAGEMENT is only applicable to the physical standby databases.

For Standby db:
initrman2.ora                                                                                        //this is my ora file for standby
*.db_name='rman'
*.db_unique_name='rman2'
*.db_file_name_convert='/u01/app/oracle/oradata/rman','/u01/app/oracle/oradata/rman2'
   //specify the path name and datafile location of primary database
    datafile followed by standby location

*.log_file_name_convert='/u01/app/oracle/oradata/rman','/u01/app/oracle/oradata/rman2'
//specify the path name and redo logfile location of primary
    database redo logfile followed by standby location

*.log_archive_dest_1='LOCATION=/u01/app/oracle/oradata/rman2'
//location of archives coming from primary   database

*.fal_server=rman  //the value is tns service name, to fetch archived log from primary site when log gap is detected at standby site

*.fal_client=rman2   //the value is tns service name to copies missing archive to standby
   database.

 
               Steps for configuring standby database

Both side create listener and  Tns
Both side create password file with same password
Both side db_name must be same

At standby database side
Configure listerner on standby side and start the listener

start the standby database in nomount state
SQL> startup nomount
ORACLE instance started.

Total System Global Area  313860096 bytes
Fixed Size                               1336232 bytes
Variable Size                       197135448 bytes
Database Buffers               109051904 bytes
Redo Buffers                         6336512 bytes


Now on Primary Side ,
Do the Below command , to connect with RMAN

SQL> !rman target sys/oracle auxiliary sys/oracle@rman2

Recovery Manager: Release 11.2.0.1.0 - Production on Sat Mar 19 19:01:13 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

connected to target database: RMAN (DBID=1792754794)
connected to auxiliary database: RMAN (not mounted)


RMAN> duplicate target database for standby from active database;

Starting Duplicate Db at 19-MAR-16
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=18 device type=DISK

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

Starting backup at 19-MAR-16
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=47 device type=DISK
Finished backup at 19-MAR-16

contents of Memory Script:
{
   backup as copy current controlfile for standby auxiliary format  '/u01/app/oracle/product/11.2.0/db_1/dbs/cntrlrman2.dbf';
}
executing Memory Script

Starting backup at 19-MAR-16
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_rman.f tag=TAG20160319T190131 RECID=5 STAMP=906922893
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 19-MAR-16

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
 "/u01/app/oracle/oradata/rman2/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to
 "/u01/app/oracle/oradata/rman2/system01.dbf";
   set newname for datafile  2 to
 "/u01/app/oracle/oradata/rman2/sysaux01.dbf";
   set newname for datafile  3 to
 "/u01/app/oracle/oradata/rman2/undotbs01.dbf";
   set newname for datafile  4 to
 "/u01/app/oracle/oradata/rman2/users01.dbf";
   set newname for datafile  6 to
 "/u01/app/oracle/oradata/rman2test01";
   backup as copy reuse
   datafile  1 auxiliary format
 "/u01/app/oracle/oradata/rman2/system01.dbf"   datafile
 2 auxiliary format
 "/u01/app/oracle/oradata/rman2/sysaux01.dbf"   datafile
 3 auxiliary format
 "/u01/app/oracle/oradata/rman2/undotbs01.dbf"   datafile
 4 auxiliary format
 "/u01/app/oracle/oradata/rman2/users01.dbf"   datafile
 6 auxiliary format
 "/u01/app/oracle/oradata/rman2test01"   ;
   sql 'alter system archive log current';
}
executing Memory Script
executing command: SET NEWNAME

renamed tempfile 1 to /u01/app/oracle/oradata/rman2/temp01.dbf in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting backup at 19-MAR-16
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/app/oracle/oradata/rman/system01.dbf
output file name=/u01/app/oracle/oradata/rman2/system01.dbf tag=TAG20160319T190141
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=/u01/app/oracle/oradata/rman/sysaux01.dbf
output file name=/u01/app/oracle/oradata/rman2/sysaux01.dbf tag=TAG20160319T190141
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/app/oracle/oradata/rman/undotbs01.dbf
output file name=/u01/app/oracle/oradata/rman2/undotbs01.dbf tag=TAG20160319T190141
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00006 name=/u01/app/oracle/oradata/rmantest01
output file name=/u01/app/oracle/oradata/rman2test01 tag=TAG20160319T190141
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/app/oracle/oradata/rman/users01.dbf
output file name=/u01/app/oracle/oradata/rman2/users01.dbf tag=TAG20160319T190141
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 19-MAR-16

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=5 STAMP=906922992 file name=/u01/app/oracle/oradata/rman2/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=6 STAMP=906922992 file name=/u01/app/oracle/oradata/rman2/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=7 STAMP=906922992 file name=/u01/app/oracle/oradata/rman2/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=8 STAMP=906922992 file name=/u01/app/oracle/oradata/rman2/users01.dbf
datafile 6 switched to datafile copy
input datafile copy RECID=9 STAMP=906922992 file name=/u01/app/oracle/oradata/rman2test01
RMAN-05535: WARNING: All redo log files were not defined properly.
RMAN-05535: WARNING: All redo log files were not defined properly.
RMAN-05535: WARNING: All redo log files were not defined properly.
Finished Duplicate Db at 19-MAR-16

Now on Standby Side , do the Below Command
SQL> alter database recover managed standby database disconnect;
// This is used to start MRP (media recovery process) process

Other Basic Commands

How to stop mrp process
SQL> alter database recover managed standby database cancel;

How to check mrp process is running or not
$ps –ef |grep mrp

How to Start the Standby db in read only mode
SQL> alter database recover managed standby database cancel ;  // stops the MRP process
SQL> alter database open ;    // opens in read only mode


Necessary Views, not to Forget :
  1. v$archive_dest              - To check if any error in transferring archives
  2. v$managed_standby    - Current status information for oracle database process
3  v$archived_log       - To check how many archives are applied at standby database                                                Side

Common Error I faced During Setup :
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-04005: error from target database:
ORA-12528: TNS:listener: all appropriate instances are blocking new connections

Solution  :
Add  (UR = A) in TnsNames.ora file , after your Service Name
Example :
..
RMAN2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = oracleasm1.localdomain)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = rman2)
                (UR = A)
    )
  )


Error 2 :
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-04005: error from target database:
ORA-12154: TNS:could not resolve the connect identifier specified

OR

RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-04005: error from target database:
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

Solution :
Check Yout TnsNames.ora  & also Listener.ora File.
Check the opening brackests and Closing Brackets.
Also Check Database SID is Properly specified or Not.



Ooho.. ..  So Big Task. . . Right  ???  :D
 So Guys , Now Lets Learn ROLE - TRANSFER …  How to Perform SWITCHOVER

STEP 1
On Primary Site :

SQL> select database_role , switchover_status from v$database;

DATABASE_ROLE              SWITCHOVER_STATUS
-----------------------          -----------------------------
PRIMARY                             TO STANDBY

Note : Primary DATABASE should be in ‘Open Mode’

SQL> alter database commit to switchover to physical standby;

Database altered.

OR          You can Use Below Query , in case Above Query Gives Error

SQL> alter database commit to switchover to PHYSICAL STANDBY with SESSION SHUTDOWN;

Primary > Shutdown Immediate
//Standby is Still either  ’Mounted’ in redo apply mode or open mode for ‘read only ‘access.

STEP 2
Now, Lets Go  to  Standby Site
You will see Pending Status , see
SQL> select database_role , switchover_status from v$database;

DATABASE_ROLE              SWITCHOVER_STATUS
---------------- --------------------------------------------
PHYSICAL STANDBY         SWITCHOVER PENDING

So now, we will switch Standby site to primary, Lets do it with below command
Standby site :

SQL>  alter database commit to switchover to primary;
 alter database commit to switchover to primary
*
ERROR at line 1:
ORA-16139: media recovery required

Oops , media Recovery is Recovered here..
So I will do

SQL> recover managed standby database finish;



Primary Site - - -  >SQL> startup mount                           //Mount the Primary Site


Done, Now check the Status Using Below Query Standby Site
SQL> select database_role , switchover_status from v$database;

DATABASE_ROLE              SWITCHOVER_STATUS
---------------- ----------------------------------------------
PRIMARY                            TO STANDBY



Check on Primary Site - - -
SQL> select database_role , switchover_status from v$database;

DATABASE_ROLE              SWITCHOVER_STATUS
---------------- -----------------------------------------------
PHYSICAL STANDBY         TO PRIMARY



ERROR FACED during SWITCHOVER

SQL>  alter database commit to switchover to primary;
 alter database commit to switchover to primary
*
ERROR at line 1:
ORA-16139: media recovery required


SQL> recover managed standby database finish;
ORA-00283: recovery session canceled due to errors
ORA-01111: name for data file 5 is unknown - rename to correct file
ORA-01110: data file 5: '/u01/app/oracle/product/11.2.0/db_1/dbs/UNNAMED00005'
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01111: name for data file 5 is unknown - rename to correct file
ORA-01110: data file 5: '/u01/app/oracle/product/11.2.0/db_1/dbs/UNNAMED00005'


SQL> alter database create datafile '/u01/app/oracle/product/11.2.0/db_1/dbs/UNNAMED00005' as '/u01/app/oracle/oradata/rman2/demots.dbf';

Database altered.

SQL> recover managed standby database finish;
Media recovery complete.

SQL>  alter database commit to switchover to primary;

Database altered.

SQL>