Thursday, August 25, 2011

DBA 101

Alert log
---------
In BACKGROUND_DUMP_DEST - Name: alert.log

Archived redo logs
--------------------
In ARCHIVELOG mode, generated when Oracle switches online redo logs - LGWR process
Can be up tp 10 destinations - LOG_ARCHIVE_DEST_n

Backup control file
-------------------
Generated by:
alter database backup controlfile to 'file_name';
or
alter database backup controlfile to trace;

Checkpoint
-------------
DB event that causes dirty buffers from memory to be written to disk.

DB Parameter file
-------------------
Has DB and instance configuration information.
spfile.ora - Binary file altered using alter system command
init.ora - text file

Flash Recovery Area
-----------------------
Optional area to store recovery related files like
RMAN backups,
archived redo logs,
online redo logs,
control files

Instance
---------
A unique set of Oracle Processes and memory from a machine that manipulates a given database (db files, control files, online redo log files, archived redo log files, Flashback logs).
An instance is associated with only one database at a given time.

The basic processes that make up an instance are:
PMON
SMON
DBWn
LGWR
CKPT

The basic RAM memory that are associated with an instance are:

SGA(System Global Area - SGA_TARGET and SGA_MAX_SIZE):

DB buffer cache - DB_CACHE_SIZE and DB_nk_CACHE_SIZE
Shared pool - SHARED_POOL_SIZE
Redo log buffer - LOG_BUFFER
Large pool - LARGE_POOL_SIZE
Java pool - JAVA_POOL_SIZE
Streams pool - STREAMS_POOL_SIZ

PGA(Program Global Area - PGA_AGGREGATE_TARGET):

---------

Online redo logs
------------------
Min - 2 online redo logs
log switch occurs when the current redo log file fills up or can be forced (by a DBA/privileged user) using SQL>alter system switch logfile; .
Each online redo log has a unique log sequence number - This is also associated with its
corresponding Archived redo log file.
Log sequence number of a log file is entered in V$LOG view.
Log sequence number associated with an Archived Redo log is entered in V$ARCHIVED_LOG view.

An online Redo log has a range of DB System Change Numbers (SCNs).

System Change Number (SCN)
--------------------------------
A number (a counter) to represent the state of a DB at a given point in time.

Trace files
------------
Files that has the list of commands/operations executed by the DB or a process or a session or a program etc that can be used for debugging purposes.

RMAN - Summary

$ rman
RMAN> connect target sys/pwd

connected to target database:MCDB01 (DBID=2882204056)
RMAN>

------
Possible error without sysdba privilege

ORA-01031: insufficient privileges

------

To connect to RMAN using a remote connection make the following entry in the tnsnames.ora
(This is not needed to connect locally from) so that it is a dedicated connection.

MCDB01RMAN =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = yemsuresh01.maricorp.com)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MCDB01)
)
)

-----------

When you use RMAN do not use
alter database backup control file to trace;
use
alter database backup control file to 'filename'
e.g.
alter database backup control file to '/u01/oracle/orabackup/orcl1/manual/control.ctl'

-----------

If you have a recovery catalog DB then you connect from RMAN to the target DB and then
to the recovery catalog (so there are 2 connections from RMAN)

$ rman
RMAN> connect target /
RMAN> connect catalog rman/pwd@reccatdb

-----------

For connecting to Auxiliary DB (that is used for Tablespace point-in-time recovery - TSPITR) or for creating standby DB using RMAN)

$ rman
RMAN> connect target /
RMAN> connect auxiliary sys/pwd@auxdb

Configure one of the DB with a password file to connect remotely and the other can DB can be connected locally.

-----------

A simple backup with the proper configurations in place

$ rman
RMAN> connect target /
RMAN> backup database;
RMAN> exit;

-----------

Minimum Parameters to set

log_archive_dest_1='location=/u01/app/oracle/arch/msdb01'
log_archive_min_succeed_dest=1
log_archive_format="%t_%s_%r.arc"
log_archive_dest_state_1='enable'

Other useful parameters

log_checkpoint_timeout
log_checkpoints_to_alert

control_file_record_keep_time (default is 7 - days)
This should be set to a value equal to or greater than db backup retention period.

-----------

Flash Recovery Area for Backups

Files stored in Flash Recovery Area

Archived redo logs
Control File
Control File AutoBackUps
Flashback logs
Redo logs
RMAN data files
RMAN back upset files

-----------

To change the size of the Flash recovery area the parameter DB_RECOVERY_FILE_DEST_SIZE can be changed using

SQL> alter system set db_recovery_file_dest_size=20G scope=both;
SQL> alter system set db_recovery_file_dest='/u01/app/oracle/flash_recovery_area' scope=both;

SQL> alter system set log_archive_dest_10='location=USE_DB_RECOVERY_FILE_DEST';

Not to use FRA

SQL> alter system set db_recovery_file_dest=' ' scope=both;

Using RMAN to increase the size of Flash recovery area:

RMAN>Backup recovery area

Views related to Flash recovery area

DBA_OUTSTANDING_ALERTS
v$RECOVERY_FILE_DEST
v$FLASH_RECOVERY_AREA_USAGE

v$controlfile
v$logfile
v$archived_log
v$datafile_copy
v$datafile
v$backup_piece

IS_RECOVERY_DEST_FILE is a Column with Boolean value in the above 6 views.

-----------

SQL> create user rman_admin identified by pwd default tablespace users;
SQL> grant sysdba to rman_admin;

$ orapwd file=dbapwdfile password=pwd entries=# force=y/n nosysdba=y/n
e.g.
$ orapwd file=dbapwdfile password=oracle1 entries=15 force=n

SQL> alter system set REMOTE_LOGIN_PASSWORD=EXCLUSIVE scope=spfile;
This is a static parameter so cannot be set when DB is running.

11g Summary of Oracle Hot Backup (Online Backup)

Summary of Backup Steps
----------------------------

1. Put the database in hot backup mode.
2. Copy all database datafiles to a backup location.
3. Take the database out of hot backup mode.
4. Force a log switch.
5. Backup the archived redo logs.

sqlplus / as sysdba

SQL> alter database begin backup;
SQL> host cp /u01/app/oracle/oradata/orcl1/*.dbf /u01/oracle/orabackup/orcl1/hot/*.*
SQL> alter database end backup;
SQL> alter system switch logfile;
optional -> SQL> alter system archive log all;
SQL > host cp /u01/oracle/arch/orcl1/* /u01/oracle/orabackup/orcl1/hot/*
SQL> alter database backup controlfile to trace;
SQL> alter database backup controlfile to '/u01/oracle/orabackup/orcl1/hot/control1.bak';

11g Oracle Point-in-time recovery (Time based)

Point-in-time recovery (Time based)
----------------------------------------
1.
ORACLE_HOME=/u01/app/oracle/product/11.0.2db_1
export ORACLE_HOME

ORACLE_SID=orcl1
export ORACLE_SID

PATH=$PATH:$ORACLE_HOME/bin

$ sqlplus / as sysdba
or
SQL> connect / as sysdba;
or
SQL> connect sys as sysdba;

SQL> Shutdown immediate;

2.
Restore DB datafiles from backup location to the location where DB files are.

$ cd /u01/oracle/orabackup/orcl1/hot/
$ pwd
$ cp *.dbf /u01/app/oracle/oradata/orcl1/*

3. sqlplus / as sysdba

SQL> startup mount

SQL> alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss';

4.
SQL> recover database until time 'yyyy-mm-dd:hh:mm:ss';

e.g.
SQL> recover database until time '2011-08-26:19:18:12';
or
SQL> recover database until time '08/26/2011 19:18:12';
(based on nls_date_format)

Enter auto when prompted for archived redo log to apply

5.
alter database open resetlogs;

Note: For this to work you should have all of your datafile backup and archived redo log
upto the point-in-time specified for the recovery and the correct control file.

Tuesday, July 12, 2011

Creating a Physical Standby using RMAN/Data Guard Broker on 11g

In this example, I am creating a Physical Standby database - on the same host - using RMAN to create the standby and the Data Guard Broker to manage the Standby's configuration.

The main impetus for doing this is to evaluate some enhancements that
are part of 11g. In particular, I want to test out the new Data Guard functionality that allows for simultaneous application of changes to the Physical Standby and processing of clients' read-only queries. As you are probably aware, these activities used to be mutually exclusive. In the past, issuing queries to the Standby - ostensibly to offload production load - required stopping the application of redo and opening the database in a read-only mode.

I start by creating the Standby using RMAN. In this case, I will be creating the Standby from an RMAN backup, instead of from the live database.

Preparing the primary database:

Ensure that the database in question is in force logging mode. If not, enable force logging as in below.

SQL> select force_logging from v$database;

FOR
---
NO

SQL> alter database force logging;

Database altered.

Preparing the auxiliary instance:

Add an entry to /etc/oratab

$ grep stdby /etc/oratab
stdby:/u03/app/oracle/product/db/11.1.0.6:N

Create a passwordfile under $ORACLE_HOME/dbs. Remember that the value of the password needs to be the same as that of the primary database's password file. I have found that the ignorecase parameter is essential in 11g when putting one of these configurations together.

$ orapwd file=orapwstdby password=password ignorecase=y entries=25

Add a static listener entry to $TNS_ADMIN/listener.ora (and reload) as well as a net service entry to $TNS_ADMIN/tnsnames.ora

listener entry

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = stdby.colestock.test)
      (ORACLE_HOME = /u03/app/oracle/product/db/11.1.0.6)
      (SID_NAME = stdby)
    )
  )

tnsnames entry

STDBY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac2.colestock.test)(PORT = 1523))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = stdby.colestock.test)
    )
  )

Create a barebones init.ora file for the auxiliary instance

initstdby.ora

db_name=stdby
control_files=(/u03/app/oracle/oradata/stdby/control01.ctl,/u03/app/oracle/orad
ata/stdby/control02.ctl)
log_file_name_convert=('/nf/','/stdby/')

Issue the commands to create the directories needed for the new database:

$ mkdir /u03/app/oracle/oradata/stdby
$ mkdir -p /u03/app/oracle/oradata/stdby/arch
$ mkdir -p /u03/app/oracle/admin/stdby/adump
$ mkdir -p /u03/app/oracle/admin/stdby/bdump
$ mkdir -p /u03/app/oracle/admin/stdby/cdump
$ mkdir -p /u03/app/oracle/admin/stdby/dpdump
$ mkdir -p /u03/app/oracle/admin/stdby/pfile
$ mkdir -p /u03/app/oracle/admin/stdby/scripts
$ mkdir -p /u03/app/oracle/admin/stdby/udump

Startup the instance in no mount. I perform the optional step of opening up the wallet, just to ensure that the wallet location is set appropriately in sqlnet.ora

$ sqlplus "/ as sysdba"

SQL*Plus: Release 11.1.0.6.0 - Production on Sat Oct 20 14:13:10 2007

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


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

SQL> startup nomount;
ORACLE instance started.

Total System Global Area  150667264 bytes
Fixed Size    1298472 bytes
Variable Size   92278744 bytes
Database Buffers   50331648 bytes
Redo Buffers    6758400 bytes
SQL> alter system set wallet open identified by "password";

System altered.

Create the necessary RMAN duplicate script

$ more create_standby.rman
DUPLICATE TARGET DATABASE
  FOR STANDBY
  DORECOVER
  DB_FILE_NAME_CONVERT=('/nf/','/stdby/')
  SPFILE
    PARAMETER_VALUE_CONVERT '/nf','/stdby'
    SET SERVICE_NAMES 'stdby.colestock.test'
    SET "db_unique_name"="stdby"
    SET log_file_name_convert '/nf/','/stdby/';

Create a wrapper script that calls this script and creates all necessary instance connections

$ more create_standby.bsh
#!/bin/bash

export ORACLE_SID=stdby
. oraenv

rman target=sys/password@nf catalog=rman/password@nf auxiliary=/ @create_standby.rman;

Run the aforementioned script

./create_standby.bsh
The Oracle base for ORACLE_HOME=/u03/app/oracle/product/db/11.1.0.6 is /u03/app/oracle

Recovery Manager: Release 11.1.0.6.0 - Production on Sat Oct 20 14:32:20 2007

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

connected to target database: NF (DBID=2018903642)
connected to recovery catalog database
connected to auxiliary database: STDBY (not mounted)

RMAN> DUPLICATE TARGET DATABASE
2>   FOR STANDBY
3>   DORECOVER
4>   DB_FILE_NAME_CONVERT=('/nf/','/stdby/')
5>   SPFILE
6>     PARAMETER_VALUE_CONVERT '/nf','/stdby'
7>     SET SERVICE_NAMES 'stdby.colestock.test'
8>     SET "db_unique_name"="stdby"
9>     SET log_file_name_convert '/nf/','/stdby/';
10>
Starting Duplicate Db at 20-OCT-07
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=98 device type=DISK

contents of Memory Script:
{
   set until scn  1335853;
   restore clone spfile to  '/u03/app/oracle/product/db/11.1.0.6/dbs/spfilestdby.ora';
   sql clone "alter system set spfile= ''/u03/app/oracle/product/db/11.1.0.6/dbs/spfilestdby.ora''";
}
executing Memory Script

executing command: SET until clause

Starting restore at 20-OCT-07
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring SPFILE
output file name=/u03/app/oracle/product/db/11.1.0.6/dbs/spfilestdby.ora
channel ORA_AUX_DISK_1: reading from backup piece /u03/nf_07iu4roo_1_1.rman
channel ORA_AUX_DISK_1: piece handle=/u03/nf_07iu4roo_1_1.rman tag=TAG20071010T103433
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 20-OCT-07

sql statement: alter system set spfile= ''/u03/app/oracle/product/db/11.1.0.6/dbs/spfilestdby.ora''

contents of Memory Script:
{
   sql clone "alter system set  audit_file_dest =
 ''/u03/app/oracle/admin/stdby/adump'' comment=
 '''' scope=spfile";
   sql clone "alter system set  control_files =
 ''/u03/app/oracle/oradata/stdby/control01.ctl'', ''/u03/app/oracle/oradata/stdby/control02.ctl'', ''/u03/app/oracle/oradata/stdby/control03.ctl'' comment=
 '''' scope=spfile";
   sql clone "alter system set  log_archive_dest_1 =
 ''LOCATION=/u03/app/oracle/oradata/stdby/arch'' comment=
 '''' scope=spfile";
   sql clone "alter system set  SERVICE_NAMES =
 ''stdby.colestock.test'' comment=
 '''' scope=spfile";
   sql clone "alter system set  db_unique_name =
 ''stdby'' comment=
 '''' scope=spfile";
   sql clone "alter system set  log_file_name_convert =
 ''/nf/'', ''/stdby/'' comment=
 '''' scope=spfile";
   shutdown clone immediate;
   startup clone nomount ;
}
executing Memory Script

sql statement: alter system set  audit_file_dest =  ''/u03/app/oracle/admin/stdby/adump'' comment= '''' scope=spfile

sql statement: alter system set  control_files =  ''/u03/app/oracle/oradata/stdby/control01.ctl'', ''/u03/app/oracle/oradata/stdby/control02.ctl'', ''/u03/app/oracle/oradata/stdby/control03.ctl'' comment= '''' scope=spfile

sql statement: alter system set  log_archive_dest_1 =  ''LOCATION=/u03/app/oracle/oradata/stdby/arch'' comment= '''' scope=spfile

sql statement: alter system set  SERVICE_NAMES =  ''stdby.colestock.test'' comment= '''' scope=spfile

sql statement: alter system set  db_unique_name =  ''stdby'' comment= '''' scope=spfile

sql statement: alter system set  log_file_name_convert =  ''/nf/'', ''/stdby/'' comment= '''' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area     196681728 bytes

Fixed Size                     1298836 bytes
Variable Size                 75501164 bytes
Database Buffers             113246208 bytes
Redo Buffers                   6635520 bytes

contents of Memory Script:
{
   set until scn  1335853;
   restore clone standby controlfile;
   sql clone 'alter database mount standby database';
}
executing Memory Script

executing command: SET until clause

Starting restore at 20-OCT-07
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=153 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u03/nf_07iu4roo_1_1.rman
channel ORA_AUX_DISK_1: piece handle=/u03/nf_07iu4roo_1_1.rman tag=TAG20071010T103433
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:03
output file name=/u03/app/oracle/oradata/stdby/control01.ctl
output file name=/u03/app/oracle/oradata/stdby/control02.ctl
output file name=/u03/app/oracle/oradata/stdby/control03.ctl
Finished restore at 20-OCT-07

sql statement: alter database mount standby database

contents of Memory Script:
{
   set until scn  1335853;
   set newname for tempfile  1 to
 "/u03/app/oracle/oradata/stdby/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to
 "/u03/app/oracle/oradata/stdby/system01.dbf";
   set newname for datafile  2 to
 "/u03/app/oracle/oradata/stdby/sysaux01.dbf";
   set newname for datafile  3 to
 "/u03/app/oracle/oradata/stdby/undotbs01.dbf";
   set newname for datafile  4 to
 "/u03/app/oracle/oradata/stdby/users01.dbf";
   set newname for datafile  5 to
 "/u03/app/oracle/oradata/stdby/flashback_data01.dbf";
   set newname for datafile  6 to
 "/u03/app/oracle/oradata/stdby/ts201.dbf";
   set newname for datafile  7 to
 "/u03/app/oracle/oradata/stdby/ts301.dbf";
   set newname for datafile  8 to
 "/u03/app/oracle/oradata/stdby/ts401.dbf";
   restore
   clone database
   ;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

renamed tempfile 1 to /u03/app/oracle/oradata/stdby/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

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting restore at 20-OCT-07
using channel ORA_AUX_DISK_1

the file name for datafile 7 is missing in the control file
the file name for datafile 8 is missing in the control file
channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00001 to /u03/app/oracle/oradata/stdby/system01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00002 to /u03/app/oracle/oradata/stdby/sysaux01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /u03/app/oracle/oradata/stdby/undotbs01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /u03/app/oracle/oradata/stdby/users01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00005 to /u03/app/oracle/oradata/stdby/flashback_data01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00006 to /u03/app/oracle/oradata/stdby/ts201.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u03/nf_06iu4rlp_1_1.rman
channel ORA_AUX_DISK_1: piece handle=/u03/nf_06iu4rlp_1_1.rman tag=TAG20071010T103433
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:45
Finished restore at 20-OCT-07

contents of Memory Script:
{
   switch clone datafile all;
}
executing Memory Script

datafile 1 switched to datafile copy
input datafile copy RECID=7 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=8 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=9 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=10 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=11 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/flashback_data01.dbf
datafile 6 switched to datafile copy
input datafile copy RECID=12 STAMP=636474869 file name=/u03/app/oracle/oradata/stdby/ts201.dbf

contents of Memory Script:
{
   set until scn  1335853;
   recover
   standby
   clone database
    delete archivelog
   ;
}
executing Memory Script

executing command: SET until clause

Starting recover at 20-OCT-07
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 20 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_20_635081437.dbf
archived log for thread 1 with sequence 21 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_21_635081437.dbf
archived log for thread 1 with sequence 22 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_22_635081437.dbf
archived log for thread 1 with sequence 23 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_23_635081437.dbf
archived log for thread 1 with sequence 24 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_24_635081437.dbf
archived log for thread 1 with sequence 25 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_25_635081437.dbf
archived log for thread 1 with sequence 26 is already on disk as file /u03/app/oracle/oradata/nf/arch/nf_1_26_635081437.dbf
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_20_635081437.dbf thread=1 sequence=20
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_21_635081437.dbf thread=1 sequence=21
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_22_635081437.dbf thread=1 sequence=22
creating datafile file number=7 name=/u03/app/oracle/oradata/stdby/ts301.dbf
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_22_635081437.dbf thread=1 sequence=22
creating datafile file number=8 name=/u03/app/oracle/oradata/stdby/ts401.dbf
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_22_635081437.dbf thread=1 sequence=22
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_23_635081437.dbf thread=1 sequence=23
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_24_635081437.dbf thread=1 sequence=24
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_25_635081437.dbf thread=1 sequence=25
archived log file name=/u03/app/oracle/oradata/nf/arch/nf_1_26_635081437.dbf thread=1 sequence=26
media recovery complete, elapsed time: 00:00:30
Finished recover at 20-OCT-07
Finished Duplicate Db at 20-OCT-07

Recovery Manager complete.

Afterwards, you will find that the database has been created and that all of the files are renamed and in the locations intended

$ ls -lart /u03/app/oracle/oradata/stdby
total 2515888
drwxr-xr-x  5 oracle dba      4096 Oct 20 12:55 ..
drwxr-xr-x  2 oracle dba      4096 Oct 20 13:18 arch
drwxr-xr-x  3 oracle dba      4096 Oct 20 14:35 .
-rw-r-----  1 oracle dba  11542528 Oct 20 14:35 users01.dbf
-rw-r-----  1 oracle dba 110108672 Oct 20 14:35 undotbs01.dbf
-rw-r-----  1 oracle dba 104865792 Oct 20 14:35 ts401.dbf
-rw-r-----  1 oracle dba 104865792 Oct 20 14:35 ts301.dbf
-rw-r-----  1 oracle dba 524296192 Oct 20 14:35 ts201.dbf
-rw-r-----  1 oracle dba 744497152 Oct 20 14:35 system01.dbf
-rw-r-----  1 oracle dba 682106880 Oct 20 14:35 sysaux01.dbf
-rw-r-----  1 oracle dba 262152192 Oct 20 14:35 flashback_data01.dbf
-rw-r-----  1 oracle dba   9748480 Oct 20 16:41 control03.ctl
-rw-r-----  1 oracle dba   9748480 Oct 20 16:41 control02.ctl
-rw-r-----  1 oracle dba   9748480 Oct 20 16:41 control01.ctl

An examination of the v$database view should yield 'PHYSICAL STANDBY'

SQL> select database_role from v$database;

DATABASE_ROLE
----------------
PHYSICAL STANDBY

At this point, the basic Duplication routine is complete, now a lot of additional configurations need to be performed in order to have a complete Standby Database configuration

Add explicit entries to the appropriate listener (and reload) for the services that will be part of the Data Guard configuration. These are used because the Data Guard broker needs to be able to stop and start the instances involved regardless of their state.

I appended the static service entries to my listener's sid list:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = stdby.colestock.test)
      (ORACLE_HOME = /u03/app/oracle/product/db/11.1.0.6)
      (SID_NAME = stdby)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = stdby_DGMGRL.colestock.test)
      (ORACLE_HOME = /u03/app/oracle/product/db/11.1.0.6)
      (SID_NAME = stdby)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = nf_DGMGRL.colestock.test)
      (ORACLE_HOME = /u03/app/oracle/product/db/11.1.0.6)
      (SID_NAME = nf)
    )
  )

Now, I alter the appropriate init.ora parameters for both the primary and standby instances

alter_primary.sql

$ more alter_primary.sql
alter system set dg_broker_start=true scope=both;
alter system set dg_broker_config_file1='/u03/app/oracle/product/db/11.1.0.6/db
s/nf_dg1.conf' scope=spfile;
alter system set dg_broker_config_file2='/u03/app/oracle/product/db/11.1.0.6/db
s/nf_dg2.conf' scope=spfile;
alter system set log_archive_format='nf_%t_%s_%r.arc' scope=spfile;
alter system set log_archive_config='DG_CONFIG=(nf,stdby)' scope=BOTH;
alter system set log_archive_dest_2='SERVICE=stdby VALID_FOR=(ONLINE_LOGF
ILES,PRIMARY_ROLE) DB_UNIQUE_NAME=stdby' scope=both;
alter system set log_archive_dest_state_2=enable scope=both;
alter system set log_archive_max_processes=4 scope=both;
alter system set fal_server=stdby scope=spfile;
alter system set fal_client=nf scope=spfile;
alter system set db_file_name_convert='/stdby/','/nf/' scope=spfile;
alter system set log_file_name_convert='/stdby/','/nf/' scope=spfile;
alter system set standby_file_management=auto scope=spfile;
shutdown immediate;
startup;

alter_standby.sql

$ more alter_standby.sql
alter system set dg_broker_start=true scope=both;
alter system set dg_broker_config_file1='/u03/app/oracle/product/db/11.1.0.6/db
s/stdby_dg1.conf' scope=spfile;
alter system set dg_broker_config_file2='/u03/app/oracle/product/db/11.1.0.6/db
s/stdby_dg2.conf' scope=spfile;
alter system set log_archive_format='stdby_%t_%s_%r.arc' scope=spfile;
alter system set log_archive_config='DG_CONFIG=(nf,stdby)' scope=BOTH;
alter system set log_archive_dest_2='SERVICE=nf VALID_FOR=(ONLINE_LOGFILE
S,PRIMARY_ROLE) DB_UNIQUE_NAME=nf' scope=both;
alter system set log_archive_dest_state_2=defer scope=both;
alter system set log_archive_max_processes=4 scope=both;
alter system set fal_server=nf scope=spfile;
alter system set fal_client=stdby scope=spfile;
alter system set db_file_name_convert='/nf/','/stdby/' scope=spfile;
alter system set log_file_name_convert='/nf/','/stdby/' scope=spfile;
alter system set standby_file_management=auto scope=spfile;
shutdown immediate;
startup mount;
alter database recover managed standby database disconnect from session;

Run both scripts after settting the appropriate environment variables

$ export ORACLE_SID=nf
$ . oraenv
$ sqlplus "/ as sysdba" @alter_primary.sql

$ export ORACLE_SID=stdby
$ . oraenv
$ sqlplus "/ as sysdba" @alter_standby.sql

At this point, changes should be propagating to the standby. Verify and troubleshoot as necessary, using the method you prefer.

For example, here is how I verify that my standby is up to date with the primary

SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;

PROCESS   STATUS          THREAD#  SEQUENCE#     BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH      CONNECTED             0          0          0          0
ARCH      CONNECTED             0          0          0          0
ARCH      CONNECTED             0          0          0          0
ARCH      CONNECTED             0          0          0          0
MRP0      WAIT_FOR_LOG          1         38          0          0
RFS       IDLE                  1         38       5406          1
RFS       IDLE                  0          0          0          0
RFS       IDLE                  0          0          0          0
RFS       IDLE                  0          0          0          0

$ export ORACLE_SID=nf
$ sqlplus "/ as sysdba"

SQL*Plus: Release 11.1.0.6.0 - Production on Sat Oct 20 19:04:00 2007

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


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

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u03/app/oracle/oradata/nf/arch
Oldest online log sequence     36
Next log sequence to archive   38
Current log sequence           38

As you can see, the standby database is waiting for log sequence 38 from the primary, which is the primary's current log.

Managing switchover/failover operations is simplified by using the DGMGRL command-line interface. Now that I have everything up and working, I will configure this tool to manage my Data Guard configuration.

$ dgmgrl
DGMGRL> connect sys/password@nf
Connected.
DGMGRL> CREATE CONFIGURATION 'MY_DG' AS PRIMARY DATABASE IS 'nf' CONNECT IDENTIFIER IS 'NF';
Configuration "MY_DG" created with primary database "nf"
DGMGRL> ADD DATABASE 'stdby' AS CONNECT IDENTIFIER IS 'stdby';
Database "stdby" added
DGMGRL> show configuration

Configuration
  Name:                MY_DG
  Enabled:             NO
  Protection Mode:     MaxPerformance
  Databases:
    nf    - Primary database
    stdby - Physical standby database

Fast-Start Failover: DISABLED

Current status for "MY_DG":
DISABLED

DGMGRL>  show database verbose 'nf'

Database
  Name:            nf
  Role:            PRIMARY
  Enabled:         NO
  Intended State:  OFFLINE
  Instance(s):
    nf

  Properties:
    DGConnectIdentifier             = 'NF'
    ObserverConnectIdentifier       = ''
    LogXptMode                      = 'ASYNC'
    DelayMins                       = '0'
    Binding                         = 'OPTIONAL'
    MaxFailure                      = '0'
    MaxConnections                  = '1'
    ReopenSecs                      = '300'
    NetTimeout                      = '30'
    RedoCompression                 = 'DISABLE'
    LogShipping                     = 'ON'
    PreferredApplyInstance          = ''
    ApplyInstanceTimeout            = '0'
    ArchiveLagTarget                = '0'
    LogArchiveMaxProcesses          = '4'
    LogArchiveMinSucceedDest        = '1'
    FastStartFailoverTarget         = ''
    StatusReport                    = '(monitor)'
    InconsistentProperties          = '(monitor)'
    InconsistentLogXptProps         = '(monitor)'
    SendQEntries                    = '(monitor)'
    LogXptStatus                    = '(monitor)'
    RecvQEntries                    = '(monitor)'
    HostName                        = 'rac2.colestock.test'
    SidName                         = 'nf'
    StandbyArchiveLocation          = '/u03/app/oracle/oradata/nf/arch'
    AlternateLocation               = ''
    LogArchiveTrace                 = '0'
    LogArchiveFormat                = 'nf_%t_%s_%r.arc'
    LatestLog                       = '(monitor)'
    TopWaitEvents                   = '(monitor)'

Current status for "nf":
DISABLED

DGMGRL> show database verbose 'stdby';

Database
  Name:            stdby
  Role:            PHYSICAL STANDBY
  Enabled:         NO
  Intended State:  OFFLINE
  Instance(s):
    stdby

  Properties:
    DGConnectIdentifier             = 'stdby'
    ObserverConnectIdentifier       = ''
    LogXptMode                      = 'ASYNC'
    DelayMins                       = '0'
    Binding                         = 'OPTIONAL'
    MaxFailure                      = '0'
    MaxConnections                  = '1'
    ReopenSecs                      = '300'
    NetTimeout                      = '30'
    RedoCompression                 = 'DISABLE'
    LogShipping                     = 'ON'
    PreferredApplyInstance          = ''
    ApplyInstanceTimeout            = '0'
    ApplyParallel                   = 'AUTO'
    StandbyFileManagement           = 'AUTO'
    ArchiveLagTarget                = '0'
    LogArchiveMaxProcesses          = '4'
    LogArchiveMinSucceedDest        = '1'
    DbFileNameConvert               = '/nf/, /stdby/'
    LogFileNameConvert              = '/nf/, /stdby/'
    FastStartFailoverTarget         = ''
    StatusReport                    = '(monitor)'
    InconsistentProperties          = '(monitor)'
    InconsistentLogXptProps         = '(monitor)'
    SendQEntries                    = '(monitor)'
    LogXptStatus                    = '(monitor)'
    RecvQEntries                    = '(monitor)'
    HostName                        = 'rac2.colestock.test'
    SidName                         = 'stdby'
    StandbyArchiveLocation          = '/u03/app/oracle/oradata/stdby/arch'
    AlternateLocation               = ''
    LogArchiveTrace                 = '0'
    LogArchiveFormat                = 'stdby_%t_%s_%r.arc'
    LatestLog                       = '(monitor)'
    TopWaitEvents                   = '(monitor)'

Current status for "stdby":
DISABLED

The configuration then needs to be enabled

DGMGRL> enable configuration;
Enabled.
DGMGRL> show database 'nf'

Database
  Name:            nf
  Role:            PRIMARY
  Enabled:         YES
  Intended State:  TRANSPORT-ON
  Instance(s):
    nf

Current status for "nf":
Warning: ORA-16789: standby redo logs not configured

As you can see, the 'Maximum Performance' protection mode now absolutely requires Standby redo logs.

Create the standby redo logs. Remember one more group (in quantity) then the online redo logs. Notice that I only have one log member per group - this is a test instance (I would never do this in a production environment)

SQL> recover managed standby database cancel
Media recovery complete.
SQL> alter database add standby logfile group 5 '/u03/app/oracle/oradata/stdby/redo05.log' size 50M;

Database altered.
SQL> alter database add standby logfile group 6 '/u03/app/oracle/oradata/stdby/redo06.log' size 50M;

Database altered.

SQL> alter database add standby logfile group 7 '/u03/app/oracle/oradata/stdby/redo07.log' size 50M;

Database altered.

SQL> alter database add standby logfile group 8 '/u03/app/oracle/oradata/stdby/redo08.log' size 50M;

Database altered.
SQL> alter database recover managed standby database disconnect from session;

Database altered.

I also create these on the Primary via

alter database add standby logfile group 5 '/u03/app/oracle/oradata/nf/redo05.log' size 50M;
alter database add standby logfile group 6 '/u03/app/oracle/oradata/nf/redo06.log' size 50M;
alter database add standby logfile group 7 '/u03/app/oracle/oradata/nf/redo07.log' size 50M;
alter database add standby logfile group 8 '/u03/app/oracle/oradata/nf/redo08.log' size 50M;

After adding the Standby Redo logs, you will receive a ORA-16826 from the Data Guard Broker.

Warning: ORA-16826: apply service state is inconsistent with the DelayMins property

Remedy via

DGMGRL> remove database 'stdby'
Removed database "stdby" from the configuration
DGMGRL> ADD DATABASE 'stdby' AS CONNECT IDENTIFIER IS 'stdby';
Database "stdby" added
DGMGRL> show database verbose 'stdby';

Database
  Name:            stdby
  Role:            PHYSICAL STANDBY
  Enabled:         NO
  Intended State:  OFFLINE
  Instance(s):
    stdby

  Properties:
    DGConnectIdentifier             = 'stdby'
    ObserverConnectIdentifier       = ''
    LogXptMode                      = 'ASYNC'
    DelayMins                       = '0'
    Binding                         = 'OPTIONAL'
    MaxFailure                      = '0'
    MaxConnections                  = '1'
    ReopenSecs                      = '300'
    NetTimeout                      = '30'
    RedoCompression                 = 'DISABLE'
    LogShipping                     = 'ON'
    PreferredApplyInstance          = ''
    ApplyInstanceTimeout            = '0'
    ApplyParallel                   = 'AUTO'
    StandbyFileManagement           = 'AUTO'
    ArchiveLagTarget                = '0'
    LogArchiveMaxProcesses          = '4'
    LogArchiveMinSucceedDest        = '1'
    DbFileNameConvert               = '/nf/, /stdby/'
    LogFileNameConvert              = '/nf/, /stdby/'
    FastStartFailoverTarget         = ''
    StatusReport                    = '(monitor)'
    InconsistentProperties          = '(monitor)'
    InconsistentLogXptProps         = '(monitor)'
    SendQEntries                    = '(monitor)'
    LogXptStatus                    = '(monitor)'
    RecvQEntries                    = '(monitor)'
    HostName                        = 'rac2.colestock.test'
    SidName                         = 'stdby'
    StandbyArchiveLocation          = '/u03/app/oracle/oradata/stdby/arch'
    AlternateLocation               = ''
    LogArchiveTrace                 = '0'
    LogArchiveFormat                = 'stdby_%t_%s_%r.arc'
    LatestLog                       = '(monitor)'
    TopWaitEvents                   = '(monitor)'

Current status for "stdby":
DISABLED

DGMGRL> enable database 'stdby'
Enabled.
DGMGRL>  show configuration

Configuration
  Name:                MY_DG
  Enabled:             YES
  Protection Mode:     MaxPerformance
  Databases:
    nf    - Primary database
    stdby - Physical standby database

Fast-Start Failover: DISABLED

Current status for "MY_DG":
SUCCESS

DGMGRL> show database 'nf' StatusReport
STATUS REPORT
       INSTANCE_NAME   SEVERITY ERROR_TEXT
DGMGRL>  show database 'stdby' StatusReport
STATUS REPORT
       INSTANCE_NAME   SEVERITY ERROR_TEXT

Now we have a fully functioning configuration for our 11g new features testing

Wednesday, July 6, 2011

Unix for the DBA

How to kill all similar processes with single command (in this case opmn)

ps -ef | grep opmn |grep -v grep | awk ‘{print $2}’ |xargs -i kill -9 {}
Locating Files under a particular directory
find . -print |grep -i test.sql
 Using AWK in UNIX
To remove a specific column of output from a UNIX command – for example to determine the UNIX process Ids for all Oracle processes on server (second column)
ps -ef |grep -i oracle |awk '{ print $2 }'
Changing the standard prompt for Oracle Users
Edit the .profile for the oracle user
PS1="`hostname`*$ORACLE_SID:$PWD>"
 Display top 10 CPU consumers using the ps command
/usr/ucb/ps auxgw | head -11
 Show number of active Oracle dedicated connection users for a particular ORACLE_SID
ps -ef | grep $ORACLE_SID|grep -v grep|grep -v ora_|wc -l
 Display the number of CPU’s in Solaris
psrinfo -v | grep "Status of processor"|wc -l
Display the number of CPU’s in AIX
lsdev -C | grep Process|wc -l
Display RAM Memory size on Solaris
prtconf |grep -i mem
Display RAM memory size on AIX
First determine name of memory device
lsdev -C |grep mem
then assuming the name of the memory device is ‘mem0’
lsattr -El mem0
Swap space allocation and usage
Solaris : swap -s or swap -l
Aix : lsps -a
 Total number of semaphores held by all instances on server
ipcs -as | awk '{sum += $9} END {print sum}'
View allocated RAM memory segments
ipcs -pmb
Manually deallocate shared memeory segments
ipcrm -m '<ID>'
 Show mount points for a disk in AIX
lspv -l hdisk13
 Display amount of occupied space (in KB) for a file or collection of files in a directory or sub-directory
du -ks * | sort -n| tail
Display total file space in a directory
du -ks .
 Cleanup any unwanted trace files more than seven days old
find . *.trc -mtime +7 -exec rm {} \;
 Locate Oracle files that contain certain strings
find . -print | xargs grep rollback
 Locate recently created UNIX files (in the past one day)
find . -mtime -1 -print
 Finding large files on the server (more than 100MB in size)
find . -size +102400 -print
Crontab :
To submit a task every Tuesday (day 2) at 2:45PM
45 14 2 * * /opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1
To submit a task to run every 15 minutes on weekdays (days 1-5)
15,30,45 * 1-5 * * /opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1
To submit a task to run every hour at 15 minutes past the hour on weekends (days 6 and 0)
15 * 0,6 * * opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1

Creating a Physical Standby Database (10g)


SQL> ALTER DATABASE FORCE LOGGING;



To learn how to put a database in archive log click here.
 
Method
Initialization Parameter
Host
Example
1
LOG_ARCHIVE_DEST_n
where:
n is an integer from 1 to 10
Local or remote
LOG_ARCHIVE_DEST_1 = 'LOCATION=/disk1/arc'
LOG_ARCHIVE_DEST_2 = 'SERVICE=standby1'
2
LOG_ARCHIVE_DEST and
LOG_ARCHIVE_DUPLEX_DEST          (a second location for the local archivelog destination)
Local only
LOG_ARCHIVE_DEST = '/disk1/arc'
LOG_ARCHIVE_DUPLEX_DEST = '/disk2/arc'

One LOG_ARCHIVE_DEST_n must be set to send the archive logs using Oracle Net to the StandBy database (ex: LOG_ARCHIVE_DEST_2 = 'SERVICE=standby1'). In this case standby1 must point to a standby database (standby1 = alias in tnsnames.ora). 

Set LOG_ARCHIVE_FORMAT (not mandatory):
LOG_ARCHIVE_FORMAT = arch_%t_%s_%r.arc

t = thread
s = sequence
r = resetlog



SQL> SELECT NAME FROM V$DATAFILE;

NOTE: The log file are created on the standby database when  ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT will run for the first time. 



SQL> startup mount;        (the data files and the newly created standby control file must have the same SCN )
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/u01/oracle/oradata/sb_controlfile.ctl';
SQL> shutdown;

NOTES:
1)  The sb_controlfile.ctl file is a binary file which is used to start the standby database;
2)  The control file says if the database is in primary or standby mode;
3)  If the database is in standby mode, the logs can be received (MRP - Managed Recovery Process apply the logs;
     this process could be started or not).
        


Copy the data files, password file and sb_control.ctl file to the standby database server. sb_control.ctl file must be copied and renamed to have sb_control01.ctl ... sb_control02.ctl at the correct location.

NOTE: The log files and the temp files are not copied. The log file are created on the standby database when  ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT will run for the first time. The TEMP tablespace will be created without a temp file. This file must be added one time the database will be open (in a physical standby mode will be OPEN READ ONLY always).

 
6. Modify init.ora (the pfile) on the standby database

If the control files, udump, bdump directories, etc are different on the standby database, the pfile must be modified.  


The database could be open in mount state to specify the correct location of the data files if the location on the standby database is different from the location on the primary database;



SQL> startup;     (connected as sys)

Now we can connect and read the data in the standby database. However no DML or DDL are allowed.



SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT;

Now the redo logs are applied. However no connections to the standby database are allowed. This command bring the database in mount state automatically (if is not already).

Now alter database OPEN READ ONLY; is not allowed. 

To open the database in READ ONLY (in READ WRITE is not possible if the database is a standby DB) we have to stop the MRP process: 

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;



SQL> ALTER TABLESPACE TEMP ADD TEMPFILE 'C:\ORACLE\DATA\TEMP01.TMP'
SIZE 200M AUTOEXTEND ON MAXSIZE 400M;