Connecting to oracle database on linux

3 Connecting to the Database

In addition to using the Oracle Database XE graphical user interface to work with the database, you can use the set of command-line utilities that come with Oracle Database XE. Table 3-1 lists these u tilities. To use these utilities, you must understand how to connect to the database with them.

Table 3-1 Oracle Database XE Command-Line Utilities

Administer the database; create, alter and drop database objects; run SQL queries; and run SQL and PL/SQL scripts.

Export and import data from one Oracle database to another.

Export and import data from one Oracle database to another.

Bulk-load data into the database from external files.

Although connection concepts apply to all utilities, this section focuses on SQL Command Line (SQL*Plus), because you use it for database administration. The following topics are covered:

About Local and Remote Connections

­Oracle Database XE supports connections between 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 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:

  • «Connecting Locally with SQL Command Line»
  • «Connecting Remotely with SQL Command Line»
  • «Managing Network Connections» for information on how Oracle Database XE accepts connection requests over the network.

About Local Connections

Connect ing locally means running 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 SQL Command Line (or other utility). To connect locally, you must supply only a database user name and password. For example, Figure 3-1 shows a user connecting locally with SQL Command Line and entering a connect command that supplies the user name hr and password hr .

Figure 3-1 Local Connection

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

Logging in to the Oracle Database XE host computer with an ssh (or telnet ) session and then starting 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

Connectin g remotely means running 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 SQL Command Line (or other utility) over the network.

Читайте также:  How to create directories in linux terminal

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 clien t software:

  • Oracle Database Express Edition Client (Oracle Database XE Client) When you install Oracle Database XE, Oracle Database XE Client is also installed on the same computer. You can install Oracle Database XE Client separately on remote computers. It is available at http://www.oracle.com/technology/xe
  • Instan t Clie nt Instant Client is available at
  • Oracle client software for Oracle Database Enterprise Edition or Standard Edition (all supported releases of Oracle Database 8, 8 i , 9 i , and 10 g )

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.

To connect remotely, you must supply not just a user name and password, but a complete Oracle Net conne ct 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]
  • // 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 .

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

For example, Figure 3-2 shows a user connecting remotely with 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.xyz.com . This connect string connects to the default database service ( XE ) on the default port ( 1521 ).

Figure 3-2 Remote Connection

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 SQL Command Line connect commands, Oracle Database XE is running on the host computer mydbserver.mydomain.com .

Читайте также:  Семейство ос linux особенности отличия от ос windows

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

CONNECT system/mypassword@mydbserver.mydomain.com

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

CONNECT system/mypassword@mydbserver.mydomain.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.mydomain.com/XE

Setting Environment Variables

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 Win dows 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 Pla tform

On the Linux platform, before running 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 Client.

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/10.2.0/server/bin/oracle_env.sh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.csh

There is no need to set environment variables before running 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 Client, 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/10.2.0/client/bin/oracle_env.sh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/bin/oracle_env.csh

Connecting Lo cally with SQL Command Line

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

Starting SQL Command Line from the Desktop

To start 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 10g Express Edition , and then select Run SQL Command Line .
    • On Linux with Gnome: In the Applications menu, point to Oracle Database 10g Express Edition , and then select Run SQL Command Line .
    • On Linux with KDE: Click the icon for the K Menu, point to Oracle Database 10g Express Edition , and then select Run SQL Command Line .

A SQL Command Line command window opens.

Starting SQL Command Line from a Terminal Session or Command Window

To start 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:

Connecting Rem otely with SQL Command Line

Connecting remotely means running 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 SQL Command Line using the Oracle Database XE Client:

  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:
CONNECT username/password@[//]host[:port][/service_name]

Environment Variables Reference for Linux

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

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

Table 3-2 Require d Linux Environment Variables for Connecting with Oracle Utilities

Remo te, using Oracle Database XE Client

Table 3-3 Environment Variable Descriptions and Values for Linux

For remote connection with Oracle Database XE Client:

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:$

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 for details.)

Defaults to AMERICAN_AMERICA.US7ASCII

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

Search path used by 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 .

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/10.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 Sc ripts

Oracle Database XE and Oracle Database XE Client 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/10.2.0/server/bin/oracle_env.csh

Источник

Оцените статью
Adblock
detector