Monday, 10 November 2014

Oracle 11g Rebuild indexes on partitions using ALTER

To rebuild indexes on partitions for a user in Oracle 11g run -

Select 'ALTER INDEX '|| index_name ||' rebuild partition ' || PARTITION_NAME ||';' from USER_IND_PARTITIONS;

Then run the output from that command

This is a fix for SQL Error: ORA-08102: index key not found, obj#


Thursday, 9 October 2014

Converting Unix time to localtime in UNIX with PERL

Convert Unix time since the Jan 1st 1970 00:00 epoch to local time using PERL with this command :

$ perl -e 'print scalar (localtime(1246130142657481/1000000)),"\n"'
Sat Jun 27 20:15:42 2009


Use gmtime instead of localtime for GMT time.

Friday, 28 February 2014

Creating CSR with openssl

Use openssl to create a Certificate Request - csr

openssl genrsa -out blogspot.co.uk.key 2048
openssl req - new blogspot.co.uk.csr -key blogsport.co.uk.key - config.openssl.cnf

Then print the values to check the csr
openssl req -text -noout -in blogspot.co.uk.csr

Wednesday, 20 November 2013

Oracle OBIEE passing middle-tier user to database

When querying using OBIEE to Oracle database the connection pool username is passed to the database. To also pass the middle-tier user name you need to set the user identifier on the session. TO do this in OBIEE, open the RPD, edit the connection pool settings and create a new connection script to run at connect time.

Add the following line to the connect script -
call dbms_session.set_identifier('VALUEOF(NQ_SESSION.USER)')

This will then be available as the CLIENT_ID attribute when querying session history and also be available in the audit trail on queries.
EG. Select sid, client_identifier from v$session

Tuesday, 10 September 2013

Using SSH to forward ports

Ports on remote machines can be forwarded over a SSH tunnel using the ssh command line tool. Using the ssh config file it is possible to set up many ports to be forwarded at the same time running one command. Putty can be used to achieve similar results on Windows.

To forward a port the syntax is the ssh command followed by the local port then the local host then remote port and host
ssh -L 8080:me.local.org:80 myuser@s1.remote.org

To forward many ports at once created a config file in ~/.ssh/config as follows

host localme
  HostName s1.remote.org
  User myuser
  LocalForward 8080 me.local.org 80
  LocalForward 8443 me.local.org 443

Then run ssh localme and give the password for myuser

Thursday, 11 July 2013

Oracle ORA-02082 creating database link

Creating a database link in oracle gives the error -
create database link orcl connect to myuser identified by oracle using 'orcl';


SQL Error: ORA-02082: a loopback database link must have a connection qualifier
02082. 00000 -  "a loopback database link must have a connection qualifier"
*Cause:    attempt to create a database link with the same name as the
           current database.
*Action:   a loopback database link needs a trailing qualifier, e.g.
           MYDB.DEV.US.ORACLE.COM@INST1  - the '@INST1' is the qualifier

To fix use
create database link orcl@loopback connect to myuser identified by oracle using 'orcl';


Wednesday, 10 July 2013

Querying Oracle for Fine Grained Auditing settings

An earlier post about Fine Grained Auditing shows how to enable FGA in an Oracle 11gR2 database - http://notastrophe.blogspot.com/2013/01/oracle-11gr2-fine-grained-auditing-fga.html . But how do you tell what FGA policies are applied. Typically to query audit settings you query DBA_STMT_AUDIT_OPTS, DBA_PRIV_AUDIT_OPTS and DBA_OBJ_AUDIT_OPTS, and these are shown in OEM's audit page and can be queried from there, but FGA requires a different query -

SELECT PNAME, PCOL,  FROM SYS.FGA$;
Gives the policy name and affected columns.

SELECT DBUID, CLIENTID, POLICYNAME FROM SYS.FGA_LOG$;
Gives audit trail of user, client id and audited policy. Can also get query details and object schema and name

Query against the regular audit log can be done against DBA_AUDIT_TRAIL and SYS.AUD$

Tuesday, 18 June 2013

Oracle Using Query Rewrite for Data Warehouses

Oracle can use computed aggregrates and joins from materialized views to improve performance on a database query by rewriting a query against the warehouse to make use of the view. To allow this to happen the view must support query rewrite and the warehouse itself must support query rewrite. Be aware that the materialized view must be fresh, or the related integrity session parameters relaxed to allow the rewrite to happen.

To enable query rewrite on the view do
CREATE MATERIALIZED VIEW my_view_mv
ENABLE QUERY REWRITE AS
SELECT .....

To enable query rewrite ensure QUERY_REWRITE_ENABLED = TRUE for the session or the system.

Basic query rewrite documentation for Oracle 11g R2
http://docs.oracle.com/cd/E11882_01/server.112/e25554/qrbasic.htm
Advanced query rewrite documentation for Oracle 11g R2
http://docs.oracle.com/cd/E11882_01/server.112/e25554/qradv.htm

Make sure the warehouse supports the creation of the materialized view -
http://docs.oracle.com/cd/E11882_01/server.112/e25554/basicmv.htm
http://docs.oracle.com/cd/E11882_01/server.112/e25554/advmv.htm

Wednesday, 5 June 2013

Oracle Audit Vault Database Firewall 12.1 Errors with network card on Oracle Virtual Machine

To get round a problem with Oracle Audit Vault Database Firewall appliance not finding the network interface after completing the initial configuration it is necessary to change the type of network of the NIC. The problem is seen when AVDF boots for the first time after the install, a screen is shown for selecting a management interface, even though this has been configured already, the NIC hardware address shows as 00:00:00:00:00:00 instead of the valid address. Dropping to a console and searching for the hardware device shows no Ethernet controller. If there are multiple NICs defined they will all be listed as 00:00:00:00:00:00.

For how to debug audit vault in the console look at http://notastrophe.blogspot.com/2013/06/oracle-audit-vault-database-firewall.html .
The command lspci | grep -i Ether xx shows no results, and ifconfig -a shows eth0 with HWADDR of 00:00:00:00:00:00

To get past this problem its necessary to use Fully Virtualized network configuration in OVM for the initial install. Then after the first reboot (just after the IP Config screen), shutdown the VM and change to Paravirtualized networking. Trying to do the install with Paravirtualized networking will fail because the install detects no interfaces. A Fully Virtualized configuration shows on OVM 2.0 as "ioemu" in the Network Interface column on the Network configuration page, Paravirtualized configuration shows as "netfront".

Tuesday, 4 June 2013

Oracle Audit Vault Database Firewall 12.1 run-level hot key

During Oracle Audit Vault Database Firewall 12.1 install you can change from the default interface to view the console (and any error messages) by using a hot-key and another key to get the interface back. From the console you can quit the running service to get to the management interface.
This helps a lot to debug Oracle AVDF install problems or errors.


To change from the interface to the console hit alt+F9
To revert hit alt+F1.
To skip over services that are hung hit ctrl+c from the console.

Once the management interface is up you can drop to a login using ctrl+alt+f2.
Login is root or support. Make sure you've set root and support passwords from the management interface first

Wednesday, 29 May 2013

Automatic Start and Stop of Oracle on LINUX

Here's a script to do automatic start and stop of an Oracle instance running on Linux. Note the change of startup and shutdown for 11gR2. I'm sticking with the dbstart and dbshut method until its removed

http://www.oracle-base.com/articles/linux/automating-database-startup-and-shutdown-on-linux.php

Guest Additions on Oracle VirtualBox

To get guest additions working in VirtualBox hosting Oracle Linux 5 update 8 its necessary to download the kernel-uek-devel and dependent packages. Make sure yum is installed and the repository is downloaded then run yum install kernel-uek-devel.

Kernel Panic - Fatal Exception

After installing Oracle Linux 5 Update 8 in VirtualBox everything worked fine until updating packages. After updating packages and rebooting I got the error message below. To fix the problem I had to change a setting in VirtualBox.

The Error:
Kernel Panic - Fatal Exception

The Fix:
In VirtualBox go to Machine -> Settings -> System.
Then Change chipset to ICH9 and check Enable IO APIC checkbox.

Simple HTTP server in Python

To start a very simple HTTP server on a LINUX machine use Python. Change to the directory that contains files to be served then run the command below. Change the port number as required.

python -m SimpleHTTPServer 8080

Tuesday, 21 May 2013

Specifying comments in SQL to support auditing in Oracle

Adding comments to an SQL statement in Oracle is a good way of providing context to auditing and sql forensic activities directed against an Oracle database audit log. The Oracle Audit trail, and related software like Oracle Audit Vault will capture any comments embedded within an sql statement as part of its normal activities. This, then, provides a way for applications built on Oracle to provide metadata that can assist in analytics performed on the audit record.

As an example, an online shopping cart may audit searches by keyword and may audit individual product pages. At the database level the sql run to generate such pages can be marked with a comment and a textual description of the functionality of the application that uses such a query. This permits application level auditing to be performed in the database, and not require that the auditor is educated in the full set of statements that may be performed by the application. This method of auditing can be done in addition to application level auditing functionality.

Comments are also useful in the enterprise manager to look up statements that are candidates for automated sql tuning.

To add comments to the statement use the following syntax -

SELECT /* [text] */ .....

When the statements require the use of hints ensure the comments do not get in the way of the correct hint definition. Ensure the comments are of the form


{DELETE|INSERT|SELECT|UPDATE} /*+ hint [text] [hint [text]]... */




Finding Day, Month, Year from a date in Oracle SQL using EXTRACT

In Oracle, to get the year, month, day or time from a date its not necessary to parse the date string using the SUBSTR function. Simply use the EXTRACT function and pass in the relevant keyword to get the value as in the following example:

SELECT EXTRACT (year FROM sysdate) "Year" FROM DUAL;

Other keywords that can be passed in are YEAR, MONTH, DAY, HOUR., MINUTE, SECOND. There are also timezone keywords that can be used with timestamps.

The function is useful when used with GROUP BY. For Example:

GROUP BY EXTRACT(month FROM start_date)

Tuesday, 14 May 2013

Setting the value of a sequence in pl/sql in Oracle

In Oracle there is no direct way to set the value of a sequence once the sequence has been created. To set the sequence value to a specific value it is necessary to either drop and recreate the sequence, not forgetting all of the options specified in the create statement, or to increment the sequence by some positive or negative value to set the current value to the desired value.

To find out the current value of a sequence you can use the currval function on the sequence, but this will only return successfully if the sequence has already been initialized with a call to nextval. Another way to find the value for the sequence is to query the database as follows:

select sequence_name, last_number from user_sequences;

Below is sample pl/sql to set a sequence to the desired value, it defines two variables to store the current sequence and the desired sequence then alters the increment to the difference of those, increments the sequence then resets the sequence increment to the previous value. -


  procedure set_seq is
    l_max_row NUMBER(38,0) := 0;
    l_cur_seq NUMBER(38,0) := 0;
  begin

  --- Now change the sequence
   select max(id_col) into l_max_row from my_tab;
   select my_seq.nextval into l_cur_seq from dual;
   execute immediate 'alter sequence my_seq increment by ' || (l_max_row-l_cur_seq) || ' nocache';
   select my_seq.nextval into l_cur_seq from dual;
   execute immediate 'alter sequence my_seq increment by 1 nocache';
   select my_seq.nextval into l_cur_seq from dual;






Wednesday, 27 March 2013

Oracle PL/SQL Merge with sequence

In Oracle 11g it is not possible to use a sequence in the USING clause of a MERGE statement. To get around this limitation enter a dummy integer in the USING clause and specify the sequence in the VALUES clause.

Putting Sequence in the subquery following USING in a MERGE statement gives an error
-- Eg>

MERGE INTO mytab tab
USING (SELECT 1 as col1,  myseq.nextval seq from dual) q1
ON (tab.col1 = q1.col1)  -- The ON clause will take ANDs eg. ON (t1.c1 = t2.c1 AND t1.c2=t2.c2)
WHEN NOT MATCHED THEN
INSERT (
  col1
)
VALUES (
  myseq.nextval
);

Gives

Error report:
SQL Error: ORA-02287: sequence number not allowed here
02287. 00000 -  "sequence number not allowed here"
*Cause:    The specified sequence number (CURRVAL or NEXTVAL) is inappropriate
           here in the statement.
*Action:   Remove the sequence number.

Use a dummy value eg -999 instead

MERGE INTO mytab tab
USING (SELECT 1 as col1, -999 seq from dual) q1
ON (tab.col1 = q1.col1)
WHEN NOT MATCHED THEN
INSERT (
  col1
)
VALUES (
  myseq.nextval
);

Monday, 25 March 2013

Oracle 11G Shrinking Temp Tablespace

There are a few methods to shrink the temporary tablespace in Oracle 11G. In some cases you can directly resize or shrink the tablespace. If that is not possible then create a new temporary tablespace, reassign the default temp space and drop the old tablespace.


SELECT tablespace_name, file_name, bytes
FROM dba_temp_files WHERE tablespace_name like 'TEMP%';


alter database tempfile '/the/full/path/to/temp01.dbf' resize 256M;
alter database tempfile '/the/full/path/to/temp01.dbf' resize 256M

-- Can give -
ERROR at line 1:
ORA-03297: file contains used data beyond requested RESIZE value

-- If that fails, on 11G do -
alter tablespace TEMP shrink space keep 256M;

-- If that fails do

CREATE TEMPORARY TABLESPACE temp2
TEMPFILE '/datapath/temp2_01.dbf' SIZE 5M REUSE
AUTOEXTEND ON NEXT 1M MAXSIZE unlimited
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

 ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2;

DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;

CREATE TEMPORARY TABLESPACE temp
2  TEMPFILE '/datapath/temp01.dbf' SIZE 256M REUSE
3  AUTOEXTEND ON NEXT 128M MAXSIZE unlimited
4  EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp;

DROP TABLESPACE temp2 INCLUDING CONTENTS AND DATAFILES;

From http://stackoverflow.com/questions/1824572/how-to-shrink-temp-tablespace-in-oracle



Friday, 22 March 2013

Disable Password expiry for Oracle Accounts

To disable password expiry for Oracle accounts find whihc profile is in use and then change the default timeout on the profile. If the users are already expired and locked the password must be reset and the account unlocked.


select * from dba_users; -- find the users and which profile they use

-- change the DEFAULT profile to not expire
ALTER PROFILE DEFAULT LIMIT
  PASSWORD_LIFE_TIME UNLIMITED;

-- could also add FAILED_LOGIN_ATTEMPTS UNLIMITED