Thursday, February 8, 2018

IDENTIFY DATABASE IDLE SESSIONS

sCRIPT FOR IDENTIFY DATABASE IDLE SESSIONS




set linesize 140
col username format a15
col idle format a15
col program format a30

PROMPT Enter the number of minutes for which the sessions should have been idle:
PROMPT

select
sid,username,status,
to_char(logon_time,'dd-mm-yy hh:mi:ss') "LOGON",
floor(last_call_et/3600)||':'||
floor(mod(last_call_et,3600)/60)||':'||
mod(mod(last_call_et,3600),60) "IDLE",
program
from
v$session
where
type='USER'
and (LAST_CALL_ET / 60) > &minutes
order by last_call_et;

How to set IDLE TIME in oracle Database

          How to set IDLE TIME in oracle Database(Step By Step)


1)    connect as sysdba user
                sqlplus “/as sysdba””

               show parameter resource_limit

              NAME                                 TYPE        VALUE
              ———————————— ———– ——————————
              resource_limit                       boolean     FALSE

           2)  Change the resource parameter to true

               alter system set resource_limit=TRUE scope=both;

              System altered.

            3) Check the idle time.

                select * from dba_profiles
               where profile='DEFAULT'
               and resource_name in ('IDLE_TIME','CONNECT_TIME');

            PROFILE                        RESOURCE_NAME                    RESOURCE LIMIT
            —————————— ——————————– ——– —————————————-
             DEFAULT                        IDLE_TIME                        KERNEL   UNLIMITED    
           DEFAULT                        CONNECT_TIME                     KERNEL   UNLIMITED

          4) Change the IDLE time to 60min

              ALTER PROFILE DEFAULT LIMIT IDLE_TIME 60;

             Profile altered.

             5) Again Check the idle time.

         select * from dba_profiles
            where profile=’DEFAULT’
           and resource_name in (‘IDLE_TIME’,’CONNECT_TIME’);      

 
           PROFILE                        RESOURCE_NAME                    RESOURCE LIMIT
            —————————— ——————————– ——–
         DEFAULT                        IDLE_TIME                        KERNEL   60
         DEFAULT                        CONNECT_TIME                     KERNEL   UNLIMITED


Tuesday, February 6, 2018

ORA-00018 maximum number of sessions exceeded

ORA-00018 maximum number of sessions exceeded (Easy Step)
  
ORA-00018 maximum number of sessions exceeded
Cause: All session state objects are in use.
Action: Increase the value of the SESSIONS initialization parameter.


 Where Oracle level may not help?

Though, using ‘Alter system kill ‘ is considered to be preferable option to be at safer side, there are still some scenarios and some valid reasons to go a step ahead – Killing the sessions at OS level

- While we face the ORA-00018 maximum number of sessions exceeded error, it’s possible to kill session using the ‘Alter system’ command only if you already are connected to the database.
If not, database won’t allow the new connection, so there is the way to kill the session at OS level. To find the SPID without logging into database some Quick-slice (Microsoft) & likewise utilities are provided.
- Another reason is releasing the locks. The alter system may not release the locks at times. Though generally it does, it may hold it back till the connected session times out or the user tries to reconnect the session to discover it’s been killed (and the session actually disappears from the session view, from the “KILLED” status)

Immediate Action

At Operating System level –

UNIX – 

Unlike Windows, UNIX is process based. The sessions can be listed down as individual processes by ps command.

Then by using ‘kill -9 ‘command the related process/session can be killed

Syntax – 

 ps –ef | grep LOCAL_NO
We will get pid After that we can kill .

$ Kill -9 spid 

The session will be killed immediately and all resources will be released.


At Oracle Level –

The sessions can be killed from within oracle using the ALTER SYSTEM KILL SESSION command.

Firstly we will check active/inactive Session Status..

Generally We will find more inactive session is idle. we can kill those session..
Below command we can check status of session and machine name from where more sessions are coming.

select count(sid) " Total number of session",USERNAME,STATUS,MACHINE from gv$session where username is not null group by username,status,machine;



Now we are killing session.

select 'alter system kill session ''' ||sid|| ',' || serial#|| ''' immediate;' from v$session where status='INACTIVE' and username is not null;

or 

 we can use use below command to kill session.

Syntax-

ALTER SYSTEM KILL SESSION ‘SID, SERIAL#’
 

This command will kill the session uniquely identified by the specified SID & serial number combo.
Most of the times, we won’t    find the session count decreased even after killing the sessions. Then the newly discovered thing by you will be the killed sessions holding the “KILLED” status.
The status will remain the same until timed out or till the user tries to reconnect the same session.

How to increase SESSION initialization parameter:

1. Login as sysdba
 sqlplus / as sysdba

2. Check Current Setting of Parameters

 sql> show parameter sessions
 sql> show parameter processes
 sql> show parameter transactions

3. If you are planning to increase "sessions" parameter you should also plan to increase "processes and "transactions" parameters.
A basic formula for determining  these parameter values is as follows:

  processes=x
  sessions=x*1.1+5
  transactions=sessions*1.1
 
4. These paramters can't be modified in memory. You have to modify the spfile only (scope=spfile) and bounce the instance.

 sql> alter system set processes=500 scope=spfile;
 sql> alter system set sessions=555 scope=spfile;
 sql> alter system set transactions=610 scope=spfile;
 sql> shutdown abort
 sql> startup 

Rman Basic Command List

Rman Basic Command List (Easy to Understand) 

*SHOW COMMAND**

 1) show all;
 2) show archivelog deletion policy;
 3) show archivelog backup copies;
 4) show auxname;
 5) show backup optimization;
 6) show [auxiliary] channel;
 7) show channel for device type [disk | <media device>;
 8) show controlfile autobackup;
 9) show controlfile autobackup format;
 10) show datafile backup copies;
11) show default device type;
12) show retention policy;
13) show encryption algorithm;
14) show encryption for [database | tablespace];
15) show exclude;
16) show maxsetsize;
17) show retention policy;
18) show snapshot controlfile name;
19) show compression algorithm;
**BACKUP COMMAND** 
1) backup current controlfile;
2) backup tablespace users include current controlfile;
3) backup spfile;
4) backup not backed up since time ‘sysdate-1′ database plus archivelog;
5) backup database force;
6) backup database;
7) backup database plus archivelogs;
8) backup archivelog all;
9) backup datafile 5 tag dbfile_5_bkp;


**CATALOG COMMAND**

1) Add user-managed copies of datafile to RMAN repository

RMAN> catalog datafilecopy ‘/u01/oracle/users.bkp’;

RMAN> catalog datafilecopy ‘/u01/oracle/users.bkp’ level 0;  (To catalog as incremental level 0 backup)
2) Add uncataloged backup piece to RMAN repository

RMAN> catalog backuppiece ‘ertt2lu4_1_1′;

3) To catalog multiple files ( say you copied production backup to target database for database cloning)

RMAN> catalog start with ‘/backups/source_bkp’ noprompt;

The start with clause specifies that RMAN catalog all valid backup sets, datafile copies, and archived redo logs starting with the string pattern you pass.

4) To catalog all files in the flash recovery area

RMAN> catalog recovery area;

**REPORT COMMAND**

1)report need backup;
2) report schema;
 report schema at time ‘sysdate-1′;  (from a past point in time)
3) report obsolete;
 **LIST COMMAND**
1) list backup;
2) list backup by file;
3) list backupset;
4) list copy;
5) list backupset tag ‘full_database_backup’;
6) list backup of database;
7) list incarnation;
8)  list restore point;
9)  list script names;
10) list expired backup;
11) list expired archivelog all;
12) list recoverable backup;
13) list restore point all;

**CROSSCHECK COMMAND**
1) crosscheck backupset;
2) crosscheck copy of database;
3) crosscheck backupset 10, 12;
4) crosscheck backuppiece tag = ‘monthly_backup’;
5) crosscheck controlfilecopy ‘/backups/control01.ctl’;
6) crosscheck backup of datafile “/u01/oracle//system01.dbf” completed after ‘sysdate-7′;
7) crosscheck backup of archivelog all spfile;
8) crosscheck backup;

**DELETE COMMAND**
1) delete backup;
2) delete copy;
3) delete backuppiece 9;
4) delete copy of controlfile like ‘/backups/%’;
5) delete backup tag=’double_bkp_prod’;
6) delete backup of tablespace sysaux device type sbt;
******************
 Note -You can also use force, expired, obsolete keyword with delete commad:

delete force ..: Deletes the specified files whether they actually exist on media or not and removes their records from the RMAN repository as well

delete expired ..: Deletes only those files marked as expired  as per crosscheck command.

delete obsolete ..: Deletes datafile backups and copies and the archived redo logs and log backups that are recorded as obsolete in the RMAN repository
The delete obsolete command relies only on the backup retention policy in force.
******************
7) delete archivelog all;
8) delete archivelog all backed up 2 times to sbt;
9) delete archivelog until sequence = 1234;
10) backup device type sbt archivelog all delete all input;
11) delete script full_disk_db;

If you have two scripts—one local and one global—in the same name, then the delete script command drops the local one, not the global one. If you want to drop the global script, you must use the keyword global in the command, as shown here:

12)delete global script full_disk_db;

**CHANGE COMMAND**
1) change backupset 6 unavailable;
2)   change backupset 6 available;
3) change backup tag ‘initial_db_bkup’ keep forever;
4) change backup tag ‘inital_db_backup’ nokeep;
5) change backupset 12 keep until time ‘sysdate+60′;

**VALIDATE COMMAND**
1) backup validate database archivelog all;
2)  backup validate check logical database archivelog all;
3) validate backupset 5;
4) validate database;
5)  validate recovery area;
6) validate recovery files;
7) validate spfile;
8) validate tablespace <tablespace_name>;
9) validate controlfilecopy <filename>;
10) validate backupset <primary_key>;


Monday, February 5, 2018

User And Their Privilege

  How to Create a User and Grant Permissions in Oracle

       Description

       The CREATE USER statement creates a database account that allows you to log into the                    Oracle database.

        Creating a User


       CREATE USER CAG
        IDENTIFIED BY xyz
         DEFAULT TABLESPACE USERS
         PROFILE DEFAULT
        ACCOUNT UNLOCK;

            NOTE --Parameters or Arguments

1             1)    user_name

The name of the database account that you wish to create
.
2               2)      TABLESPACE

Optional , It is the name of the tablespace that you wish to assign to the users.

3               3)      PROFILE profile_name

Optional. It is the name of the profile that you wish to assign to the user account to limit the amount of database resources assigned to the user account. If you omit this option, the DEFAULT profile is assigned to the user.
4               4)      PASSWORD EXPIRE

Optional. If this option is set, then the password must be reset before the user can log into the Oracle database.
           5) ACCOUNT LOCK

Optional. It disables access to the user account.

            6) ACCOUNT UNLOCK

  Optional. It enables access to the user account

    The Grant Statement


With our new CAG account created, we can now begin adding privileges to the account using the GRANT statement. GRANT is a very powerful statement with many possible options, but the core functionality is to manage the privileges of both users and roles throughout the database.

 Providing Roles

Typically, you’ll first want to assign privileges to the user through attaching the                   account to  various roles, starting with the CONNECT role:       
GRANT CONNECT TO CAG;
In some cases to create a more powerful user, you may also consider adding the RESOURCE role (allowing the user to create named types for custom schemas) or even the DBA role, which allows the user to not only create custom named types but alter and destroy them as well.
GRANT CONNECT, RESOURCE, DBA TO CAG;

Assigning Privileges

Next you’ll want to ensure the user has privileges to actually connect to the database and create a session using GRANT CREATE SESSION. We’ll also combine that with all privileges using GRANT ANY PRIVILEGES.
GRANT CREATE SESSION GRANT ANY PRIVILEGE TO CAG;
We also need to ensure our new user has disk space allocated in the system to actually create or modify tables and data, so we’ll GRANT TABLESPACE like so:
GRANT UNLIMITED TABLESPACE TO CAG;

Table Privileges

While not typically necessary in newer versions of Oracle, some older installations may require that you manually specify the access rights the new user has to a specific schema and database tables.
For example, if we want our CAG user to have the ability to perform SELECTUPDATEINSERT, and DELETE capabilities on the books table, we might execute the following GRANT statement:
GRANT
  SELECT,  INSERT,  UPDATE,  DELETE ON   schema.books
TO
  CAG;
This ensures that CAG can perform the four basic statements for the books table that is part of the schema schema.




How to create user in MY SQL

Create  a new MySQL user Account mysql > CREATE USER ' newuser '@'localhost' IDENTIFIED BY ' password '...