Skip Headers
Oracle® Database Express Edition 2 Day DBA
11g Release 2 (11.2)

Part Number E18804-04
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

3 Connecting to the Database

To interact with Oracle Database XE, you must typically connect to the database as a database user. The interaction might be through the SQL Command Line, through SQL Developer, or through utilities invoked from the system command line.

This chapter focuses on the SQL Command Line (SQL*Plus). For information about using database connections in SQL Developer, see the section about creating database connections in Oracle Database Express Edition Getting Started Guide. That section includes the convenient technique of automatically generating connections for all unlocked accounts on the local system.

This chapter covers the following topics:

See Also:

The following documents, for information on how to connect to the database from your application:

About Local and Remote Connections

­Oracle Database XE supports connections between the SQL Command Line (SQL*Plus) and the database either locally, or remotely over a TCP/IP network. The method that you use to connect to Oracle Database XE with the SQL Command Line depends on whether you are initiating a local connection or a remote connection. Local and remote connections are explained in the following sections:

See Also:

About Local Connections

Connecting locally means running the SQL Command Line (or any other Oracle command-line utility) on the same host computer where Oracle Database XE is installed (the "Oracle Database XE host computer") and then initiating a database connection from the SQL Command Line (or other utility), or using SQL Developer to connect as a local database user. To connect locally, you must supply only a database user name and password. For example, Figure 3-1 shows a user connecting locally with the SQL Command Line and entering a connect command that supplies the user name hr and password hr.

Figure 3-1 Local Connection

Description of Figure 3-1 follows
Description of "Figure 3-1 Local Connection"

Environment Variables

Before making a local connection on Linux, you must set environment variables. See "Setting Environment Variables" for more information.

Note:

Logging in to the Oracle Database XE host computer with an ssh (or telnet) session and then starting the SQL Command Line (or other Oracle command-line utility) is considered a local connection, even though you start the ssh (or telnet) application on a remote computer.

About Remote Connections

Connecting remotely means running the SQL Command Line (or any other Oracle command-line utility) on a computer other than the Oracle Database XE host computer, and then initiating a database connection from the SQL Command Line (or other utility) over the network.

Oracle Client Software

The remote computer must have Oracle client software installed. It is through Oracle client software that Oracle command-line utilities (and your applications) connect to the database. Oracle Database XE accepts connections from all of the following types of Oracle client software:

  • Oracle Database Express Edition Client (Oracle Database XE)

    When you install Oracle Database XE, Oracle Database XE is also installed on the same computer. You can install Oracle Database XE separately on remote computers. It is available at

    http://www.oracle.com/technetwork/database/express-edition/

  • Instant Client

    Instant Client is available at

http://www.oracle.com/technetwork/database/features/instant-client/index-100365.html
  • Oracle client software for Oracle Database Enterprise Edition or Standard Edition (all supported releases of Oracle Database)

All Oracle client software includes Oracle Net, which is the Oracle network software that enables client applications on one computer to connect to databases on another computer over a network.

Connect Strings

To connect remotely, you must supply not just a user name and password, but a complete Oracle Net connect string. In addition to the database user name and password, a connect string includes a host name or host IP address, an optional TCP port number, and an optional database service name. These additional parameters are required to help Oracle Net find the right host computer and connect to Oracle Database XE. An Oracle Net connect string has the following format:

username/password@[//]host[:port][/service_name]

where:

  • // is optional

  • host is the host name or IP address of the computer that is running Oracle Database XE

  • port (optional) is the TCP port number on which the Oracle Net listener is listening. If not specified, the default port number 1521 is assumed.

  • service_name (optional) is the name of the database service to which to connect. For Oracle Database XE, the service name is XE. If service_name is omitted, Oracle Database XE Client appends a request for the default database service, which is configured during installation as XE.

Note:

Only Oracle Database XE supports the notion of a default database service. If you connect remotely from any Oracle client software other than Oracle Database XE, you must include the XE service name.

For example, Figure 3-2 shows a user connecting remotely with the SQL Command Line and entering a connect command that includes a connect string that supplies: the user name hr, the password hr, and the host name dbhost.example.com. This connect string connects to the default database service (XE) on the default port (1521).

Figure 3-2 Remote Connection

Description of Figure 3-2 follows
Description of "Figure 3-2 Remote Connection"

Environment Variables

Before making a remote connection from Linux, you must set environment variables. See "Setting Environment Variables" for more information.

Remote Connection Examples

In the following examples of the SQL Command Line connect commands, Oracle Database XE is running on the host computer mydbserver.example.com.

Example 1 This example initiates a remote connection from Oracle Database XE, using the default port number.

CONNECT system/mypassword@mydbserver.example.com

Example 2 This example initiates a remote connection from Oracle Database XE, using a nondefault port number (1522):

CONNECT system/mypassword@mydbserver.example.com:1522

Example 3 This example initiates a remote connection using the default port number, and includes the optional service name.

CONNECT system/mypassword@mydbserver.example.com/XE

Setting Environment Variables

the SQL Command Line (SQL*Plus) and other Oracle utilities retrieve configuration information from operating system environment variables. This section explains how to set these environment variables, and contains the following topics:

Setting Environment Variables on the Windows Platform

On the Windows platform, environment variables are stored in the Windows registry, and are automatically set for you. You need not take any action involving environment variables before initiating a database connection.

Setting Environment Variables on the Linux Platform

On the Linux platform, before running the SQL Command Line or other Oracle utilities from a terminal session, you must set some environment variables for that session. The procedure for setting environment variables depends on whether you are connecting locally, or connecting remotely from Oracle Database XE.

Setting Environment Variables for a Local Connection

To set environment variables when connecting locally, enter one of the following commands in a terminal session.

For Bourne, Korn, or Bash shell:

source /usr/lib/oracle/xe/app/oracle/product/11.2.0/server/bin/oracle_env.sh

For C shell:

source /usr/lib/oracle/xe/app/oracle/product/11.2.0/server/bin/oracle_env.csh

See "Environment Variables Reference for Linux" for more information.

Note:

There is no need to set environment variables before running the SQL Command Line from the K menu (KDE) or Applications menu (Gnome).

Setting Environment Variables for a Remote Connection

To set environment variables when connecting remotely from Oracle Database XE, enter one of the following commands in a terminal session at the remote computer:

For Bourne, Korn, or Bash shell:

source /usr/lib/oracle/xe/app/oracle/product/11.2.0/client/bin/oracle_env.sh

For C shell:

source /usr/lib/oracle/xe/app/oracle/product/11.2.0/client/bin/oracle_env.csh

See "Environment Variables Reference for Linux" for more information.

Connecting Locally with the SQL Command Line

Connecting locally means running the SQL Command Line (SQL*Plus) and Oracle Database XE on the same computer. There are two ways to start a local connection with the SQL Command Line:

Starting the SQL Command Line from the Desktop

To start the SQL Command Line from the desktop and connect locally:

  1. Do one of the following:

    • On Windows: Click Start, point to Programs (or All Programs), point to Oracle Database 11g Express Edition, and then select Run SQL Command Line.

    • On Linux with Gnome: In the Applications menu, point to Oracle Database 11g Express Edition, and then select Run SQL Command Line.

    • On Linux with KDE: Click the icon for the K Menu, point to Oracle Database 11g Express Edition, and then select Run SQL Command Line.

    A the SQL Command Line command window opens.

  2. At the SQL Command Line prompt, enter the following command:

    CONNECT username/password
    

    For example, to connect as user HR with the password PEOPLE, enter the following command:

    CONNECT HR/PEOPLE
    

Starting the SQL Command Line from a Terminal Session or Command Window

To start the SQL Command Line from a terminal session or command window and connect locally:

  1. If not already open, open a terminal session (Linux) or a command window (Windows).

  2. (Linux only) If the required environment variables are not already set for your session, set them as described in "Setting Environment Variables on the Linux Platform".

  3. Enter the following command at the operating system prompt:

    SQLPLUS /NOLOG
    
  4. At the SQL Command Line prompt, enter the following command:

    CONNECT username/password
    

    For example, to connect as user HR with the password PEOPLE, enter the following command:

    CONNECT HR/PEOPLE
    

Connecting Remotely with the SQL Command Line

Connecting remotely means running the SQL Command Line (SQL*Plus) on one computer (the remote computer), and then initiating a connection to Oracle Database XE on a different computer.

To initiate a remote connection from the SQL Command Line using the Oracle Database XE:

  1. On the remote computer, start a terminal session (Linux) or open a command window (Windows.)

    If prompted for host credentials, log in to the remote computer.

  2. (Linux only) If the required environment variables are not already set for your session, set them as described in "Setting Environment Variables on the Linux Platform".

  3. Enter the following command at the operating system prompt:

    SQLPLUS /NOLOG
    
  4. Enter a CONNECT command at the SQL Command Line prompt, supplying a connect string.

    CONNECT username/password@[//]host[:port][/service_name]
    

    See "About Remote Connections" for a description and examples of connect strings.

Environment Variables Reference for Linux

This section provides reference information for setting environment variables on Linux for the following two scenarios:

Table 3-1 lists the environment variables that you must set for each of these scenarios. Table 3-2 provides environment variable descriptions and required values.

Table 3-1 Required Linux Environment Variables for Connecting with Oracle Utilities

Connection Type Required Environment Variables

Local


ORACLE_SID
ORACLE_HOME
PATH
NLS_LANG
LD_LIBRARY_PATH

Remote, using Oracle Database XE


ORACLE_HOME
PATH
NLS_LANG
LD_LIBRARY_PATH
SQLPATH

Table 3-2 Environment Variable Descriptions and Values for Linux

Variable Name Description Required Value

ORACLE_SID

Oracle Instance ID

XE

ORACLE_HOME

Oracle home directory

For local connection:

/usr/lib/oracle/xe/app/oracle/product/11.2.0/server

For remote connection with Oracle Database XE:

/usr/lib/oracle/xe/app/oracle/product/11.2.0/client

PATH

Search path for executables. (Must add $ORACLE_HOME/bin to the path.)

For Bourne, Korn, or Bash shell: $ORACLE_HOME/bin:$PATH

For C shell: $ORACLE_HOME/bin:${PATH}

NLS_LANG

Locale (language and territory used by client applications and the database; character set used by client applications)

(The desired language, territory, and character set. See Oracle Database Express Edition Installation Guide for Linux x86-64 for details.)

Defaults to AMERICAN_AMERICA.US7ASCII

LD_LIBRARY_PATH

Search path for shared libraries. (Must add $ORACLE_HOME/lib to the path.

$ORACLE_HOME/lib:$LD_LIBRARY_PATH

SQLPATH

Search path used by the SQL Command Line (SQL*Plus) for *.sql scripts. Contains a colon-separated list of paths. Must include the location of the site profile script, glogin.sql.

$ORACLE_HOME/sqlplus/admin


Example

The following are the Bash shell commands that set the required environment variables for a local connection on a Linux installation in the United States:

ORACLE_SID=XE;export ORACLE_SID
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/11.2.0/server;export ORACLE_HOME
PATH=$ORACLE_HOME/bin:$PATH;export PATH
NLS_LANG=AMERICAN_AMERICA.AL32UTF8;export NLS_LANG
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH

Environment Variable Scripts

Oracle Database XE and Oracle Database XE ship with two shell scripts that you can use to easily set environment variables. The scripts are located in $ORACLE_HOME/bin and are named as follows:


oracle_env.sh (for Bourne, Korn, or Bash shell)
oracle_env.csh (for C shell)

You can invoke these scripts from within dot files so that environment variables are set automatically each time that you start a new terminal session (start a new shell). The following is an example of a command that you can add to the .cshrc file in your home directory:

source /usr/lib/oracle/xe/app/oracle/product/11.2.0/server/bin/oracle_env.csh