Sie sind auf Seite 1von 51

Oracle Database Administration

Introduction Basic administration

Additional information
http://technet.oracle.com - for developers and administrators, available for:
Download of Oracle products Download and viewing of product documentation

http://www.mini.pw.edu.pl/~maczewsk/oracle
Presentations from the lecture Lab tasks

Grades
Each lab will start with a test
test duration: 15 90 minutes depending on the task each test will be graded in the scale 0 10 points at the end one worst result will be excluded

Final exam:
exam duration: 90 minutes will take place in the computer room

Lecture plan
Part 1 basic database administration:
creating database objects: users, tables, indexes, tablespaces, datafiles etc. managing database objects networking, connecting to remote databases programming Oracle PL/SQL language

Part 2 data safety


performing hot and cold backups restoring database from backup recovering database after hardware failure or user error managing standby databases

Lecture plan cont.


Part 3 performance tuning:
database instance parameters (e.g.: memory) tuning individual database components (log buffers, undo space) tuning SQL statements (obtaining execution plan, creating indexes, using hints)

Introduction to the Oracle Database

History
1978 Oracle V1, never officially released 1980 Oracle V2 released, written in assembly language 1994 Oracle 7 for PC released 1998 Support for Linux platform 1999 Oracle 8i with Java integration 2001 Oracle 9i 2004 Oracle 10g 2007 Oracle 11g

Oracle database
The best database for large database systems:
most reliable in terms of data safety highest availability scalable good internationalization support

Oracle shortcomings:
price expensive speed not the fastest database available amount of resources consumed

Data safety
Many features that ensure safety of data:
multiplexing of important database files writing changes to data files and to special log files. Log files are used to recover database after software or hardware failure archiving of log files. Archived log files can be written to backup location(s) protection from human errors:
possibility to view data from some point in time in the pas possibility to use backup and perform point in time recovery

Availability
Very high availability:
online backups without shutting down the database most administrative tasks can be performed while the database is running and available for users, for example: analyzing, rebuilding indexes, moving data disk failure: it is possible to turn off and recover only the part of the database that was affected by the failure, the rest of the database can continue running parameters that ensure that recovery after failure will take no more than the specified time standby database second database that can be opened if the main database fails

Scalability
Oracle can handle unlimited amount of data:
no limit on number of rows, number of tables etc. no limit on total size of the database

Oracle runs on various operating systems:


Windows NT/XP Linux Solaris, HP-UX, AIX

Oracle Grid single database on a cluster

Internationalization
Database supports multiple languages/locales:
sorting of text can be done in client language order date, time and currency is client dependent character set conversion database running in utf8 or iso8859-2 will convert data for clients using windows-1250

Internally database can store data in Unicode

Oracle editions
Express edition:
Simple installation and administration, some features are not available (for example: Java Virtual Machine is disabled)

Standard edition:
Most frequently used features are available

Enterprise edition:
All features, very expensive

Speed of the database


Oracle favors data safety over speed:
it is not possible to disable some features to achieve greater speed (e.g.: transactions) some other databases (e.g. MySQL) are faster than Oracle

Resources consumed
Oracle consumes large amounts of resources:
new, empty database at least 200MB, usually over 1GB at least 256MB RAM

Hint: when installing Oracle on private computer (Windows):


install Standard or Enterprise edition assign around 256MB RAM for Oracle install database normally change startup of Oracle services to manual when required start the database manually with: oradim startup sid database_sid

Oracle terminology - database


Database data stored on disk (data files and some additional files) Database instance program (group of programs) that opens the database (disk files) and makes it available to users User connects to the database instance and retrieves data or requests changes to the database When running Oracle on a cluster, there are multiple database instances that open the same database

Oracle terminology - schema


Schema set of objects (tables, indexes, views) that belong to a database user.
In Oracle SCHEMA = USER When new user is created (CREATE USER command), initially it has an empty schema When the user logs in and creates new table, the table is added to his schema When user issues: SELECT * FROM some_table Oracle looks for some_table in the users schema It is possible to access data in some other schema: SELECT * FROM some_user.some_table

Major differences from other database systems

Single database
Many other database systems (Postgres, MSSQL) can have multiple databases opened by a single database engine
each application can use separate database for its data

In Oracle single Oracle Instance can open one database


normally on a single database server there will be only one database each application can store its data in separate user schema

Data storage
Many other database systems (Postgres, MySQL) store table data in a separate file or group of files Oracle stores table data in a tablespace. Multiple tables are normally stored in the same tablespace. Tablespace can be stored in one or more data files (physical disk files)

Transactions
Oracle executes every statement in a transaction
there is no command to start a transaction (like BEGIN TRANSACTION) transaction is started automatically with a first statement after COMMIT or ROLLBACK usually transaction must be finished manually with COMMIT, exceptions:
it is possible to turn on AUTOCOMMIT (by default: disabled) some statements commit the transaction automatically: all CREATE, ALTER, DROP and TRUNCATE statements

Transaction example
-- user connects to the database DELETE FROM table1; -- new transaction created INSERT INTO table1 VALUES (1, some text); INSERT INTO table1 VALUES (2, some text); CREATE TABLE table2 ( id NUMBER PRIMARY KEY, text VARCHAR2(256) ); ROLLBACK; -- table1 contains 2 rows, -- the rollback statement rolled -- back empty transaction

Date format, language


Oracle has no fixed date and time format. When displaying dates, Oracle uses the format consistent with the connected client Language used by Oracle (for example: error messages) also depend on the language settings To use English with Oracle, set the environment variable NLS_LANG before starting SQLPlus:
SET NLS_LANG=AMERICAN_AMERICA.UTF8

General database terminology


Database user login and password used to connect to the database. Each user has:
set of privileges for accessing the database schema objects: objects: tables, indexes etc.

Table database object used for data storage:


can have any number of rows has fixed number of columns, each column has a name and datatype can have constraints, indexes and relations to other tables

Relation defines a dependency between two tables

Constraint rule that must be satisfied by each table row. Constraint can be:
PRIMARY KEY constraint NOT NULL constraint UNIQUE constraint FOREIGN KEY constraint (relation) CHECK constraint

Index object that organizes data in a table. Indexes are used:


to enforce PRIMARY KEY and UNIQUE constraints to improve query performance

Trigger action performed when user makes changes to database data Session single connection to the database. One user can open multiple sessions to the database Transaction set of operations that are visible as single (atomic) operation on the database. Transaction belongs to a single open database session.

Accessing the database


Oracle can be accessed from various tools:
Oracle tools:
SQLPlus (always available) command line tool for executing SQL statements. All administrative statements can be executed from SQLPlus Enterprise Manager software for monitoring and managing Oracle databases.

Third party tools:


TOAD application for administrating Oracle database, executing SQL statements, TOAD Freeware Edition is available for free SQLNavigator application for creating stored procedures, inspecting data, executing SQL statements

Connecting to the database


In order to connect, you need to specify:
user name password connect string (optional) connect mode (optional)

Connect string:
determines location of the database, if not specified local database

Connect mode specified for administrative connections (database startup and shutdown)

Starting database session


The following commands will start SQLPlus and connect to the database:
sqlplus test/testpass Log in as user test
password testpass to local database test to database db

sqlplus test/testpass@db Log in as user sqlplus system/orclstudent Log in as user


system password orclstudent

sqlplus sys/orclstudent as sysdba Log in


as user sys with special SYSDBA privileges

Networking

Connecting to Oracle
Connection to Oracle is possible:
in local mode when connecting to local database only via network when connecting to some other database

All non-local connections are done through the listener

The listener
Oracle Listener is separate from the database In Windows it starts as service OraclexxxxTNSListener One listener can handle connections for multiple databases Listener configuration determines how clients will connect to the database. By default listener:
uses TCP/IP listens on port 1521 default Oracle port

Listener configuration
Listener configuration is in
$ORACLE_HOME/network/admin/listener.ora

Example configuration:
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = host_name)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0)) ) )

Listener startup and shutdown


Listener can be started manually from the command line:
lsnrctl start

Other commands:
lsnrctl stop stops the listener lsnrctl status shows list of services that this listener supports

Naming configuration
When connecting to database using SQLPlus you specify:
user name password database name

When the database name is empty connection to the local database When the database name is not empty lookup of the database name. By default database names are looked up in tnsnames.ora file

tnsnames.ora
Location:
$ORACLE_HOME/network/admin/tnsnames.ora

This file is used to lookup names of databases:


IP address of the database listener port number name of the Oracle instance

tnsnames.ora - example
Typical entry in the tnsnames.ora file:
TEST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )

tnsnames.ora - example
Entries in tnsnames.ora enable:
specifying multiple addresses for a connection (connect time failover) request load balancing between the list of available addresses
TEST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = other_host) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )

tnsnames.ora - example
Load balancing example
TEST = (DESCRIPTION = (ADDRESS_LIST = (LOAD_BALANCE=on) (FAILOVER=off) (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = other_host) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )

Database administration

Administrative privileges
Database administration requires strong privileges:
SYSDBA the most powerful database privilege, enables database startup, shutdown and access to all data in the database SYSOPER enables database startup and shutdown without access to database data DBA role (a group of privileges) giving user full administrative access, but without the right to start and shut down the database

Administrator accounts
Each Oracle database has two accounts:
SYS owner of the administrative objects, created with DBA role, has SYSDBA and SYSOPER privileges SYSTEM automatically created with DBA role

SYS and SYSTEM users should not be used to create any tables

DBA, SYSDBA and SYSOPER


DBA role grants administrator access to the database when the database is running DBA role is not enough to startup and shutdown the database SYSDBA and SYSOPER enable database startup and shutdown In order to use the SYSDBA or SYSOPER privilege, the connection must be in special mode, e.g.: connect sys/sys_password as

SYSDBA and SYSOPER


DBA user can be granted SYSDBA or SYSOPER privilege SYSDBA privilege enables you to

Perform STARTUP and SHUTDOWN operations ALTER DATABASE: open, mount, back up, or change character set CREATE DATABASE DROP DATABASE CREATE SPFILE ALTER DATABASE ARCHIVELOG ALTER DATABASE RECOVER Includes the RESTRICTED SESSION privilege SYSDBA lets you look at any users data

SYSDBA and SYSOPER


SYSOPER privilege enables you to
Perform STARTUP and SHUTDOWN operations CREATE SPFILE ALTER DATABASE OPEN/MOUNT/BACKUP ALTER DATABASE ARCHIVELOG ALTER DATABASE RECOVER (Complete recovery only. Any form of incomplete recovery, such as UNTIL TIME|CHANGE|CANCEL|CONTROLFILE requires connecting as SYSDBA.) Includes the RESTRICTED SESSION privilege

SYSOPER does not let you look at other users data

Database shutdown
SHUTDOWN NORMAL waits for all users to disconnect, not recommended can take a long time SHUTDOWN TRANSACTIONAL waits for all users to finish their transactions SHUTDOWN IMMEDIATE rolls back all uncommitted transactions, closes all sessions, closes the database and shuts down the instance SHUTDOWN ABORT aborts the instance, does not close the database properly. Requires automatic recovery after startup.

Database startup
Database startup involves 3 steps:
starting the instance mounting the database opening the database opened database is available for use

STARTUP executes those 3 steps When performing administrative actions, those steps can be performed one by one:
STARTUP NOMOUNT only start the instance, useful when creating the database STARTUP MOUNT start the instance and mount the database (open database control files)

Database startup
Example startup (rarely used):
STARTUP NOMOUNT ALTER DATABASE MOUNT ALTER DATABASE OPEN

Example startup (used for many administrative actions):


STARTUP MOUNT perform some administrative actions, e.g. recover the database, change archiving mode ALTER DATABASE OPEN opens database for use

System views
Oracle provides many system views that provide information about the database:
v$session all open sessions v$datafile all data files v$logfile all log files v$database general information about the database v$instance general information about the instance

Some of those views are available when the database is closed

Example scenario
The database cannot start, error message is that file number 4 is corrupt. Recovery:
STARTUP MOUNT SELECT name FROM v$datafile WHERE file# = 4

fix the problem with the file, e.g. restore it from backup recover the database
ALTER DATABASE OPEN

Restricted mode
Sometimes the administrator wants to execute some actions without users being able to log in In order to do that, activate the restricted mode, only users with RESTRICTED SESSION privilege will be able to log in:
STARTUP RESTRICTED starts up in restricted

mode
ALTER SYSTEM ENABLE RESTRICTED SESSION ALTER SYSTEM DISABLE RESTRICTED SESSION

Das könnte Ihnen auch gefallen