Thursday 1 May 2014

Setup Oracle on RedHat

Prerequisite-
Oracle11gR2 setup

If your trying on server, you probably have no internet connection. So, to download the packages needed for oracle, you may need to use your .iso file like this

Steps-
     1.      Installing Dependencies
To install dependencies, run
Yum update
yum install binutils-2*
yum install compat-libstdc++-33*
yum install compat-libstdc++-33*i386*
yum install elfutils-libelf-0*
yum install elfutils-libelf-devel-0*
yum install gcc-4*
yum install gcc-c++-4*
yum install glibc-2*
yum install glibc-2*i686*
yum install glibc-common-2*
yum install glibc-devel-2*
yum install glibc-devel-2*i386*
yum install glibc-headers-2*
yum install ksh-20*
yum install libaio-0*
yum install libaio-0*i386*
yum install libaio-devel-0*
yum install libaio-devel-0*i386*
yum install libgcc-4*
yum install libgcc-4*i386*
yum install libstdc++-4*
yum install libstdc++-4*i386*
yum install libstdc++-devel-4*
yum install make-3*
yum install numactl-devel-0*
yum install sysstat-7*
yum install unixODBC-2*
yum install unixODBC-2*i386*
yum install unixODBC-devel-2*
yum install unixODBC-devel-2*i386*

       2.      We need to create some softlinks to start the installation. To do so run the following commands,
sudo ln -sf /bin/bash /bin/sh
sudo ln -s /usr/bin/awk /bin/awk
sudo ln -s /usr/bin/rpm /bin/rpm
sudo ln -s /usr/bin/basename /bin/basename
sudo ln -s /usr/lib/i386-linux-gnu/libpthread_nonshared.a /usr/lib/libpthread_nonshared.a
sudo ln -s /usr/lib/i386-linux-gnu/libc_nonshared.a /usr/lib/libc_nonshared.a
sudo ln -s /lib/i386-linux-gnu/libgcc_s.so.1 /lib/libgcc_s.so.1
sudo ln -s /usr/lib/i386-linux-gnu/libstdc++.so.6 /usr/lib/libstdc++.so.6
       3.      Also we need to add oracle user and oinstall, dba, nobody group. Run following commands,
     /usr/sbin/groupadd oinstall
        /usr/sbin/groupadd dba
        /usr/sbin/groupadd nobody
        /usr/sbin/usermod -g nobody nobody
  /usr/sbin/ useradd -g oinstall -G dba -p password -d /home/oracle -s /bin/bash oracle
sudo mkdir /home/oracle
sudo chown -R oracle:dba /home/oracle
        4.      We need to do changes in start-up script, run following commands as root
mkdir /etc/rc.d
for i in 0 1 2 3 4 5 6 S
do ln -s /etc/rc$i.d /etc/rc.d/rc$i.d
done
        5.      Now we will create the directory for installation, and changes it owner to oracle. Run following commands to do the same
mkdir -p /u01/app/oracle/product/11.2.0/dbhome_1
chown -R oracle:oinstall /u01
chmod -R 0775 /u01
6.      Open /etc/security/limits.conf and add these lines.
oracle           soft    nproc   2047
oracle           hard    nproc   16384
oracle           soft    nofile  1024
oracle           hard    nofile  65536
Where "nproc" is the maximum number of processes available to the user and "nofiles" is the number of open file descriptors.
7.      Open /etc/pam.d/login and add the following line if it is already not there.
session    required     pam_limits.so
8.      Disable secure linux by editing the /etc/selinux/config file, making sure the SELINUX flag is set as follows:
SELINUX=disabled
If you leave SELINUX=enforcing then you may get an error later while starting sqlplus:
sqlplus: error while loading shared libraries:
/usr/lib/oracle/default/client64/lib/libclntsh.so.11.1: cannot restore segment
prot after reloc: Permission denied
9.      Allow the user oracle to use X server
     Vi /etc/gdm/custom.conf
Add entry to the existing categories:
[xdmcp]
Enable=true
 
10.   Now switch to the user oracle.
# su - oracle
$ echo $SHELL
/bin/bash
11.  If the returned shell is bash then open ~/.bash_profile and add these lines:
# Oracle settings
TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
 
# If /tmp doesn't have 1G space free then you can workaround it by
# pointing the variables TMP AND TMPDIR to a location where you have 
# sufficient space.
 
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1; export ORACLE_HOME
ORACLE_SID=novapay; export ORACLE_SID
PATH=$ORACLE_HOME/bin:/usr/sbin:$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
 
if [ $USER = "oracle" ]; then
   if [ $SHELL = "/bin/ksh" ]; then
     ulimit -p 16384
     ulimit -n 65536
   else
     ulimit -u 16384 -n 65536
   fi
fi
 
umask 022
 
 
      12.  Download Oracle 11g R2 database from Oracle Download Center for linux.

I have downloaded both the zip in /home/oracle. We need to extract both the zip to start the installation. To unzip run,
cd /home/oracle
unzip linux_11gR2_database_1of2.zip
unzip linux_11gR2_database_2of2.zip
      13.  Go to the binaries and start the installation as a oracle user,
cd /home/oracle
chmod 777 -R database
cd database
./runInstaller
      14.  Next we go though the installation (I don’t have the “Oracle Support Password” so just skip in here):

In Selection Installation Option, choose Create and configure a database:

In System Class, choose Server Class (we hope to give a full control during installation):

In Node Selection, choose Single instance database installation:

In Select Install Type, choose Advanced install:

In Select Product Languages, I choose to install English language:

In Select Database Edition, I choose Enterprise Edition:

In Specify Installation Location, it should default as below:
Oracle Base: /u02/oradata/oracle
Sofeware Location: /u02/oradata/oracle/product/11.2.0/dbhome_1
      15.  In Create Inventory, it should default as below:
Inventory Directory: /u02/app/oraInventory
oraInventory Group Name: oinstall
      16.  In Select Configure Type, choose General Purpose / Transaction Processing:

(IMPORTANT!) In Specify Database Identifiers, configure as below:
Global database name: orcl.me.com
Oracle Service Identifier (SID): orcl
      17.  In Specify Configuration Options, configure as below:
Memory -> Enable Automatic Memory Management: TRUE
Character sets: Use Unicode (AL32UTF8)
      18.  In Specify Management Options, choose Use Database Control for database management but keep other else empty:

In Specify Database Storage Options, choose File System where:

Specify database file location: /u02/oradata/oracle/oradata

In Specify Recovery Options, choose Do not enable automated backups:

In Specify Schema Passwords, I am a bit lazy so choose Use the same password for all accounts (don’t do this in production server!):

In Privileged Operating System Groups, confirm as below:
Database Administrator (OSDBA) Group: dba
Database Operator (OSOPER) Group: oinstall
       19.   In Preform Prerequisite Checks, just simply ignore all warning message (since we are using Debian but not Oracle officially supported OS):

         20.   Then press Continue and keep going …
         21.     Now following the instructions and run the following commands with root user:
/u01/app/oraInventory/orainstRoot.sh
/u01/app/oracle/product/11.2.0/dbhome_1/root.sh
22.     Confirm with installation progress
23.     In previous screen the installer mentioned that we can access Enterprise Manager Database Control URL
from https://localhost.localdomain:1158/em, so now let’s open the browser and test it with following account:
User Name: SYS
Password: your_secret_password
Connect As: SYSDBA
24.     login as oracle user root@ubs-desktop:~# su – oracle
25.     startup lsnrctl
26oracle@ubs-desktop:~$ lsnrctl start
27.     startup database 
28oracle@ubs-desktop:~$ sqlplus sys as sysdba
SQL> startup;
SQL> alter user scott identified by tiger;
SQL> alter user scott account unlock;
SQL> exit



Running a stored procedure Liferay

Here myreload() is the stored procedure:-

1. my-portlet\src\main\resources\custom-sql\reload_data.xml

<?xml version="1.0" encoding="UTF-8"?>
<custom-sql>
<sql id="com.me.service.persistence.MyFinder.reloadStaging">
<![CDATA[
select myreload();
]]>
</sql>
</custom-sql>

2. Create MyFinderImpl.java

package com.me.service.persistence;

import com.liferay.portal.kernel.dao.orm.Dialect;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;

public class MyFinderImpl extends BasePersistenceImpl implements MyFinder {
public static String RELOAD_STAGING = MyFinder.class.getName()+".reloadStaging";

    public void reloadStaging()
            throws SystemException {
        // open a new hibernate session in normal case when you are opening
        // session for same entity
        Session session = null;
        SQLQuery query = null;
        try {
            session = openSession();
            String sql = CustomSQLUtil.get(RELOAD_STAGING);
            System.out.println("sql: "+sql);
            // create a SQLQuery object
            System.out.println("dialect: "+getDialect());
            query = session.createSQLQuery(sql);
            query.setCacheable(false);

            
            query.addScalar("myreload", Type.TEXT);
           //This is used when you want to pass any value to the query and place "?" in the query where you want the value to be set
           QueryUtil.list(query, getDialect(), -1, -1);
           
        } catch (Exception e) {
            System.out.println("Exception : Finder custom");
            e.printStackTrace();
        } finally {
            closeSession(session);
        }

        // execute the query and return a list from the db
        //return (Object) query.list().get(0);
    }
}
3. Add the following code to MyLocalServiceImpl.java
public void reloadStagingData() throws SystemException {
         TerminalTrackFinderUtil.reloadStaging();
    }
You can run it like any other LocalServiceImpl in your code