oracle database administration introduction basic administration

50
Oracle Database Administration Introduction Basic administration

Upload: deborah-meghan-alexander

Post on 26-Dec-2015

295 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Oracle Database Administration Introduction Basic administration

Oracle Database Administration

Introduction

Basic administration

Page 2: 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

Page 3: Oracle Database Administration Introduction Basic administration

Lecture plan• Part 1 – basic database administration:

– creating database objects: users, tables, indexes, tablespaces, datafiles etc.

– managing database objects– networking, connecting to remote databases– SQL language – SQL standard and Oracle

extensions– programming Oracle – PL/SQL language– exporting and importing data

Page 4: Oracle Database Administration Introduction Basic administration

Lecture plan cont.• Part 2 – 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)

Page 5: Oracle Database Administration Introduction Basic administration

Lecture plan cont.• Part 3 – backup and recovery:

– Physical data storage – Offline and online backups– Restoring database from backup– Recovering database after hardware failure or user

error– Managing standby database

Page 6: Oracle Database Administration Introduction Basic administration

Introduction to the Oracle Database

Page 7: Oracle Database Administration Introduction Basic administration

Oracle 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 – modern Oracle • 2004 – Oracle 10g• 2007 – Oracle 11g• 2013 – Oracle 12c

Page 8: Oracle Database Administration Introduction Basic administration

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

Page 9: Oracle Database Administration Introduction Basic administration

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

Page 10: Oracle Database Administration Introduction Basic administration

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

Page 11: Oracle Database Administration Introduction Basic administration

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 Cloud/Grid – single database on a cluster

Page 12: Oracle Database Administration Introduction Basic administration

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

Page 13: Oracle Database Administration Introduction Basic administration

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

Page 14: Oracle Database Administration Introduction Basic administration

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

Page 15: Oracle Database Administration Introduction Basic administration

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

Page 16: Oracle Database Administration Introduction Basic administration

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

Page 17: Oracle Database Administration Introduction Basic administration

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 user’s schema

– It is possible to access data in some other schema:

SELECT * FROM some_user.some_table

Page 18: Oracle Database Administration Introduction Basic administration

Major differences from other database systems

Page 19: Oracle Database Administration Introduction Basic administration

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

Page 20: Oracle Database Administration Introduction Basic administration

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)

Page 21: Oracle Database Administration Introduction Basic administration

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

Page 22: Oracle Database Administration Introduction Basic administration

Transaction example-- user connects to the databaseDELETE FROM table1; -- new transaction createdINSERT 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

Page 23: Oracle Database Administration Introduction Basic administration

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

Page 24: Oracle Database Administration Introduction Basic administration

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

Page 25: Oracle Database Administration Introduction Basic administration

• 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

Page 26: Oracle Database Administration Introduction Basic administration

• 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.

Page 27: Oracle Database Administration Introduction Basic administration

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

• SQL Developer – free development tool, easier to use than SQLPlus

– Third party tools:• TOAD –application for administrating Oracle database,

executing SQL statements, TOAD Freeware Edition is available for free

Page 28: Oracle Database Administration Introduction Basic administration

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)

Page 29: Oracle Database Administration Introduction Basic administration

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

– sqlplus test/testpass@db – Log in as user “test” to database “db”

– sqlplus system/orclstudent – Log in as user “system” password “orclstudent”

– sqlplus sys/orclstudent as sysdba – Log in as user “sys” with special SYSDBA privileges

Page 30: Oracle Database Administration Introduction Basic administration

Networking

Page 31: Oracle Database Administration Introduction Basic administration

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

Page 32: Oracle Database Administration Introduction Basic administration

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

Page 33: Oracle Database Administration Introduction Basic administration

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))

)

)

Page 34: Oracle Database Administration Introduction Basic administration

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

Page 35: Oracle Database Administration Introduction Basic administration

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

Page 36: Oracle Database Administration Introduction Basic administration

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

Page 37: Oracle Database Administration Introduction Basic administration

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)

)

)

Page 38: Oracle Database Administration Introduction Basic administration

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) ) )

Page 39: Oracle Database Administration Introduction Basic administration

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) ) )

Page 40: Oracle Database Administration Introduction Basic administration

Database administration

Page 41: Oracle Database Administration Introduction Basic 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

Page 42: Oracle Database Administration Introduction Basic administration

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

Page 43: Oracle Database Administration Introduction Basic administration

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:

connect sys/sys_password as sysdba

Page 44: Oracle Database Administration Introduction Basic administration

Important!• SYSDBA is not database user name – it is a

privilege and special connect mode.

• When connecting to the database:connect sys/sys_password as sysdba

special connection with in SYSDBA mode is requested.

Page 45: Oracle Database Administration Introduction Basic administration

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 user’s data

Page 46: Oracle Database Administration Introduction Basic administration

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.

Page 47: Oracle Database Administration Introduction Basic administration

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)

Page 48: Oracle Database Administration Introduction Basic administration

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

Page 49: Oracle Database Administration Introduction Basic administration

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

Page 50: Oracle Database Administration Introduction Basic administration

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