Getting Started With Oracle

- Installation

- Solaris
- Linux

- Create Users, Groups and Directories
- Make Sure You Have Enough Memory
- Install the Development Software
- Set Up the Environment
- Install Oracle
- Starting the Database at Boot Time

- Creating a Database
- Accessing a Database
- Accessing a Database With SQL Relay

Installation

I've sucessfully installed Oracle on Linux and I've witnessed many Solaris installations.

When installing Oracle, the most common mistake I've made is having too little physical ram or swap. The second most common mistake I've made is not configuring the kernel with enough shared memory or semaphore resources. The installation instructions that come with Oracle for each platform indicate how much ram and swap you need and how to configure the kernel properly. I recommend meeting these requirements prior to installation.

Solaris

Oracle versions 8.1.5-11.2.0 for Solaris have been or are currently available for download from technet.oracle.com or on CD. Installation appears to be as simple as following the directions. Note that for Solaris, these versions of Oracle will fail to create a database at installation time unless the kernel is configured properly to support more and larger shared memory segments and semaphores. Aside from neglecting this step, I've never seen or heard of anyone having much difficulty with these versions on Solaris.

Linux

Oracle has supported Linux since version 8.0.5. Versions 8.0.5 through 11.2.0 have been or are currently available from technet.oracle.com and have been available on CD from various sources as well.

Create Users, Groups and Directories

Run the following commands as root to set up the appropriate users, groups and directories.

> groupadd oinstall
> groupadd dba
> groupadd oper
> useradd -m oracle -g oinstall -G dba,oper
> chown -R oracle:dba /home/oracle
> passwd oracle (assign the oracle user a password)
> mkdir /u01
> chown oracle:dba /u01
> chmod 775 /u01

Edit /home/oracle/.bashrc and add the following command near the bottom.

> umask 022

Make Sure You Have Enough Memory

The number one reason that Oracle installations have failed for me is because I ran out of ram and swap. Oracle really needs about 725mb of total ram/swap. If your system has less than that, you can add 725mb of swap as follows.

> dd if=/dev/zero of=/extraswap bs=1k count=742400
> chmod 600 /extraswap
> /sbin/mkswap /extraswap
> /sbin/swapon /extraswap

Oracle doesn't appear to need that much ram/swap to run, so after the installation is finished, you can remove it as follows:

> /sbin/swapoff /extraswap
> rm /extraswap

Install the Development Software

The Oracle installer needs GNU make, the GNU C Compiler (gcc) and GNU Binutils (ar, as, ld, etc.) to compile and link several libraries at installation time. Make sure that these packages are installed.

Set Up the Environment

Edit /etc/profile (or /etc/bash.bashrc.local if your system has it) and add the following somewhere near the top, replacing 11.2.0 in the ORACLE_VERSION environment variable with the version of Oracle that you are installing.

> export ORACLE_VERSION=11.2.0
> export ORACLE_BASE=/u01/app/oracle
> export ORACLE_HOME=$ORACLE_BASE/product/$ORACLE_VERSION
> export ORACLE_SID=ora1
> export PATH=$PATH:$ORACLE_HOME/bin
> export CLASSPATH=$ORACLE_BASE/jdbc/lib/classes111.zip
> export LD_LIBRARY_PATH=$ORACLE_HOME/lib
> export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
> export ORACLE_OWNER=oracle
> export ORACLE_TERM=386
> export NLS_LANG=american

If you want to use a language other than US English, consult the following table and replace american with the appropriate language string.

Language | NLS_LANG value
US English | american
Arabic | arabic
Brazilian Portugese | "brazilian portugese"
Canadian French | frc
Czech | czech
Danish | danish
Dutch | dutch
Finnish | finnish
French | french
German | german
Hungarian | hungarian
Icelandic | is
Italian | italian
Japanese | japanese
Korean | korean
Lithuanian | lt
Mexican Spanish | esm
Norweigan | norweigan
Polish | polish
Portugese | portugese
Russian | russian
Simplified Chinese | "simplified chinese"
Slovak | slovak
Swedish | swedish
Thai | th
Traditional Chinese | "traditional chinese"
Turkish | turkish

Install Oracle

At this point, you should be ready to install Oracle.

Each version of Oracle has different installation procedures. Follow the links below for the version of Linux and Oracle that you are installing. Each "Yes" link indicates that I was able to get that version of Oracle to install and run on that version of Linux.

| 8.0.5 | 8.1.5 | 8.1.6 | 8.1.7 | 9.0.x | 9.2.x | 10.1.x | 10.2.x | 11.2.0
Redhat 5.2 | Yes | No | No | No | No | No | No | No | No
Redhat 6.x | No | No | Yes | Yes | No | No | No | No | No
Redhat 7.x | No | No | Yes | Yes | No | No | No | No | No
Redhat 8 | No | No | No | No | No | Yes | No | No | No
Redhat 9 | No | No | No | No | No | Yes | No | No | No
Fedora Core 1 | No | No | No | No | No | Yes | Yes | No | No
Fedora Core 2 | No | No | No | No | No | No | Yes | No | No
Fedora Core 3 | No | No | No | No | No | No | Yes | No | No
Fedora Core 4 | No | No | No | No | No | No | Yes | No | No
Fedora Core 5 | No | No | No | No | No | No | No | Yes | No
Fedora Core 12 | No | No | No | No | No | No | No | No | Yes
Fedora Core 13 | No | No | No | No | No | No | No | No | Yes
Fedora Core 14 | No | No | No | No | No | No | No | No | Yes
Suse 9.0 | No | No | No | No | No | Yes | No | No | No

Starting the Database at Boot Time

For Oracle versions 10g or earlier, you can use the following script to start/stop the database at boot/shutdown time. Replace /u01/app/oracle/product/11.2.0 with your $ORACLE_HOME

>     #!/bin/sh

>     case "$1" in
>       start)
>             su -l oracle -c "/u01/app/oracle/product/11.2.0/bin/dbstart"
>             su -l oracle -c "/u01/app/oracle/product/11.2.0/bin/lsnrctl start"
>             ;;
>       stop)
>             su -l oracle -c "/u01/app/oracle/product/11.2.0/bin/dbshut"
>             su -l oracle -c "/u01/app/oracle/product/11.2.0/bin/lsnrctl stop"
>             ;;
>       *)
>             echo $"Usage: $0 {start|stop}"
>             exit 1
>     esac

>     exit 0

For Oracle 11g or later, use the following script, replacing /u01/app/oracle/product/11.2.0 with your $ORACLE_HOME

> #!/bin/sh
> ORACLE_HOME=/u01/app/oracle/product/11.2.0
> case "$1" in
> start)
> su -l oracle -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"
> ;;
> stop)
> su -l oracle -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"
> ;;
> *)
> echo $"Usage: $0 {start|stop}"
> exit 1
> esac
> exit 0

Install this script and run it with the "start" option to start up the database. Running it with the "stop" option shuts the database down. To access a database, it must be running.

Creating a Database

During the installation, you could have chosen to create a database instance. If you did not create one during installation, you can do so afterward. Log in as oracle and run $ORACLE_HOME/bin/dbassist for Oracle 8i or $ORACLE_HOME/bin/dbca for Oracle 9i or higher and follow the on-screen instructions.

The dbassist/dbca program will ask you to provide an SID; a local identifier for the database. No two databases on the same machine can have the same SID. If you are using Oracle 8i or 9i you will also be asked to provide a Global Identifier. The Global Identifier should be unique among the Oracle databases on the local network.

After creating the database, edit the file /etc/oratab and look for a line containing the SID of the database you just created. Change the N at the end of that line to Y. This configures the database to start when the dbstart command is issued.

At this point, you can run the start/stop script to start up the database.

To access the database, the file $ORACLE_HOME/network/admin/tnsnames.ora must also be configured. This file contains an entry for each database that the local system needs to know about. Here is an example of a tnsnames.ora entry, identifying the database ORA1 on the local machine.

>     ORA1.LOCALDOMAIN =
>       (DESCRIPTION =
>         (ADDRESS_LIST =
>           (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
>         )
>         (CONNECT_DATA =
>           (SERVICE_NAME = ora1)
>         )
>       )

For Oracle 7 and 8, the format is SID = (... parameters ...). For Oracle 8i and 9i, the format is SID.DOMAIN = (... parameters ...). In this example the SID is ORA1. The HOST parameter refers to the DNS name of the host that the database is running on. The SERVICE_NAME refers to the SID of the database as it is known on that host.

When a database instance is created, two special administration users are created: SYS and SYSTEM. For versions of Oracle less than 9i, the default password for the SYSTEM user is manager. The default password for the SYS user is change_on_install. For Oracle 9i passwords must be set during the installation process. These users own the tables that contain privileges and other housekeeping information.

Database tables are stored in a file or set of files collectively called a tablespace. The tables owned by the SYS and SYSTEM users are kept in their own tablespace. Though you could create your own tables there, it's not a good idea. You should create your own tablespace.

First, check to see if /u01/app/oracle/oradata and /u01/app/oracle/oradata/ora1 exist. If either does not, run one or both of the following commands to create the necessary directory:

> mkdir -p /u01/app/oracle/oradata
> chown oracle:oinstall /u01/app/oracle/oradata
> mkdir -p /u01/app/oracle/oradata/ora1
> chown oracle:oinstall /u01/app/oracle/oradata/ora1

Now, for versions of Oracle less than 9i, use the following command to log into the ORA1 instance as the SYSTEM user:

> sqlplus system/manager@ora1

For Oracle 9i, use the same command, but replace manager with the password you entered during installation.

For Oracle 10g or higher, use the following command to log into the ORA1 instance as the SYS user, replacing password with the password you entered during installation:

> sqlplus sys/password@ora1 as sysdba

The following queries create a tablespace and temporary tablespace called testtablespace and testtablespacetemp with some minimal sizing parameters.

For versions of Oracle less than 8i, use the following queries:

>     CREATE TABLESPACE testtablespace
>     	DATAFILE '/u01/app/oracle/oradata/ora1/testtablespace01.dbf'
>     	SIZE 1M REUSE
>     	AUTOEXTEND ON NEXT 1M
>     	MAXSIZE UNLIMITED
>     	NOLOGGING;

>     CREATE TABLESPACE testtablespacetemp
>     	DATAFILE '/u01/app/oracle/oradata/ora1/testtablespacetemp01.dbf'
>     	SIZE 2M REUSE
>     	AUTOEXTEND ON NEXT 1M
>     	MAXSIZE UNLIMITED;

For versions of Oracle equal to or greater than 8i, use the following queries:

>     CREATE TABLESPACE testtablespace
>     	DATAFILE '/u01/app/oracle/oradata/ora1/testtablespace01.dbf'
>     	SIZE 1M REUSE
>     	AUTOEXTEND ON NEXT 1M
>     	MAXSIZE UNLIMITED
>     	NOLOGGING;

>     CREATE TEMPORARY TABLESPACE testtablespacetemp
>     	TEMPFILE '/u01/app/oracle/oradata/ora1/testtablespacetemp01.dbf'
>     	SIZE 2M REUSE
>     	AUTOEXTEND ON NEXT 1M
>     	MAXSIZE UNLIMITED;

The set of database objects (such as tables, indexes, stored procedures and triggers) owned by a particular user is called a schema. Though schemas may be distributed across tablespaces, they are more often confined to a single tablespace. The SYSTEM and SYS schemas contain tables used by internal systems and should not be used for application data. You can create a new user and schema by logging in as SYSTEM and running the following queries:

>     CREATE USER testuser IDENTIFIED BY testpassword
>     	DEFAULT TABLESPACE testtablespace
>     	TEMPORARY TABLESPACE testtablespacetemp;

>     GRANT CREATE SESSION TO testuser;

>     GRANT RESOURCE TO testuser;

Now you can exit and log in as the new user with the following command:

> sqlplus testuser/testpassword@ora1

To delete a user and everything owned by that user, log in as SYSTEM and run the following command:

>     DROP USER testuser CASCADE;

To delete a tablespace, log in as SYSTEM and run the following command:

>     DROP TABLESPACE testtablespace;

>     DROP TABLESPACE testtablespacetemp;

This should be enough you get you started. To set up more complex configurations, consult the documentation that came with your Oracle distribution.

Accessing a Database

Any user can access an Oracle database provided that the users environment is configured correctly. The ORACLE_HOME environment variable needs to be set to the same value that it was set for the oracle user during installation. The PATH varible must contain $ORACLE_HOME/bin and the LD_LIBRARY_PATH variable must contain $ORACLE_HOME/lib.

The user must have read access to the $ORACLE_HOME/network/admin/tnsnames.ora file. Alternatively, an individual user can have a .tnsnames.ora file.

The following command logs testuser with password testpassword into the database identified by the SID ora1.

> sqlplus testuser/testpassword@ora1

A sample session follows.

>     [user@localhost user]$ sqlplus testuser/testpassword@ora1

>     SQL*Plus: Release 8.1.7.0.0 - Production on Tue Jan 8 18:44:46 2002

>     (c) Copyright 2001 Oracle Corporation.  All rights reserved.

>     Connected to:
>     Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
>     JServer Release 8.1.7.0.0 - Production

>     SQL> create table testtable (
>       2  col1 varchar2(40),
>       3  col2 number
>       4  );

>     Table created.

>     SQL> select table_name from user_tables;

>     TABLE_NAME
>     ------------------------------
>     TESTTABLE

>     SQL> describe testtable;
>      Name                                      Null?    Type
>      ----------------------------------------- -------- ----------------------------
>      COL1                                               VARCHAR2(40)
>      COL2                                               NUMBER

>     SQL> insert into testtable values ('hello',50);

>     1 row created.

>     SQL> insert into testtable values ('hi',60);

>     1 row created.

>     SQL> insert into testtable values ('bye',70);

>     1 row created.

>     SQL> select * from testtable;

>     COL1                                           COL2
>     ---------------------------------------- ----------
>     hello                                            50
>     hi                                               60
>     bye                                              70

>     SQL> delete from testtable where col2=50;

>     1 row deleted.

>     SQL> select * from testtable;

>     COL1                                           COL2
>     ---------------------------------------- ----------
>     hi                                               60
>     bye                                              70

>     SQL> update testtable set col2=0 where col1='hi';

>     1 row updated.

>     SQL> select * from testtable;

>     COL1                                           COL2
>     ---------------------------------------- ----------
>     hi                                                0
>     bye                                              70

>     SQL> drop table testtable;

>     Table dropped.

>     SQL> quit
>     Disconnected from Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
>     JServer Release 8.1.7.0.0 - Production

Accessing a Database With SQL Relay

Accessing Oracle from SQL Relay requires an instance entry in your sqlrelay.conf file for the database that you want to access. Here is an example sqlrelay.conf which defines an SQL Relay instance called oracletest. This instance connects to the ora1 database as the user testuser with password testpassword. The ora1 parameter refers to an entry in the tnsnames.ora file which would be found in the network/admin subdirectory of the directory specified in the oracle_home parameter.

    <?xml version="1.0"?>
    <!DOCTYPE instances SYSTEM "sqlrelay.dtd">
    <instances>

            <instance id="oracletest" port="9000" socket="/tmp/oracletest.socket" dbase="oracle8" connections="3" maxconnections="5" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass">
                    <users>
                            <user user="oracletest" password="oracletest"/>
                    </users>
                    <connections>
                            <connection connectionid="oracletest" string="user=testuser;password=testpassword;oracle_home=/u01/app/oracle/product/11.2.0;oracle_sid=ora1" metric="1"/>
                    </connections>
            </instance>

    </instances>

Alternatively, if you don't have a tnsnames.ora file on the machine you want to run SQL Relay on, or don't want to create one, you can specify the oracle_sid as if it were a line from the tnsnames.ora file, as follows:

    <?xml version="1.0"?>
    <!DOCTYPE instances SYSTEM "sqlrelay.dtd">
    <instances>

            <instance id="oracletest" port="9000" socket="/tmp/oracletest.socket" dbase="oracle8" connections="3" maxconnections="5" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass">
                    <users>
                            <user user="oracletest" password="oracletest"/>
                    </users>
                    <connections>
                            <connection connectionid="oracletest" string="user=testuser;password=testpassword;oracle_sid=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ora1.localdomain)))" metric="1"/>
                    </connections>
            </instance>

    </instances>

Now you can start up this instance with the following command.

> sqlr-start -id oracletest

To connect to the instance and run queries, use the following command.

> sqlrsh -id oracletest

The following command shuts down the SQL Relay instance.

> sqlr-stop oracletest