mercredi 9 novembre 2011

Administration - Session

Show all connected users
set lines 100 pages 999
col ID format a15
select username
,      sid || ',' || serial# "ID"
,      status
,      last_call_et "Last Activity"
from   v$session
where  username is not null
order by status desc
,        last_call_et desc
/ 
 
Time since last user activity
set lines 100 pages 999
select username , floor(last_call_et / 60) "Minutes" , status from v$session where username is not null order by last_call_et
/

Sessions sorted by logon time
set lines 100 pages 999
col ID  format a15
col osuser format a15
col login_time format a14
select  username
, osuser
, sid || ',' || serial# "ID"
, status
, to_char(logon_time, 'hh24:mi dd/mm/yy') login_time
, last_call_et
from v$session
where username is not null
order by login_time
/ 
 
Show user info including os pid
col "SID/SERIAL" format a10
col username format a15
col osuser format a15
col program format a40
select s.sid || ',' || s.serial# "SID/SERIAL" , s.username , s.osuser , p.spid "OS PID" , s.program from v$session s , v$process p Where s.paddr = p.addr order by to_number(p.spid)
/
Show a users current sql
Select sql_text
from   v$sqlarea
where  (address, hash_value) in
(select sql_address, sql_hash_value 
        from v$session
        where username like '&username')
/ 
Session status associated with the specified os process id
select s.username , s.sid , s.serial# , p.spid , last_call_et , status from V$SESSION s , V$PROCESS p where s.PADDR = p.ADDR and p.spid='&pid'
/

 
All active sql
set feedback off
set serveroutput on size 9999
column username format a20
column sql_text format a55 word_wrapped
begin
  for x in
   (select username||'('||sid||','||serial#||') ospid = '|| process ||
    ' program = ' || program username,
    to_char(LOGON_TIME,' Day HH24:MI') logon_time,
    to_char(sysdate,' Day HH24:MI') current_time,
    sql_address,
    sql_hash_value
   from v$session
   where status = 'ACTIVE'
   and rawtohex(sql_address) <> '00'
   and username is not null ) loop
   for y in (select sql_text
   from v$sqlarea
   where address = x.sql_address ) loop
   if ( y.sql_text not like '%listener.get_cmd%' and
    y.sql_text not like '%RAWTOHEX(SQL_ADDRESS)%' ) then
    dbms_output.put_line( '--------------------' );
    dbms_output.put_line( x.username );
    dbms_output.put_line( x.logon_time || ' ' || x.current_time || ' SQL#=' || x.sql_hash_value);
    dbms_output.put_line( substr( y.sql_text, 1, 250 ) );
   end if;
  end loop;
 end loop;
end;
/
 
Display any long operations
set lines 100 pages 999
col username format a15
col message format a40
col remaining format 9999
select username
, to_char(start_time, 'hh24:mi:ss dd/mm/yy') started
, time_remaining remaining
, message
from v$session_longops
where time_remaining = 0
order by time_remaining desc
/ 
List open cursors per user
set pages 999
select     sess.username
,    sess.sid
,    sess.serial#
,    stat.value cursors
from    v$sesstat stat
,    v$statname sn
,    v$session sess
where    sess.username is not null
and    sess.sid = stat.sid
and    stat.statistic# = sn.statistic#
and    sn.name = 'opened cursors current'
order by value
/

Administration - Status

Startup time
select to_char(startup_time, 'HH24:MI DD-MON-YY') "Startup time"
from v$instance
/
How large is the database
col "Database Size" format a20
col "Free space" format a20
col "Used space" format a20
select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ' GB' "Database Size" , round(sum(used.bytes) / 1024 / 1024 / 1024 ) - round(free.p / 1024 / 1024 / 1024) || ' GB' "Used space" , round(free.p / 1024 / 1024 / 1024) || ' GB' "Free space" from (select bytes from v$datafile union all select bytes from v$tempfile union all select bytes from v$log) used , (select sum(bytes) as p from dba_free_space) free group by free.p
/

Distribution of objects and data
Which schemas are taking up all of the space

set pages 999
col "size MB" format 999,999,999
col "Objects" format 999,999,999
select obj.owner "Owner"
, obj_cnt "Objects"
, decode(seg_size, NULL, 0, seg_size) "size MB"
from  (select owner, count(*) obj_cnt from dba_objects group by owner) obj
, (select owner, ceil(sum(bytes)/1024/1024) seg_size
 from dba_segments group by owner) seg
where  obj.owner  = seg.owner(+)
order by 3 desc ,2 desc, 1
/ 
 
Show the ten largest objects in the database
col owner format a15
col segment_name format a30
col segment_type format a15
col mb format 999,999,999
select owner , segment_name , segment_type , mb from ( select owner , segment_name , segment_type , bytes / 1024 / 1024 "MB" from dba_segments order by bytes desc ) where rownum < 11
/

Display character set information
select * from nls_database_parameters
/ 
 
Show all used features
select name , detected_usages from dba_feature_usage_statistics where detected_usages > 0 /
 

 

mardi 19 octobre 2010

How to Use DVSYS.DBMS_MACADM procedures to Prohibit Data Access to Users

A realm is created on HR schema objects : how to prohibit the access to HR objects to the accounts SYSTEM and SYS without using the Database Vault Administrator GUI tool (dva) ?

1. Realm creation and schema objects selection :
SQL> execute dvsys.dbms_macadm.CREATE_REALM('Test HR','Protects data from DBA access','Y', 1);

SQL>  execute DVSYS.DBMS_MACADM.ADD_OBJECT_TO_REALM ( 'Test HR', 'HR', '%', '%');

or
begin
dvsys.dbms_macadm.add_object_to_realm ( realm_name => 'Test HR'  , object_owner => 'HR'  ,
object_name => '%'  ,
object_type => '%');
end;
/

2. Tests : connect as SYSTEM user to check that SYSTEM user has no access to HR data :
SQL> conn system/manager
Connected.

SQL> select * from HR.dept;

DEPTNO DNAME LOC
----------   -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO

Connect as SYS user to check that SYS user has no access to HR data :
SQL> conn sys/xxx as sysdba
Connected

SQL> select * from HR.dept;

select * from HR.dept
*
ERROR at line 1:
ORA-01031: insufficient privileges

Solution


1. Check  if the user SYSTEM user has been granted the object privilege SELECT on HR.DEPT  table.

Yes , it had been granted. Hence the realm does not prohibit the SYSTEM user from selecting data from HR table as Realm only protects data from being accessed with system privileges.

2. Revoke the object privilege on HR schema tables from SYSTEM user.
SQL> conn sys/xxx as sysdba
Connected.

SQL> revoke select on hr.dept from system;
Revoke succeeded.

SQL> conn dvowner/xxx
Connected.

SQL> execute dvsys.dbms_macadm.CREATE_REALM('Test HR','Protects data from DBA access','Y', 1);
PL/SQL procedure successfully completed.

SQL> execute DVSYS.DBMS_MACADM.ADD_OBJECT_TO_REALM ('Test HR', 'HR', '%', '%');
PL/SQL procedure successfully completed.

SQL> conn system/xxx
Connected.

SQL> select * from HR.dept;
select * from HR.dept
*
ERROR at line 1:
ORA-01031: insufficient privileges

user_has_auth_in_realm

jeudi 14 octobre 2010

How to Drop and Recreate the DB Control Repository

Information in this article applies to:
Oracle Enterprise Manager 10g Release 1 (10.1)
Oracle Enterprise Manager 10g Release 1 (10.2)
So:

Goal:
Provide detailed instructions on how to remove and recreate the DB Control Repository in a 10g Database

1. Stop the DB Console if not already stoped: $ORACLE_HOME/bin/emctl stop dbconsole

On Unix Systems:
1.  Run 10g DB $OH/sysman/admin/emdrep/bin/RepManager
NOTE: Type RepManager by itself and you will get a list of options available for the tool

2.  After you delete the Repository, you will need to remove the configuration files.  To do this, run emca -x

3.  Finally, to created both the schema objects and configuration files run emca and answer the prompts

On Windows Systems:
NOTE: The RepManager script in 10.1.0.2.0 does not work correctly on Windows.  For this reason, you will have to manually drop the sysman schema and management objects.  The RepManager script should be fully functional in the 10.1.0.3.0 release.

1.  Logon SQLPLUS as user SYS or SYSTEM, and drop the sysman account and mangement objects:

•    SHUTDOWN IMMEDIATE;

•    STARTUP RESTRICT;

•    EXEC sysman.emd_maintenance.remove_em_dbms_jobs;

•    EXEC sysman.setEMUserContext(”,5);

•    REVOKE dba FROM sysman;

•    DECLARE
•        CURSOR c1 IS
•          SELECT owner, synonym_name name
•          FROM   dba_synonyms
•          WHERE  table_owner = ‘SYSMAN’
•        ;
•      BEGIN
•        FOR r1 IN c1 LOOP
•            IF r1.owner = ‘PUBLIC’ THEN
•               EXECUTE IMMEDIATE ‘DROP PUBLIC SYNONYM ‘||r1.name;
•            ELSE
•               EXECUTE IMMEDIATE ‘DROP SYNONYM ‘||r1.owner||’.'||r1.name;
•            END IF;
•        END LOOP;
•      END;

•    DROP USER mgmt_view CASCADE;

•    DROP ROLE mgmt_user;

•    DROP USER sysman CASCADE;

•    ALTER SYSTEM DISABLE RESTRICTED SESSION;

2.  At a command Prompt, run emca -x sid to remove the existing configuration files and then recreate the schema and configuration files with just emca.  For example:
emca -x <SID>
emca

For additional information on emca commandline options, please see the Oracle Enterprise Manager 10g Advanced Configuration Guide.  The Guide is available from OTN.  http://download-west.oracle.com/docs/pdf/B12013_01.pdf

1.    Logon SQLPLUS as user SYS or SYSTEM, and drop the sysman account and mangement objects:

SHUTDOWN IMMEDIATE;
STARTUP RESTRICT;
A. Drop the repository manually using following procedure.

Stop the DB Console if not already stoped: $ORACLE_HOME/bin/emctl stop dbconsole
Log on in sqlplus as sys user and run the following commands:

a. drop user sysman cascade;
b. drop role MGMT_USER;
c. drop user MGMT_VIEW cascade;
d. drop public synonym MGMT_TARGET_BLACKOUTS;
e. drop public synonym SETEMVIEWUSERCONTEXT;
(Note If any of the above statement failed and saying object doesn’t exist just skip that
statement)

B. Drop the DB Control configuration files.

Remove the following directories:
$rm -rf $ORACLE_HOME/<HOSTNAME_SID>
$rm -rf $ORACLE_HOME/oc4j/j2ee/OC4J_DBConsole_<HOSTNAME_SID>

On Windows systems just delete the folders above from the Explorer.

Remove the entries for the old dbconsole information from the $ORACLE_HOME/install/portlist.ini file

On Windows, use delsrv.exe (available for free download from Microsoft) or the ‘sc’ command to remove the ORACLEDBCONSOLE<SID> service if it still remains.

1.    Remove the current Database Control configuration files and repository

1.1 set ORACLE_HOME and ORACLE_SID environment variable
1.2 <ORACLE_HOME>/bin/emca -x <SID>
1.3 <ORACLE_HOME>/sysman/admin/emdrep/bin/RepManager <hostname> <actual_listener_port> <sid> -action drop
1.4 Manually delete any <hostname>_<sid> directories in <ORACLE_HOME> and <ORACLE_HOME>/oc4j/j2ee
1.5 On Windows, use delsrv.exe (available for free download from Microsoft) or the ‘sc’ command to remove the ORACLEDBCONSOLE<SID> service if it still remains.

2. Recreate the Database Control configuration files and repository

2.1 <ORACLE_HOME>/bin/emca

vendredi 8 octobre 2010

Exploring the Oracle Database Architecture

Explain the Memory Structures


  • System Global Area (SGA) - The SGA is a group of shared memory structures, known as SGA components, that contain data and control information for one Oracle Database instance. The SGA is shared by all server and background processes. Examples of data stored in the SGA include cached data blocks and shared SQL areas.

  • Program Global Area (PGA) - A PGA is a nonshared memory region that contains data and control information exclusively for use by an Orcle process. The PGA is created by the Oracle Database when an Oracle process is started.

    One PGA exists for each server process and background process. The collection of individual PGAs is the total instance PGA, or instance PGA. Database initialization parameters set the size of the instance PGA, not individual PGAs.

  • User Global Area (UGA) - The UGA is memory associated with a user session.

  • Software Code Areas - Software code areas are portions of memory used to store code that is being run or can be run. Oracle Database code is stored in a software area that is typically at a different location from user programs-a more exclusive or protected location.



Describe the Process Structures


  • Client processes run the application or Oracle tool code

  • Oracle Processes run the Oracle Database code. Oracle processes include the following subtypes:

    • Background Processes start with the database instance and perform maintenance tasks such as performing instance recovery, cleaning up processes, writing redo buffers to disk, and so on.

    • Server Processes perform work based on a client request.

      For example, these processes parse SQL queries, place them in the shared pool, create and execute a query plan for each query, and read buffers from the database buffer cache or from disk.

    • Slave Processes perform additional tasks for a background or server process.





Overview of Storage Structures


  • Physical

    • Data Files and Temp Files - A data file is a physical file on disk that was created by Oracle Database and contains data structures such as tables and indexes. A temp file is a data file that belongs to a temporary tablespace. The data is written to these files in an Oracle proprietary format that cannot by read by other programs.

    • Control Files - is a root file that tracks the physical components of the database.

    • Online Redo Log - is a set of file containing records of changes made to data.



  • Logical

    • Data Blocks - Oracle Database manages the logical storage space in the data files of a database in units called data blocks, also called logical blocks, Oracle blocks, or pages. An Oracle data block is the minimum unit of database I/O.

    • Extents - is a logical unit of database storage space allocation made up of contiguous data blocks. Data blocks in an extent are logically contiguous but can be physically spread out on disk because of RAID striping and file system implementations.

    • Segments - is a seg of extents that contains all the data for a logical storage structure within a tablespace. For example, Oracle Database allocates one or more extents to form the data segment for a table. The database also allocates one or more extents to form the index segment for a table.

    • Tablespaces - is a logical storage container for segments. Segments are database objects, such as tables and indexes, that consume storage space. At the physical level, a tablespace stores data in one or more data files or temp files. A database must have the SYSTEM and SYSAUX tablespaces.




mardi 13 juillet 2010

Error 1044 in MySQL: Access denied when using LOCK TABLES



I got an error while using mysqldump

mysqldump: Got error: 1044: Access denied for user x@y to database z when using LOCK TABLES

To solve this problem, either ask you administrator to grant you the lock privileges, or use the following command instead.

mysqldump -u username -p database --single-transaction >dump.sql

vendredi 11 juin 2010

ORA-08102: index key not found, obj# ... Solution to resolve

When updated table, found error:


ORA-08102: index key not found, obj# 116528, file 96, block 8795 (2)

This problem I had resolved by rebuild indexes (ORA-08102 on index objects). But today I can not use this solution:


What is about this error?


Error: ORA-08102

Oracle10g Message
Text: index key not found, obj# %s, file %s, block %s (%s)
Oracle 9.2 or Earlier Error Message
Text: index key not found, obj# %s, dba %s (%s)

ORA-08102 is a mismatch between the key(s) stored in the index and the values stored in the table.

What typically happens is the index is built and at some future time, some type of corruption occurs, either in the table or index, to cause the mismatch.

When know about is, what is the way to resolve it? How?

-> Check object, that have the problem, if it's index... try to rebuild:

SQL>select object_name, object_type
from dba_objects
where object_id = [obj# in ORA-08102]

Example:

SQL> select object_name, object_type
from dba_objects
where object_id = 116528;

OBJECT_NAME OBJECT_TYPE
--------------- -------------------
XXX_IDX07 INDEX PARTITION

And then rebuild it.

SQL> alter index [index_name] rebuild [online];

OR "partition index"

SQL> select partition_name from dba_segments where TABLESPACE_NAME = (select tablespace_name from dba_data_files where file_id=[file_id from ORA-08102] ) and SEGMENT_NAME=[object_name from dba_objects];

SQL> select partition_name from dba_segments where TABLESPACE_NAME = (select tablespace_name from dba_data_files where file_id=96) and SEGMENT_NAME='XXX_IDX07';

PARTITION_NAME
------------------------------
XXX_PART1

SQL> alter index [index_name] rebuild partition [partition_name] [online];

if ORA-08102 on table objects -> resolve by "analyze table ... validate structure.. or use dbv to check corrupt on table" ) don't forget check error on OS , trace file and alert log file.

-> (ORA-08102 on index objects), if rebuilt indexes, But still error ORA-08102: Drop that index and recreate it.