Showing posts with label Oracle Database 11g Release 2. Show all posts
Showing posts with label Oracle Database 11g Release 2. Show all posts

09 December 2016

Shell script to send multiple SQL queries output in HTML format

Shell script to send multiple SQL queries output in HTML format

shell script content
***********************
#!/usr/bin/ksh
export ORACLE_BASE=/optware/oracle
ORACLE_HOME=/optware/oracle/11.2.0.4/db_1
export ORACLE_HOME
ORACLE_SID=PROD3
export ORACLE_SID
PATH=$PATH:$ORACLE_HOME/bin
export PATH
ORAENV_ASK=NO

sqlplus /nolog <connect sys/password as sysdba

set markup HTML ON HEAD " -
DB Sync Status Report" -
TABLE "border='1' width='90%' align='center'" -
ENTMAP ON SPOOL ON

set pages 10000
spool /home/oracle/scripts/sync_status_detailed.html
@/home/oracle/scripts/dg_check.sql
spool off
exit
EOF

echo "Please find the attached detailed report, showing data about the DB Sync Status." | mutt -a "/home/oracle/scripts/sync_status_detailed.html" -s "DB Sync Status" abc@xyz.com
rm /home/oracle/scripts/sync_status_detailed.html
***********************


dg_check.sql contents
***********************
connect system/password@PROD3_P

col DATABASE_ROLE for a20
col host_name for a25
set pages 1000
SET NEWPAGE NONE
set lines 180
prompt
prompt ++++++++++++++++++++++++++++++++++++
prompt + Primary Database details
prompt ++++++++++++++++++++++++++++++++++++
select name "DB Name",host_name "Host Name",database_role "Database Role" from v$database,v$instance;
prompt

prompt
prompt ****************Last Sequence generated in Primary****************
SELECT THREAD# "Thread",max(SEQUENCE#)as  "Last Sequence Generated"
FROM GV$ARCHIVED_LOG group by THREAD# ORDER BY 1
/

conn / as sysdba
prompt
prompt
set lines 180
set pages 130
SET NEWPAGE NONE
col DATABASE_ROLE for a20
col host_name for a35
col status for a15
prompt ++++++++++++++++++++++++++++++++++++
prompt + Standby Database details
prompt ++++++++++++++++++++++++++++++++++++
select name "DB Name",host_name "Host Name",database_role "Database Role" from v$database,v$instance;
prompt
Prompt *************Last Sequence received/applied/difference in Standby**********
SELECT distinct ARCH.THREAD# "Thread", ARCH.SEQUENCE# "Last Sequence Received", APPL.SEQUENCE# "Last Sequence Applied", (ARCH.SEQUENCE# - APPL.SEQUENCE#) "Difference"
FROM
(SELECT THREAD# ,max(SEQUENCE#) as SEQUENCE# FROM V$ARCHIVED_LOG WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$ARCHIVED_LOG GROUP BY THREAD#) group by THREAD#) ARCH,
(SELECT THREAD# ,max(SEQUENCE#) as SEQUENCE# FROM V$LOG_HISTORY WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$LOG_HISTORY GROUP BY THREAD#) group by THREAD#) APPL
WHERE
ARCH.THREAD# = APPL.THREAD#
ORDER BY 1
/
Prompt *************Last Sequence received/applied time in Standby**********
set pages 0
SET NEWPAGE NONE
select 'Last Sequence applied Time: ' Logs, to_char(next_time,'DD-MON-YY HH:MI:SS AM') Time    from v$archived_log
where sequence# = (select max(sequence#) from v$archived_log where applied='YES')
union
select 'Last Sequence received Time: ' Logs, to_char(next_time,'DD-MON-YY HH:MI:SS AM') Time    from v$archived_log   where sequence# = (select max(sequence#) from v$archived_log) order by 1 desc;

***********************

08 December 2016

Shell script to send SQL query output in HTML format

#!/usr/bin/ksh
export ORACLE_BASE=/optware/oracle
ORACLE_HOME=/optware/oracle/11.2.0.4/db_1
export ORACLE_HOME
ORACLE_SID=PROD3
export ORACLE_SID
PATH=$PATH:$ORACLE_HOME/bin
export PATH
ORAENV_ASK=NO

sqlplus /nolog <    connect sys/xxxxxxx as sysdba
SET MARKUP HTML ON PREFORMAT OFF ENTMAP ON -
HEAD "DB Sync Status Report -
-

-->" -
BODY "TEXT='#FF00Ff'" -
TABLE "WIDTH='90%' BORDER='5'"
@/home/oracle/scripts/sync_status.sql
SPOOL /home/oracle/sync_status.html
/
SPOOL OFF
EXIT;
EOF

echo "Please find the attached report, showing the details on DB Sync Status." | mutt -a "/home/oracle/sync_status.html" -s "DB Sync Status" abc@xyz.com
rm /home/oracle/sync_status.html

28 April 2016

Check, how many times a database has been restarted

Can you find out how many times a database has been restarted since its creation?
Well, the answer is YES!

Oracle has this view dba_hist_database_instance for getting these details:

DBA_HIST_DATABASE_INSTANCE

DBA_HIST_DATABASE_INSTANCE displays the databases and instances in the Workload Repository.
ColumnDatatypeNULLDescription
DBIDNUMBERNOT NULLDatabase ID
INSTANCE_NUMBERNUMBERNOT NULLInstance number
STARTUP_TIMETIMESTAMP(3)NOT NULLStartup time of the instance
PARALLELVARCHAR2(3)NOT NULLIndicates whether the instance is running in an Oracle Real Application Clusters (Oracle RAC) environment (YES) or not (NO)
VERSIONVARCHAR2(17)NOT NULLDatabase version
DB_NAMEVARCHAR2(9) Name of the database
INSTANCE_NAMEVARCHAR2(16) Name of the instance
HOST_NAMEVARCHAR2(64) Name of the host
LAST_ASH_SAMPLE_IDNUMBERNOT NULLLast sample ID for the active session history


SELECT STARTUP_TIME FROM dba_hist_database_instance ORDER BY startup_time DESC;


Hope this was helpful, have an Oracle day!

31 December 2015

Cloning An Existing Oracle11g Release 2 (11.2.0.x) RDBMS Installation Using OUI

Cloning An Existing Oracle11g Release 2 (11.2.0.x) RDBMS Installation Using OUI

The source installation is packed up using the "tar" command:

cd /optware/oracle/11.2.0.4
tar -cvf /tmp/db_1.tar

and then moved to the target area(different server) and unpacked:

cd /optware/oracle/11.2.0.4
tar -xvf db_1.tar

cd $ORACLE_HOME/clone/bin
perl clone.pl ORACLE_HOME="" ORACLE_HOME_NAME="" ORACLE_BASE="" OSDBA_GROUP= OSOPER_GROUP=

In my scenario:

cd $ORACLE_HOME/clone/bin
perl clone.pl ORACLE_HOME="/optware/oracle/11.2.0.4/db_1" ORACLE_HOME_NAME="OraHome1" ORACLE_BASE="/optware/oracle" OSDBA_GROUP=dba OSOPER_GROUP=oper





Run the orainstRoot.sh and root.sh scripts as mentioned and you are good to go!!

Confirm the clone by . oraenv, if it prompts for ORACLE_SID.







References:
Cloning An Existing Oracle11g Release 2 (11.2.0.x) RDBMS Installation Using OUI (Doc ID 1221705.1)

07 November 2015

Oracle Database Upgrade 11.2.0.1 to 11.2.0.4 on Windows 2008 R2 64-bit

Hello friends,

Again, an upgrade, this was done by my colleague and he has documented this changes, credit goes to him. Thanks buddy!

Database Upgrade 11.2.0.1 to 11.2.0.4 on Windows 2008 R2 64-bit

Download Patchset p13390677 01 and 02 files (2.3gb) from metalink
à Install 11.2.0.4 on a different home
Pre-checks :-
àAfter Installing 11.2.0.4 , Go to 11.2.0.1 database and run :
SQL> spool pre_upgrade.log
SQL> @d:/oracle/11.2.0.4/dbhome_2/rdbms/admin/utlu112i.sql
SQL> spool off
àCopy the following Files to New Home 11.2.0.4 :
1.Spfile/Pfile/init.ora
2.orapwd
3.tnsname.ora
4.Listener.ora
àSet The environment to 11.2.0.4
Set ORACLE_HOME=D:/oracle/product/11.2.0.4/dbhome_2
Set ORACLE_SID=PROTECT
à Bring down the database from 11.2.0.1 and start the database in upgrade mode from 11.2.0.4 home by copying the pfile/spfile of 11.2.0.1 database to 11.2.0.4_HOME/dbs path
                --> SC DELETE <dbservice_name>            (dbservice_ame in services.msc)
                --> oradim -new -sid PROTECT -startmode auto
set ORACLE_SID=protect
sql>startup upgrade
sql>spool cat_upgrd.log
@d:/oracle/11.2.0.4/dbhome_1/rdbms/admin/catupgrd.sql
spool off

sql>startup
@d:/oracle/11.2.0.4/dbhome_1/rdbms /admin/utlrp.sql
shutdown immediate;
Post upgrade steps –
$ sqlplus “/as sysdba”
SQL> STARTUP
SQL> @?/rdbms/admin/utlu112s.sql
select comp_name,version,status from dba_registry;
select owner,count(*) from dba_objects where status != ‘VALID’ group by owner;
7àChange the compatibility parameter
Change the compatible parameter and restart the database.
—-SQL> alter system set compatible=’11.2.0.4.0' scope=spfile;
SQL> shutdown immediate;
SQL> startup;
NOTE :-
if OWB component is not upgraded properly, do the following steps...
¦Upgrade the OWB Component to the database version
1.Start SQLPlus and connect with an account having SYSDBA privileges
2.Drop the OWBSYS schema by executing the script:
SQL> @<OH>/owb/UnifiedRepos/clean_owbsys.sql 
3.Re-create the OWBSYS schema with the script:
SQL> spool <a_file_system_path>cat_owb.log

SQL> @<OH>/owb/UnifiedRepos/cat_owb.sql <tablespace_name>

27 October 2015

Switchover and Failover

An Oracle database operates in one of two roles: primary or standby. Data Guard helps you change the role of a database using either a switchover or a failover:

A switchover is a role reversal between the primary database and one of its standby databases. A switchover guarantees no data loss and is typically done for planned maintenance of the primary system. During a switchover, the primary database transitions to a standby role, and the standby database transitions to the primary role.



Switchover
You can switch a database from the primary role to the standby role, as well as from standby to primary. This is known as a database switchover, because the standby database that you specify becomes the primary database, and the original primary database becomes a standby database, with no loss of data.

Whenever possible, you should switch over to a physical standby database:


  • If the switchover transitions a physical standby database to the primary role, then:
  • The original primary database will be switched to a physical standby role.
  • The online redo log files will be continuously archived from the new primary database to all standby databases in the configuration.


The original primary database will be restarted as a part of the switchover operation.

Standby databases not involved in the switchover will continue operating in the state they were in before the switchover occurred and will automatically begin applying redo data received from the new primary database.


  • If the switchover transitions a logical standby database to the primary role, then:
  • The original primary database will be switched to a logical standby role.


Neither the primary database nor the logical standby database needs to be restarted after the switchover completes.

Other logical standby databases in the broker configuration that were not involved in the switchover will remain viable after the switchover. There is no need to restart any databases. All physical and snapshot standby databases will be disabled after a switchover to a logical standby database.

Switchover to a logical standby database is disallowed when the configuration is operating in maximum protection mode.


Failover
A failover is done when the primary database (all instances of an Oracle RAC primary database) fails or has become unreachable and one of the standby databases is transitioned to take over the primary role. Failover should be performed when the primary database cannot be recovered in a timely manner. Failover may or may not result in data loss depending on the protection mode in effect at the time of the failover.



References:
http://docs.oracle.com/cd/B28359_01/server.111/b28295/sofo.htm

21 March 2015

Oracle DBA interview questions

Hi aspiring DBA's, I will be posting the commonly asked interview questions and answers here, be updated regularly. I'm not an expert, do correct me if I'm wrong anywhere, much appreciated, Thank you for reading and learning something today!

Disclaimer: The following questions and solutions are intended for a quick refresh on the interview questions which may or may not be correct 100%. Use them at your own risk.

Non-Technical
Q1. Introduce yourself, brief the previous/current projects.
Q2. Daily activities you do in your current project?
Q3. What is the backup strategy used in your project?

Technical

Q1. If you are given an already exported dmp file with no log details, how do you check the contents of the dmp file?
Solution1:

imp scott/tiger file=/tmp/abc.dmp show=Y

This above command will show the contents inside the dmp file without actually importing it into the database, we can then decide, what all we want to import.


Q2. How do you proceed with the "ORA-01113: file X needs media recovery"?
Solution2:
ORA-01113: file X needs media recovery

Oracle mounts the database, but refuses to open the database with an ORA-01113: file X needs media recovery.

[oracle@primary ~]$ export ORACLE_SID=ORCL
[oracle@primary ~]$ sqlplus '/as sysdba'

SQL*Plus: Release 11.2.0.1.0 Production on Sat Mar 21 14:17:51 2015

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  849530880 bytes
Fixed Size                  1339824 bytes
Variable Size             507514448 bytes
Database Buffers          335544320 bytes
Redo Buffers                5132288 bytes
Database mounted.
Database opened.
SQL> 
ORA-01113: file 1 needs media recovery
ORA-01110: data file 1: '/u01/app/oracle/oradata/ORCL/system01.dbf'

SQL> !oerr ora 01113
01113, 00000, "file %s needs media recovery"
// *Cause:  An attempt was made to online or open a database with a file that
//         is in need of media recovery.
// *Action: First apply media recovery to the file.

Resolution
Startup the database with the mount option
SQL> startup mount
ORACLE instance started.

Total System Global Area  849530880 bytes
Fixed Size                  1339824 bytes
Variable Size             507514448 bytes
Database Buffers          335544320 bytes
Redo Buffers                5132288 bytes
Database mounted.

Find the name of the redo log file which belongs to the active group
SQL> SELECT MEMBER FROM V$LOG G, V$LOGFILE F WHERE G.GROUP# = F.GROUP# AND G.STATUS = 'CURRENT';

MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/redo01.log

Using the backup control file, start cancel based recovery. Oracle will suggest a non-existing archive log, we will ignore this suggestion from Oracle and specify the log file name {with full path} that we get from the above query

SQL> RECOVER DATABASE USING BACKUP CONTROLFILE UNTIL CANCEL
ORA-00279: change 31527825 generated at 03/21/2015 14:24:24 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/flash_recovery_area/ORCL/archivelog/arch1_7.dbf
ORA-00280: change 31527825 for thread 1 is in sequence #4

Specify log: {=suggested | filename | AUTO | CANCEL}
/u01/app/oracle/oradata/ORCL/redo01.log
Log applied.
Media recovery complete.

Now we open the database in RESETLOGS mode. Oracle recommends to reset the online logs after incomplete recovery or recovery with a backup control file
SQL> ALTER DATABASE OPEN RESETLOGS;
Database altered.


Now take a full backup.


Q3. What is the difference between full backup and level 0 backup?
Solution3. Both being similar, but not same, level 0 backup can be used as a parent backup for the level 1 backups, full backups cannot be.

15 February 2015

Data Guard Physical Standby Setup in Oracle Database 11g Release 2


Introduction
Data Guard is an Oracle feature that primarily provides database redundancy. This is done by having a standby (physical copy) database, preferably in another location and on separate disk. This standby database is maintained by applying the changes from the primary database to it. Standby databases can be maintained with either Redo (Physical standby) or SQL (Logical standby). Here, we will be doing a Physical Standby.

Why Use Data Guard?

Each Oracle High Availability tool has its purpose. The reasons we have used Data Guard for the system that prompted this paper, are:
• Full database redundancy
• Fast recovery in the case of a failure
• Ability for clients to automatically reconnect after a failure
• Ability to offload the backup process to another server
• Provides very good Mean Time to Repair
• Not overly complex

Dear friends,


I have a detailed video presentation and the steps involved in the setup as below:

YouTube Links:

https://www.youtube.com/watch?v=UACsUEgvRU4
https://www.youtube.com/watch?v=DztBchYtwlQ

Document and Video Links:

https://drive.google.com/folderview?id=0BxVnIed8wZv1cDhUOE85NVh5OWM&usp=drive_web

configure-dataguard-broker