Monday, June 27, 2016

Install Oracle 11g release 2 on CEntOS 6.3

Environment


  • Hostname: oel1.localdomain
  • Arch: x86_64
  • OS: CentOS 6.3
  • Oracle: 11.2.0.4
  • RAM: 4G
  • HDD: 500G


Required Linux Package:

  • binutils-2*x86_64*
  • glibc-2*x86_64* nss-softokn-freebl-3*x86_64*
  • glibc-2*i686* nss-softokn-freebl-3*i686*
  • compat-libstdc++-33*x86_64*
  • glibc-common-2*x86_64*
  • glibc-devel-2*x86_64*
  • glibc-devel-2*i686*
  • glibc-headers-2*x86_64*
  • elfutils-libelf-0*x86_64*
  • elfutils-libelf-devel-0*x86_64*
  • gcc-4*x86_64*
  • gcc-c++-4*x86_64*
  • ksh-*x86_64*
  • libaio-0*x86_64*
  • libaio-devel-0*x86_64*
  • libaio-0*i686*
  • libaio-devel-0*i686*
  • libgcc-4*x86_64*
  • libgcc-4*i686*
  • libstdc++-4*x86_64*
  • libstdc++-4*i686*
  • libstdc++-devel-4*x86_64*
  • make-3.81*x86_64*
  • numactl-devel-2*x86_64*
  • sysstat-9*x86_64*
  • compat-libstdc++-33*i686*
  • unixODBC-*x86_84
  • unixODBC-devel-*x86_84

Execute following command to verify the package installation.

rpm -q binutils glibc nss-softokn-freebl compat-libstdc++-33 glibc-common glibc-devel \
glibc-headers elfutils-libelf elfutils-libelf-devel gcc gcc-c++ ksh libaio libaio-devel \
libgcc libstdc++ libstdc++-devel make numactl-devel sysstat unixODBC unixODBC-devel


Add following kernel parameters. Add following lines on the /etc/sysctl.conf

fs.aio-max-nr = 1048576
fs.file-max = 6815744
#kernel.shmall = 2097152
#kernel.shmmax = 1054504960
kernel.shmmni = 4096
# semaphores: semmsl, semmns, semopm, semmni
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_default=262144
net.core.wmem_max=1048586

Execute following command to reflect the above kernel parameters

/sbin/sysctl -p


Add following lines to /etc/security/limits.conf

oracle              soft    nproc   2047
oracle              hard    nproc   16384
oracle              soft    nofile  4096
oracle              hard    nofile  65536
oracle              soft    stack   10240

Add following line /etc/pam.d/login

session    required     pam_limits.so

Disable selinux. Modify /etc/selinux/config

SELINUX=disable

Stop iptables on Linux and disable iptables on startup

/etc/init.d/iptables stop
chkconfig iptables off


Execute following command to create OS groups and users.

groupadd -g 501 oinstall
groupadd -g 502 dba
groupadd -g 503 oper

useradd -u 502 -g oinstall -G dba,oper oracle
passwd oracle

Create required directories for oracle installation.

mkdir -p /u01/app/oracle/product/11.2.0.4/db_1
chown -R oracle:oinstall /u01/app/oracle

Add following environment variables on .bash_profile

ORACLE_HOSTNAME=OEL1.localdomain; export ORACLE_HOSTNAME
ORACLE_UNQNAME=orcl; export ORACLE_UNQNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
DB_HOME=$ORACLE_BASE/product/11.2.0.4/db_1; export DB_HOME
ORACLE_HOME=$DB_HOME; export ORACLE_HOME
ORACLE_SID=orcl; export ORACLE_SID
ORACLE_TERM=xterm; export ORACLE_TERM
BASE_PATH=/usr/sbin:$PATH; export BASE_PATH
PATH=$ORACLE_HOME/bin:$BASE_PATH; export PATH

LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH


On terminal window change directory to source and execute ./runInstaller

oracle@OEL1 database]$ ./runInstaller 
Starting Oracle Universal Installer...

Checking Temp space: must be greater than 120 MB.   Actual 25021 MB    Passed
Checking swap space: must be greater than 150 MB.   Actual 3122 MB    Passed
Checking monitor: must be configured to display at least 256 colors.    Actual 16777216    Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2016-06-26_09-56-43PM. Please wait ...


































Once you execute the runInstaller executable above screen will be appeared. Check out "I wish to receive security updates via My Oracle Support"


Click on Next after checkout the security updates option.

Warning message window will be prompt. Click on Yes to continue.

Click on Skip software updates and Click on Next to continue.


Click on Create and configure a database option and Click on Next to continue. 


Select Server Class if you have sufficient hardware resource as RAM, CPU and click on Next to continue.



Click on Single instance database installation and Click on Next to continue.


Click on Typical install and Click on Next to continue.


Select Oracle base, Software location, Storage Type (File System if you do not have ASM), Database Edition, Global database name and Administrative Password. Click on next to continue.


Prerequisite checks will be performed and if there is any package or configuration error it will prompt for fix. 


Once prerequisite checks is completed it will display the Summary window. Click on Save Response file to save the Summary file for future use.


Select the location to save the response file.


Click on Install to start installation.


Installation in progress #####


Installation in progress ##########



Database installation in progress #####



Once database installation is completed Summary and Password Management window will be displayed. Click on OK to continue.


On terminal window execute the mentioned script as user root.



Finally database installation completion window will be displayed. Click on Close window to complete installation.







Sunday, May 8, 2016

Oracle Virtual Columns

Oracle Virtual Columns appears to be normal table columns but their values are derived from existing column data rather that storing the data on the disc. It was first introduced with the release Oracle Database 11g Release 1. Following is the syntax to create Oracle Virtual Column.

Column [datatype] [GENERATED ALWAYS] as (expression) [Virtual]

  • datatype: Datatype is optional and can be define to make the table datatype.
  • GENERATED ALWAYS: This keyword is optional to make table defination more readable.
  • Expression: Expression should be defined and enclosed by small bracket.
  • VIRTUAL: Virtual is optional keyword that makes table defination more readable.

Following are few restrictions on virtual columns:

  1. Indexes defined against virtual column is same as the functional based index.
  2. Virtual columns can be used on where clause but we can not manipulate them by any DML.
  3. Tables containing virtual column can still be eligible for result caching.
  4. Functions in expression must be deterministic at the time of table creating.
  5. Virtual columns are not supported for IOT, external, object, cluster or temporary tables.

Following are the restrictions on virtual column expression:

  1. It can not refer to another virtual column.
  2. It can only refer to columns on the same table.
  3. If it refers to a deterministic user-defined function, it cannot be used as a partitioning key column.
  4. The output of the expression must be scalar value. It cannot return and Oracle supplied datatype, a user-defined tyoe, or LOB ro LONG RAW.

Creating table with Oracle Virtual Column

CREATE TABLE tbl_advisory_rpt(
owner_name VARCHAR2(32),
tbl_name VARCHAR2(32),
adv_msg VARCHAR2(256),
allocated_space NUMBER AS
(SubStr(adv_msg,InStr(adv_msg,':',1,1)+1,InStr(adv_msg,':',1,2) - InStr(adv_msg,':',1,1) -1 )),
used_space generated always AS
(SubStr(adv_msg,InStr(adv_msg,':',1,3)+1,InStr(adv_msg,':',1,4) - InStr(adv_msg,':',1,3) -1 )) VIRTUAL,
reclaimable_space NUMBER generated always
AS (SubStr(adv_msg,InStr(adv_msg,':',1,5)+1,InStr(adv_msg,':',1,6) - InStr(adv_msg,':',1,5) -1 )) VIRTUAL
);
 

 










Inserting data into the table.

insert into tbl_advisory_rpt(owner_name,tbl_name,adv_msg) values
('SCOTT','EMPLOYEE','Allocated Space:32471289347: Used space:32471200000: Reclaimable Space:89347:');
insert into tbl_advisory_rpt(owner_name,tbl_name,adv_msg) values
('SCOTT','SAL','Allocated Space:4194304: Used space:324712: Reclaimable Space:3869592:');
 













 Selecting data from the table.

select * from tbl_advisory_rpt;

Sunday, April 24, 2016

Extended Data Types Oracle 12c

Till Oracle 11g character data types like varchar2, nvarchar2 supports only 4000 bytes where as RAW supports only 2000 bytes. From Oracle 12c, Oracle has extended these datatypes length to 32Kbytes (32767 bytes). For the extended datatype support we need to configure it.

Enabling the extended datatype support on non-cdb Oracle 12c database.


Connect to non cdb database. In my case orcl is non cdb database.

. oraenv
sqlplus / as sysdba
















Shutdown database and startup on upgrade mode.

shutdown immediate
startup upgrade

















Modify system parameter max_string_size to extended.

alter system set max_string_size=extended;








Execute the sql file that is located under rdbms/admin directory.

@?/rdbms/admin/utl32k.sql













After completing the execution of above sql command startup database in normal mode.

shutdown immediate
startup

















Enabling the extended datatype support on cdb Oracle 12c database. In my case cdb1 is cdb database with 3 pluggable database: pdb1, pdb2, pdb3.

On root container database execute the following commands

. oraenv
sqlplus / as sysdba
























Modify system parameter max_string_size to extended.

alter system set max_string_size=extended scope=spfile;








Shutdown database and startup on upgrade mode.

shutdown immediate
startup upgrade
















Open all pluggable database on upgrade mode.

alter pluggable database all open upgrade;









Execute utl32k.sql for all the pdbs.


Change directory to script file directory i.e. ORACLE_HOME then rdbms/admin. We are using catcon.pl, it can run sql in the root and in specified PDBs in the correct order, and it generates log files that you can view to confirm that the SQL script or SQL statement did not generate unexpected errors. It also starts multiple processes and assigns new scripts to them as they finish running scripts previously assigned to them. 

cd $ORACLE_HOME/rdbms/admin
../../perl/bin/perl catcon.pl -d $ORACLE_HOME/rdbms/admin -b /tmp/utl32k.out utl32k.sql










Login to the cdb, shutdown the database and start database in the normal mode.

sqlplus / as sysdba
shutdown immediate
startup;
alter pluggable database all open;




















Using extended datatypes

Create sequence seq_id;

Create table that use extended datatype.

Create table tbl_memo (
id number default seq_id.nextval,
notes varchar2(32767),
enabled char default 'Y',
primary key(id));

Creating table on non cdb database.













Creating table on cdb root database

It is noticed that we cannot enable extended datatype at cdb root database. Even I have executed "@?/rdbms/admin/utl32k.sql" at cdb root database but there is no luck. Following error has encountered.


ORA-00910: specified length too long for its datatype













Whereas on the pdb database, we can create tables with extended datatypes.






Monday, April 18, 2016

Oracle 12c Monitor Privileges Usage

Over the course of time, database user get more privilege than actually required. This could be security loophole which may result in unauthorised database access. More over excessive privileges violate the basic security principle of least privilege.
In Oracle 12c we have new database package, DBMS_PRIVLEGE_CAPTURE helps us on capture and analyse the privileges usage. On basic of analysis we can determine the privileges that can be revoked for normal operations. This package will generate information of privileges usage between time the capture is enabled and the capture is disabled.


CAPTURE_ADMIN role should be granted to the user for executing package DBMS_PRIVLEGE_CAPTURE.

grant capture_admin to monitoring;











Following are the procedures available on the package.


  • create_capture: Creates a policy that specifies the conditions for analyzing privilege use.
  • enable_capture: Starts the recording of privilege analysis for a specified privilege analysis policy.
  • disable_capture: Stops the recording of privilege use for a specified privilege analysis policy
  • generate_result: Populates the privilege analysis data dictionary views with data
  • drop_capture: Removes a privilege analysis policy together with the data recorded.



Create_capture procedure creates the policy to analyse privilege usage. Privileges can be
analysed on following level.

G_DATABASE : Analyse the privilege usage on database level for all the users except SYS
G_ROLE : Analyse the privilege usage for specific role(s). ROLE_NAME_LIST need to be used for the roles list.
G_CONTEXT : Analyse the privilege whenever the condition specified get matched and returns TRUE.
G_ROLE_AND_CONTEXT : Analyse the privileges whenever the role and the condition get matched and returns TRUE.

Create capture at database level.

begin
DBMS_PRIVILEGE_CAPTURE.create_capture(
name => 'db_capture',
type => DBMS_PRIVILEGE_CAPTURE.g_database
);
end;
/















Create capture at role level.

begin
DBMS_PRIVILEGE_CAPTURE.create_capture(
name => 'rl_capture',
type=> DBMS_PRIVILEGE_CAPTURE.g_role,
roles => role_name_list('CONNECT','RL_DILLI')
);
end;
/













Create capture at context level.

begin
DBMS_PRIVILEGE_CAPTURE.create_capture(
name => 'cont_capture',
type => DBMS_PRIVILEGE_CAPTURE.g_context,
condition => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') = ''DILLI'''
);
end;
/














Create capture on role and context combined.

begin
DBMS_PRIVILEGE_CAPTURE.create_capture(
name => 'rl_cont_capture',
type => DBMS_PRIVILEGE_CAPTURE.g_role_and_context,
roles => role_name_list('RL_DILLI'),
condition => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') IN (''SCOTT'',''DILLI'')'
);
end;
/















View captured lists.

We can view the list of the capture and its details using DBA_PRIV_CAPTURES data dictionary views.
desc DBA_PRIV_CAPTURES;

set linesize 200
col name for a16
col roles for a20
col context for a100

select name,type,enabled,roles,context from dba_priv_captures;













Enable the captures created earlier.

ENABLE_CAPTURE procedure is used to enable capture. Basically it will start capturing. Typically only one capture can be enabled at one time. Exception is that we can enable one database  level capture and another none database level at same time.

begin
DBMS_PRIVILEGE_CAPTURE.enable_capture('cont_capture');
DBMS_PRIVILEGE_CAPTURE.enable_capture('rl_cont_capture');
end;
/














An error will be encountered if we try to enable both none db level capture at once. But first one among the two will be enabled.













We can enable one db level capture and another none db level capture at once.

begin
DBMS_PRIVILEGE_CAPTURE.enable_capture('db_capture');
DBMS_PRIVILEGE_CAPTURE.enable_capture('rl_cont_capture');
end;
/















Disable capture is used to disable capture. Basically it stop capture of the policy specified.


begin
DBMS_PRIVILEGE_CAPTURE.disable_capture('db_capture');
DBMS_PRIVILEGE_CAPTURE.disable_capture('rl_cont_capture');
end;
/












Finally we need to use GENERATE_RESULTS procedure to generate report on basic of the
capture.

begin
DBMS_PRIVILEGE_CAPTURE.generate_result('rl_cont_capture');
DBMS_PRIVILEGE_CAPTURE.generate_result('db_capture');
end;
/












Following data dictionary views are available to get the details on the privilege capture result.


  • DBA_PRIV_CAPTURES
  • DBA_USED_OBJPRIVS
  • DBA_USED_OBJPRIVS_PATH
  • DBA_USED_PRIVS
  • DBA_USED_PUBPRIVS
  • DBA_USED_SYSPRIVS
  • DBA_USED_SYSPRIVS_PATH
  • DBA_USED_USERPRIVS
  • DBA_USED_USERPRIVS_PATH
  • DBA_UNUSED_OBJPRIVS
  • DBA_UNUSED_OBJPRIVS_PATH
  • DBA_UNUSED_PRIVS
  • DBA_UNUSED_SYSPRIVS
  • DBA_UNUSED_SYSPRIVS_PATH
  • DBA_UNUSED_USERPRIVS
  • DBA_UNUSED_USERPRIVS_PATH

Accessing dba_used_privs to view all the privileges used during enable and disable capture.


col capture for a16
col USED_ROLE for a30
col SYS_PRIV for a30
set linesize 200
col username for a30

select capture,username,used_role,sys_priv from dba_used_privs;
























Accessing dba_unused_privs to view all the privileges not used during enable and disable capture.


col capture for a16
col rolename for a30
col SYS_PRIV for a30
set linesize 200
col username for a30

select capture,username,rolename,sys_priv from dba_unused_privs;


















On basic on above data we can revoke unnecessary privileges granted to the user SCOTT.


DROP_CAPTURE is used to drop all the captures created.

begin
DBMS_PRIVILEGE_CAPTURE.drop_capture(name => 'db_capture');
DBMS_PRIVILEGE_CAPTURE.drop_capture(name => 'rl_capture');
DBMS_PRIVILEGE_CAPTURE.drop_capture(name => 'cont_capture');
DBMS_PRIVILEGE_CAPTURE.drop_capture(name => 'rl_cont_capture');
end;
/