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

Tuesday 18 March 2014

Set up Oracle 11gR2 in Ubuntu 12.04

Prerequisites:-
Download Oracle 11gR2
Steps:-
1.      Installing Dependencies
To install dependencies, run
sudo apt-get update
sudo apt-get install gcc make binutils gawk x11-utils rpm build-essential libaio1 libaio-dev libmotif4 libtool expat alien ksh pdksh unixODBC unixODBC-dev sysstat elfutils libelf-dev binutils lesstif2 lsb-cxx libstdc++5
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,
sudo addgroup oinstall
sudo addgroup dba
sudo addgroup nobody
sudo usermod -g nobody nobody
sudo 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
sudo mkdir -p /u02/oradata/MYDB
sudo chown -R oracle:dba /u01
6.      We also need to modify the kernel parameter, to do so run following commands as root
echo "#">> /etc/sysctl.conf
echo "# Oracle 11gR2 entries">> /etc/sysctl.conf
echo "fs.aio-max-nr=1048576" >> /etc/sysctl.conf
echo "fs.file-max=6815744" >> /etc/sysctl.conf
echo "kernel.shmall=2097152" >> /etc/sysctl.conf
echo "kernel.shmmni=4096" >> /etc/sysctl.conf
echo "kernel.sem=250 32000 100 128" >> /etc/sysctl.conf
echo "net.ipv4.ip_local_port_range=9000 65500" >> /etc/sysctl.conf
echo "net.core.rmem_default=262144" >> /etc/sysctl.conf
echo "net.core.rmem_max=4194304" >> /etc/sysctl.conf
echo "net.core.wmem_default=262144" >> /etc/sysctl.conf
echo "net.core.wmem_max=1048586" >> /etc/sysctl.conf
echo "kernel.shmmax=1073741824" >> /etc/sysctl.conf
7.      Note: kernel.shmmax = max possible value, e.g. size of physical memory in bytes. In my case machine is having 2GB so we are specifying 1GB. Adjust the parameter as per your configuration

Load new kernel parameters, by running following,
sudo sysctl -p
8.      We have to change shell configuration, to do the same run following commands as root user,
cp /etc/security/limits.conf /etc/security/limits.conf.original
echo "#Oracle 11gR2 shell limits:">>/etc/security/limits.conf
echo "oracle soft nproc 2048">>/etc/security/limits.conf
echo "oracle hard nproc 16384">>/etc/security/limits.conf
echo "oracle soft nofile 1024">>/etc/security/limits.conf
echo "oracle hard nofile 65536">>/etc/security/limits.conf
9.      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
10.  Now we will start the installation. You can start the installation from /home/oracle/database.
Login as a oracle user.
su oracle
11.  Go to the binaries and start the installation as a oracle user,
cd /home/oracle
chmod 777 -R database
cd database
./runInstaller -ignoreSysPrereqs
12.  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/MYDB
Sofeware Location: /u02/oradata/MYDB/product/11.2.0/dbhome_1
13.  In Create Inventory, it should default as below:
Inventory Directory: /u02/app/oraInventory
oraInventory Group Name: oinstall
14.  In Select Configure Type, choose General Purpose / Transaction Processing:

(IMPORTANT!) In Specify Database Identifiers, configure as below:
Global database name: mydb.asaptp.com
Oracle Service Identifier (SID): MYDB
15.  In Specify Configuration Options, configure as below:
Memory -> Enable Automatic Memory Management: TRUE
Character sets: Use Unicode (AL32UTF8)
16.  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/MYDB/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
17.   In Preform Prerequisite Checks, just simply ignore all warning message (since we are using Debian but not Oracle officially supported OS):
18.   Special note for Ubuntu 12.04: If you face error during installation so update as below:
19.   When you see the above error, open a new session to the Server and execute the below commands:
export ORACLE_HOME=/u02/oradata/MYDB/product/11.2.0/dbhome_1
sed -i 's/^\(\s*\$(MK_EMAGENT_NMECTL)\)\s*$/\1 -lnnz11/g' $ORACLE_HOME/sysman/lib/ins_emagent.mk
sed -i 's/^\(\$LD \$LD_RUNTIME\) \(\$LD_OPT\)/\1 -Wl,--no-as-needed \2/g' $ORACLE_HOME/bin/genorasdksh
sed -i 's/^\(\s*\)\(\$(OCRLIBS_DEFAULT)\)/\1 -Wl,--no-as-needed \2/g' $ORACLE_HOME/srvm/lib/ins_srvm.mk
sed -i 's/^\(TNSLSNR_LINKLINE.*\$(TNSLSNR_OFILES)\) \(\$(LINKTTLIBS)\)/\1 -Wl,--no-as-needed \2/g' $ORACLE_HOME/network/lib/env_network.mk
sed -i 's/^\(ORACLE_LINKLINE.*\$(ORACLE_LINKER)\) \(\$(PL_FLAGS)\)/\1 -Wl,--no-as-needed \2/g' $ORACLE_HOME/rdbms/lib/env_rdbms.mk
20.   Then press Continue and keep going …

21.   Now following the instructions and run the following commands with root user:
/u02/app/oraInventory/orainstRoot.sh
/
u02/oradata/MYDB/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
oracle@ubs-desktop:~$ lsnrctl start
26.   startup database
oracle@ubs-desktop:~$ sqlplus sys as sysdba
SQL> startup;
SQL> alter user scott identified by tiger;
SQL> alter user scott account unlock;
SQL> exit

Troubleshoot -EM Daemon is not running

Quite often, the Enterprise Manager Database Control doesn’t work.  An error is seen as follows
$ emctl status dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.1.0
Copyright (c) 1996, 2009 Oracle Corporation. All rights reserved.
http://bassam-desktop:1158/em/console/aboutApplication
EM Daemon is not running.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Logs are generated in directory /u02/app/oracle/product/carried-desktop_orcl/sysman/log
FIX:-
Set date/time

  •         SQL>ALTER database SET TIME_ZONE ='Asia/Calcutta';
Run following commands

  1.        $ORACLE_HOME/sysman/lib
  2.        make -f ins_emagent.mk "agent"
  3.        snip ... -lnmemso -lcore11 -lnnz11 ...snip
Still not fixed????
Try  running:-

  •          emca -config dbcontrol db -repos recreate