Thứ Năm, 22 tháng 8, 2013

FLASHBACK DATABASE TO SCN

FLASHBACK DATABASE TO SCN

FLASHBACK DATABASE TO SCN
Time to FLASHBACK DATABASE TO – 06:30 a.m on 31 July.
Get oldest flashback time –
SELECT OLDEST_FLASHBACK_SCN,to_char(OLDEST_FLASHBACK_TIME,'DD-MON-YYYY HH24:MI:SS') FROM V$FLASHBACK_DATABASE_LOG;
If there are logs up until 06:30 on 31 Jul, you can flashback.
Get the SCN number to flashback to -
col first_change# format 99999999999
select first_change# ,to_char(first_time,'DD-MON-YYYY HH24:MI:SS'),sequence# from V$LOG_HISTORY ORDER BY 1 ;
23856899540 31-JUL-2009 04:30:09 119695
23856965501 31-JUL-2009 06:30:10 119696
23857031498 31-JUL-2009 08:30:09 119697
23859233947 31-JUL-2009 10:00:29 119704
23859242647 31-JUL-2009 10:31:34 119705
Note down the SCN at the time you want to flashback to which is the FIRST_CHANGE# in the V$LOG_HISTORY view –
Example here the SCN is 23856965501 at 06:30 – the time you want to flashback to.
Restore a few archivelogs prior to and after the flashback time – the log sequence is in the last column above….
Ex – to restore archivelogs from 119695 to 119702
rman target / catalog rman/rman@rman
RMAN > restore archivelog from sequence 119695 until sequence 119705;
Once the logs are restored make sure no automatic RMAN backup job of archivelogs run which delete old archivelogs from disk -
Next flashback the database –
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
FLASHBACK DATABASE TO SCN 23856965501;
Once FLASHBACK is complete-
Verify the data before opening with a resetlogs -
ALTER DATABASE OPEN READ ONLY;
If satisfied with the data -
Shutdown immediate;
Startup mount ;
Alter database open resetlogs;

Using FLASHBACK to rollback a TRUNCATE

Using FLASHBACK to rollback a TRUNCATE

This scenario will show how we can use a combination of FLASHBACK database and also recovery to take a database back in time to undo a TRUNCATE operation and then roll forward the database after the flashback operation to bring it to the current point in time.
INSERT ROWS INTO TABLE MYOBJ – THIS WILL BE TRUNCATED
SQL> insert into scott.myobj select * from all_objects;

50496 rows created.

SQL> /

50496 rows created.

SQL> select count(*) from scott.myobj;

  COUNT(*)
----------
    100992
OBTAIN THE CURRENT SCN – FLASHBACK WILL HAPPEN TO THIS SCN
SQL> select current_scn from v$database;


          CURRENT_SCN
---------------------
          15633908021

TRUNCATE THE TABLE

SQL> truncate table scott.myobj;

Table truncated.

SQL> select count(*) from scott.myobj;

  COUNT(*)
----------
         0

AT THE SAME TIME OTHER CHANGES ARE HAPPENING IN THE DATABASE AND THESE CHANGES WILL BE RECOVERED AFTER THE FLASHBACK IS DONE

SQL> insert into scott.myobj2 select * from scott.myobj2;

356874 rows created.

SQL> /

713748 rows created.

SQL> commit;

Commit complete.
SHUTDOWN THE DATABASE AND PERFORM THE FLASHBACK TO THE SCN BEFORE THE TRUNCATE WAS DONE
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area  469762048 bytes
Fixed Size                  2084880 bytes
Variable Size             377491440 bytes
Database Buffers           83886080 bytes
Redo Buffers                6299648 bytes
Database mounted.



SQL> FLASHBACK DATABASE TO SCN 15633908021;

Flashback complete.

OPEN THE DATABASE IN READ ONLY MODE AND EXPORT THE TABLE THAT WAS TRUNCATED EARLIER. THIS TABLE WILL BE IMPORTED AFTER THE RECOVERY IS DONE
SQL> alter database open read only;

Database altered.


SQL>  select count(*) from scott.myobj;

  COUNT(*)
----------
         100992

SQL> quit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
crashdb:/u03/oradata/crashdb/arch> exp file=scott.dmp tables=myobj

Export: Release 10.2.0.4.0 - Production on Fri Feb 6 09:53:00 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Username: scott
Password:

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                          MYOBJ          100992 rows exported
Export terminated successfully without warnings.
NOW SHUTDOWN THE DATABASE,STARTUP MOUNT AND PERFORM THE RECOVERY
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area  696254464 bytes
Fixed Size                  2086616 bytes
Variable Size             184551720 bytes
Database Buffers          503316480 bytes
Redo Buffers                6299648 bytes
Database mounted.
SQL> recover database;
Media recovery complete.
SQL> alter database open;

Database altered.

AS EXPECTED TABLE MYOBJ WHICH WAS TRUNCATED NOW AGAIN HAS 0 ROWS AFTER THE RECOVERY – WE CAN IMPORT THE DUMP WE TOOK AFTER THE FLASHBACK
SQL> select count(*) from scott.myobj;

  COUNT(*)
----------
         0
CONFIRM THAT OTHER COMMITTED CHANGES IN THE DATABASE HAVE BEEN RECOVERED
SQL> select count(*) from scott.myobj2;

  COUNT(*)
----------
  713748

Thứ Bảy, 17 tháng 8, 2013

Duplicate database bằng rman activity



Duplicate database bằng rman activity
Phạm Văn Hiếu
DBA

Yêu cầu:
Máy orcl. Đã có Oracle Software DB  và Database.
Máy TEST. Đã cài Oracle Software DB.
Nhiêm vụ:
Duplicate từ máy orcl : IP 192.168.159.50

Đến máy Test : IP 192.168.159.201


Các bước thực hiện:
Bước 1:
Tạo tnsnames.ora trên 2 máy có thông tin của máy kia:
Vi tnsnames.ora


ORCL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.159.50)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
      (SERVER = DEDICATED)
    )
  )

TEST =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.159.201)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = TEST)
      (SERVER = DEDICATED)
    )
  )


Bước 2:  
·         Trên máy orcl: Listener đã chạy chỏ vào chính nó
Listener.ora có nội dung như sau:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = orcl)
      (ORACLE_HOME = /home/oracle/app/oracle/product/11.2.0/dbhome_1)
      (SID_NAME = orcl)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ora.local)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

Khởi động lsnrctl start lên.
·         Tạo Listener trên máy TEST và khởi chạy listener:
Tạo file Listener.ora có nội dung như bên dưới:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = TEST)
      (ORACLE_HOME = /export/home/oracle/app/oracle/product/11.2.0/dbhome_1)
      (SID_NAME = TEST)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

Khởi động lsnrctl start lên.
Bước 3:
Tạo pfile, orapw từ máy orcl copy sang vị trí tương ứng ở máy TEST
Bước 4:
·         Orcl instance đang chạy
·         TEST Khởi động ở chế độ nomount:
Mở cửa sổ xshell kết nối vào TEST bằng user oracle:
                $export ORACLE_SID=TEST
                $sqlplus / as sysdba
                Sqlplus> startup nomount pfile=’/tmp/pfile.ora’
Mở cửa sổ khác xshell kết nối vào TEST bằng user oracle:
                $rman
                RMAN> connect target sys/sys123@orcl
RMAN> connect auxiliary sys/sys123@TEST
RMAN> duplicate target database to 'TEST' from active database db_file_name_convert '/home/oracle/app/oracle/oradata/orcl','/export/home/oracle/app/oracle/oradata/orcl';
Đến đây rman tự động Duplicate database nếu không có lỗi thì việc duplicate sẽ thành công. Nếu có lỗi thì kiểm tra lại và làm lại từ đầu.
Bước 5:
Tạo spfileTEST.ora Tại máy TEST:
$sqlplus / as sysdba
Sqlplus> create pfile=’/tmp/pfileTEST.ora’ from memory;
Sqlplus>shutdown immediate;
Sqlplus>exit
$sqlplus / as sysdba
$startup pfile=’/tmp/pfileTEST.ora’;
Sqlplus> create spfile=’ /export/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs/spfileTEST.ora’ from pfile=’/tmp/pfileTEST.ora’;

-----------------XONG----------------




Thứ Năm, 15 tháng 8, 2013

How to Setup the Oracle Wallet for Encryption Functions




Step 1: Configure Networking

Add the following entry to your
$TNS_ADMIN/sqlnet.ora changing the directory to a path relevant to your installation.
[/u03/app/oracle/product/db/11.1.0.6/network/admin nf@rac2]$ cd $TNS_ADMIN
[/u03/app/oracle/product/db/11.1.0.6/network/admin nf@rac2]$ more sqlnet.ora
ENCRYPTION_WALLET_LOCATION=(SOURCE=(METHOD=file)(METHOD_DATA=(DIRECTORY=/u03/app/oracle/oradata/nf/wallet)))

Step 2: Create the wallet via your preferred method (I use Oracle Wallet Manager)

Launch the Oracle Wallet Manager:
[/u03/app/oracle/product/db/11.1.0.6/bin nf@rac2]$ export DISPLAY=192.168.1.104:0.0
[/u03/app/oracle/product/db/11.1.0.6/bin nf@rac2]$ ./owm

Here are the screenshots for this step:

Create a new wallet

http://www.colestock.com/img/owm/1.gif

Enter the password

http://www.colestock.com/img/owm/2.gif

Specify the location

http://www.colestock.com/img/owm/3.gif

Set as an Auto-login wallet and save

http://www.colestock.com/img/owm/4.gif

Should yield the following files:
[/u03/app/oracle/product/db/11.1.0.6/bin nf@rac2]$ ls -lart /u03/app/oracle/oradata/nf/wallet
total 24
drwxr-x---  6 oracle dba 4096 Feb 11 00:56 ..
-rw-------  1 oracle dba 7312 Feb 11 00:56 ewallet.p12
drwx------  2 oracle dba 4096 Feb 11 00:56 .
-rw-------  1 oracle dba 7340 Feb 11 00:56 cwallet.sso

Step 3 Create Master Key for TDE:
SQL> alter system set encryption key identified by "password";
--Password saved to wallets Manager.

System altered.

Note that the Oracle wallet manager does not create this master key. The above is the appropriate method for doing so.

Step 4 Verify wallet is open
SQL> col wrl_parameter format a40
SQL> select * from v$encryption_wallet;

WRL_TYPE             WRL_PARAMETER                            STATUS
-------------------- ---------------------------------------- ---------
file                 /u03/app/oracle/oradata/nf/wallet        OPEN

You should see that the size of the auto-login and encryption wallet have changed (since the master key has been added):
[/u03/app/oracle/oradata/nf/wallet nf@rac2]$ ls -alrt /u03/app/oracle/oradata/nf/wallet
total 32
drwxr-x---  6 oracle dba 4096 Feb 11 00:56 ..
drwx------  2 oracle dba 4096 Feb 11 00:56 .
-rw-------  1 oracle dba 8453 Feb 11 01:24 ewallet.p12
-rw-------  1 oracle dba 8481 Feb 11 01:24 cwallet.sso

If the wallet is not open:
SQL> alter system set encryption wallet open identified by "password";
System altered.

At this point, you should be able to transparently encrypt tablespaces, columns, LOBS, etc.

Frequently asked questions about TDE: 10g

11g Tablespace Encryption: Three easy steps

ALTER SYSTEM SET ENCRYPTION KEY
certificate_ID
IDENTIFIED BY "
password
";
ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "
password
";

ALTER SYSTEM SET ENCRYPTION WALLET CLOSE IDENTIFIED BY "
password
";

SELECT * FROM V$ENCRYPTION_WALLET;
SELECT * FROM DBA_ENCRYPTED_COLUMNS;

SELECT TABLESPACE_NAME, ENCRYPTED FROM DBA_
TABLESPACES;
SELECT * FROM V$ENCRYPTED_TABLESPACES;




Chủ Nhật, 11 tháng 8, 2013

ORA-01034 ORA-27101

ORA-01034 & ORA-27101: Shared Memory Realm Does Not Exist

ORA-27101 and ORA-01034  combined may occurs in various scenarios .One of the reason for this error is that the database is not up. When we try to connect with oracle database or sometimes during installation we may get this error . We may face this error in some other scenario's too .  Here are few possible scenario's (  some scenario's are from window platform and some are from Linux  platform ) .


Case 1 :  When try to connect with normal user we get the error as

C:\>sqlplus
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 6 11:22:08 2012
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
Enter user-name: scott
Enter password:
ERROR:
ORA-01034: ORACLE not available
ORA-27101:  shared memory realm does not exist
Process ID: 0
Session ID: 0 Serial number: 0

To solve this issue , set the ORACLE_SID and start the database using the "sys" user as sysdba and start the database ,

C:\>set ORACLE_SID=noida
C:\>sqlplus sys/xxxx@noida as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Mon Feb 6 11:25:13 2012
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
Connected to an idle instance.
SQL> startup
ORACLE instance started.

Total System Global Area  535662592 bytes
Fixed Size                  1375792 bytes
Variable Size             331350480 bytes
Database Buffers          197132288 bytes
Redo Buffers                5804032 bytes
Database mounted.
Database opened.

Case  2 :  On a system with multiple IP addresses, when we connect with sqlplus locally on the server  (i.e. "sqlplus user/password") everything is OK, but connecting through a TNS alias, either from the network or locally on the server, we get the the following errors :  

$ sqlplus user/password@db_alias
ERROR:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
SVR4 Error: 2: No such file or directory

We need to Manually startup the database or check whether the listener is started or not . So either connect as
$export ORACLE_SID=noida 
$ sqlplus sys/password as sysdba 
SQL>startup 
or
check the status of listener if not started then start the listener  as
$lsnrctl
lsnrctl> stop
lsnrctl>start
lsnrctl>exit
$export ORACLE_SID=noida
$sqlplus sys/password@noida as sysdba
SQL> startup


Case 3 :  Make sure while connecting, that the ORACLE_SID and ORACLE_HOME is correctly set . There should not be trailing trash in ORACLE_HOME  path . Remove the extra "/" from the end of ORACLE_HOME

Incorrect Home location:    (ORACLE_HOME = /u01/app/oracle/product/11.2.0/)
Correct Home location :     (ORACLE_HOME = /u01/app/oracle/product/9.2.0)

Once , we have correctly set  the path  then  reload  the listener and  set or export  ORACLE_SID  before connecting as :
Linux  : 
$export ORACLE_SID=noida 
$sqlplus sys/xxxx@noida as sysdba
SQL> startup


Window  : 
C:\> set ORACLE_SID=noida
C:\> sqlplus sys/xxxx@noida as sysdba
C:\> startup 


Case 4 :   Sometimes in case of window ,  if event log is full , then we get this error . So delete the event logs and try to connect again .

Case 5 :  Sometimes , in case of window , we resolve this issue by simply restart the oracle services . so restart the window services as
start --> cmd  --- > net stop oracleserviceXXXX
--- >> net start oracleservice .


Enjoy     :-)

Thứ Bảy, 3 tháng 8, 2013

Delete a node from Oracle DB 11g2 RAC

I tested this Case successfully. Following these steps:

.
---------------
Oracle Software:
_______________

node2 run dbca -> cluster database -> instance management -> delete instance node3
node3 cd $ORACLE_HOME/oui/bin
./runInstaller -updateNodeList    ORACLE_HOME=/export/home/oracle/app/oracle/product/11.2.0/dbhome_1 "CLUSTER_NODES={node3}" -local
node3 cd $ORACLE_HOME/deinstall
./deinstall -local
node2 cd $ORACLE_HOME/oui/bin
./runInstaller -updateNodeList ORACLE_HOME=/export/home/oracle/app/oracle/product/11.2.0/dbhome_1 "CLUSTER_NODES={node2}"

---------------------
Oracle Grid Software:
____________________

node3 su - root
cd $GRID_HOME/bin
./crsctl unpin css -n node3
cd $GRID_HOME/crs/install
./rootcrs.pl -deconfig -force

node2 su - root
cd $GRID_HOME/bin
./crsctl delete node -n node3

node3 su - oracle (grid user if exist)
cd $GRID_HOME/oui/bin
./runInstaller -updateNodeList ORACLE_HOME=/export/home/oracle/grid "CLUSTER_NODES={node3}" CRS=TRUE -local
cd $GRID_HOME/deinstall
./deinstall -local

answer question
run commands from /tmp
enter to complete
node2 su - grid
cd $GRID_HOME/oui/bin
./runInstaller -updateNodeList ORACLE_HOME=/export/home/oracle/grid "CLUSTER_NODES={node2}" CRS=TRUE

Check Node3 was removal
#olsnodes -n -t
#cluvfy stage -post nodedel -n node3