Views

Wednesday, September 27, 2017

Oracle - Creating the Recovery Catalog

Creating the Recovery Catalog


Connect to Catalog database.

[oracle@localhost dbs]$ export ORACLE_SID=CATDB

[oracle@localhost dbs]$ sqlplus / as sysdba

Create Catalog tablespace in catalog database

SQL> create tablespace tbs_catalog  datafile '/u01/app/oracle/oradata/DUPDB1/catalog.dbf'
     size 10M autoextend on extent management local uniform size 1M;

Create Recovery Catalog owner

SQL> create user rman identified by oracle default tablespace tbs_catalog quota unlimited on tbs_catalog;

Grant recovery catalog owner role which contains list of all privileges required.

SQL> grant recovery_catalog_owner to rman;

SQL> exit

Connect to target database and catalog database through RMAN

[oracle@localhost dbs]$ export ORACLE_SID=ORADB1
[oracle@localhost dbs]$ rman target / catalog rman/oracle@CATDB

Recovery Manager: Release 11.2.0.4.0 - Production on Thu Sep 28 11:39:44 2017

connected to target database: ORADB1 (DBID=2294695355)
connected to recovery catalog database

Create Recovery Catalog.

RMAN> create catalog ;

recovery catalog created

Register target database to recovery catalog.

RMAN> register database;

database registered in recovery catalog
starting full resync of recovery catalog
full resync complete

Configure rman parameters

RMAN> configure default device type to disk;
RMAN> configure retention policy to recovery window of 2 days;
RMAN> configure controlfile autobackup on;
RMAN> configure controlfile autobackup format for device type disk to '/u01/backup/rman/ctl_%F';
RMAN> configure channel device type disk format '/u01/backup/rman/Full%d_DB_%U_%S_%P';
RMAN> configure device type disk backup type to compressed backupset parallelism 2;'



Oracle - How to Change/Reset SYS password

How to Change/Reset SYS password

You can reset/change sys password for Oracle database using ORAPWD utility.

[oracle@localhost dbs]$ sqlplus / as sysdba

Check the value for parameter remote_login_passwordfile. If remote_login_passwordfile is EXCLUSIVE, then database needs to be shut down.

SQL> show parameter remote_log

NAME                            TYPE     VALUE
------------------------------------ ----------- ------------------
remote_login_passwordfile       string   EXCLUSIVE

Shutdown database.

SQL> shut immediate;

Remove old password file.

[oracle@localhost dbs]$ rm orapwCATDB*.*

Create new passwordfile using ORAPWD utility.

[oracle@localhost dbs]$ orapwd file=/u01/app/oracle/product/11.2.0/db_1/dbs/orapwCATDB password=oracle entries=10

Password is ready to use.

[oracle@localhost dbs]$ sqlplus sys/oracle@catdb as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 27 16:54:25 2017
Connected to an idle instance.

SQL> startup
ORACLE instance started.

Database mounted.
Database opened.

For further help:

How To Recover From Lost SYS Password MOSC Note:  805084.1


Oracle - How to rename an Oracle database ORACLE_SID

How to rename an Oracle database ORACLE_SID

We can change database name using DBNEWID utility nid.

Steps Outlined:
               
1.       Start the database in mount mode
2.       Invoke nid command using user having sysdba privilege. Substitute DBNAME with new database name.
3.       Shutdown database.
4.       Change db_name=CATDB in initDUPDB1.ora
5.       Start database in mount mode. Ignore the error.
6.       Shutdown database.
7.       Create new password file for new database name.
8.       Set ORACLE_SID to new value.
9.       Rename the SPFILE or pfile whichever you are using
10.   Alter the listener.ora and tnsnames.ora with new database SID
11.   Reload listener to affect the change.
12.   Start the database in mount mode.
13.   Open database in resetlogs mode.
14.   Verify whether dbname and instance name has changed.

Start the database in mount mode

[oracle@localhost dbs]$ . oraenv
ORACLE_SID = [ORADB1] ? DUPDB1
The Oracle base remains unchanged with value /u01/app/oracle
[oracle@localhost dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 27 13:42:35 2017

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

Connected to an idle instance.

SQL> startup mount
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size               2253824 bytes
Variable Size          314575872 bytes
Database Buffers        96468992 bytes
Redo Buffers             4247552 bytes
Database mounted.

Invoke nid command using user having sysdba privilege. Substitute DBNAME with new database name.

[oracle@localhost dbs]$ nid TARGET=sys/oracle@DUPDB1 DBNAME=CATDB

DBNEWID: Release 11.2.0.4.0 - Production on Wed Sep 27 13:45:27 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to database DUPDB1 (DBID=800698880)

Connected to server version 11.2.0

Control Files in database:
    /u01/app/oracle/oradata/DUPDB1/control01.ctl
    /u01/app/oracle/fast_recovery_area/DUPDB1/control02.ctl

Change database ID and database name DUPDB1 to CATDB? (Y/[N]) => Y

Proceeding with operation
Changing database ID from 800698880 to 2501525751
Changing database name from DUPDB1 to CATDB
    Control File /u01/app/oracle/oradata/DUPDB1/control01.ctl - modified
    Control File /u01/app/oracle/fast_recovery_area/DUPDB1/control02.ctl - modified
    Datafile /u01/app/oracle/oradata/DUPDB1/system01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/sysaux01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/undotbs01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/users01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/example01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/tbs_restore01.db - dbid changed, wrote new name
    Datafile /u01/app/oracle/oradata/DUPDB1/temp01.db - dbid changed, wrote new name
    Control File /u01/app/oracle/oradata/DUPDB1/control01.ctl - dbid changed, wrote new name
    Control File /u01/app/oracle/fast_recovery_area/DUPDB1/control02.ctl - dbid changed, wrote new name
    Instance shut down

Database name changed to CATDB.
Modify parameter file and generate a new password file before restarting.
Database ID for database CATDB changed to 2501525751.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.

Shutdown database.

[oracle@localhost dbs]$
shutdown immediate

Change db_name=CATDB in initDUPDB1.ora

Start database in mount mode. Ignore the error.

SQL> startup mount pfile=initDUPDB1.ora;
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size               2253824 bytes
Variable Size          314575872 bytes
Database Buffers        96468992 bytes
Redo Buffers             4247552 bytes
ORA-01103: database name 'CATDB' in control file is not 'DUPDB1'

Shutdown database.

shutdown immediate

Create new password file for new database name.

[oracle@localhost dbs]$ orapwd file=/u01/app/oracle/product/11.2.0/db_1/dbs/orapwCATDB.ora password=oracle entries=10

Set ORACLE_SID to new value.

ORACLE_SID=CATDB; export ORACLE_SID

Rename the SPFILE or pfile whichever you are using

[oracle@localhost dbs]$ mv initDUPDB1.ora initCATDB.ora

Alter the listener.ora and tnsnames.ora with new database SID


Reload listener to affect the change.
lsnrctl reload

Start the database in mount mode.

[oracle@localhost dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 27 14:44:49 2017

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

Connected to an idle instance.

SQL> startup mount;
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size               2253824 bytes
Variable Size          331353088 bytes
Database Buffers        79691776 bytes
Redo Buffers             4247552 bytes
Database mounted.

Open database in resetlogs mode.

SQL> alter database open resetlogs;

Database altered.

Verify whether dbname and instance name has changed.

SQL> select name,open_mode from v$database;

NAME     OPEN_MODE
--------- --------------------
CATDB    READ WRITE

SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
CATDB

Our database rename is complete. For complete rename, you need to change all references to old database name.
You can see still datafiles and logfiles are refering to a directory with old dbname.
You can change it by method of renaming datafiles and log files. I am not explaining it here.

SQL>
SQL> select file_name , tablespace_name,AUTOEXTENSIBLE,status  from dba_data_files;

FILE_NAME                                TABLESPACE_NAME    AUT STATUS
------------------------------------------------ ------------------ --- ---------
/u01/app/oracle/oradata/DUPDB1/system01.dbf     SYSTEM          YES AVAILABLE
/u01/app/oracle/oradata/DUPDB1/sysaux01.dbf     SYSAUX          YES AVAILABLE
/u01/app/oracle/oradata/DUPDB1/undotbs01.dbf    UNDOTBS1        YES AVAILABLE
/u01/app/oracle/oradata/DUPDB1/users01.dbf      USERS           YES AVAILABLE
/u01/app/oracle/oradata/DUPDB1/example01.dbf    EXAMPLE         YES AVAILABLE

SQL> select member from v$logfile;

MEMBER
-----------------------------------------------
/u01/app/oracle/oradata/DUPDB1/redo03.log
/u01/app/oracle/oradata/DUPDB1/redo02.log
/u01/app/oracle/oradata/DUPDB1/redo01.log

Finally when everything is done, take a good backup of database as all old backups are obsolete now.


Backup the database.

Tuesday, September 26, 2017

Oracle Backup and Restore Case Scenarios - Case Study 7 (Block Corruption)

Oracle Backup and Restore Case Scenarios - Case Study 7 (Block Corruption)

Case 7: Recover a block corruption

Corrupted blocks can be recovered in many ways. I am showing you 2 methods. First using RMAN BLOCKRECOVER and next using Export/Import.

Steps Outlined:
                Method 1
1.       Create a test tablespace for the practical
2.       Create a table in tablespace and populate it with some rows
3.       Take a consistent RMAN backup of database
4.       Find the block numbers for ROWIDs in the table
5.       Corrupt few blocks using OS dd command.
6.       Flush buffer cache to force Oracle to read rows from disk.
7.       Read from table to see corruption
8.       Corruption can be seen in alert log as well
9.       Repair corrupted blocks using BLOCKRECOVER command of RMAN
10.   Check whether corruption has been recovered
Method 2
1.       Take datapump export of test table
2.       Corrupt few blocks again for the table
3.       Drop the table
4.       Import the dump back to table
5.       Check whether corruption has been recovered.

Create a test tablespace

SQL> create tablespace tbs_test datafile '/u01/app/oracle/oradata/ORADB1/tbs_test.df' size 20m autoextend on next 10m extent management local segment space management auto;

Tablespace created.

SQL> create user aj identified by aj default tablespace tbs_test;

User created.

SQL> grant connect, resource to aj;

Grant succeeded.

SQL> grant dba to aj;

Grant succeeded.

SQL> conn aj/aj
Connected.

Create test table in tablespace and populate it with some rows.

SQL> create table testaj tablespace tbs_test as select * from dba_objects;

Table created.

SQL> select count(*) from testaj;

  COUNT(*)
----------
     86977

Get the block numbers of rows using procedure dbms_rowid.rowid_block_number.

SQL> select * from (select distinct dbms_rowid.rowid_block_number(rowid) bn from testaj order by bn )
where rownum <10;
 
       BN
----------
       131
       132
       133
       134
       135
       136
       137
       138
       139

9 rows selected.

Take a good consistent backup of database to rely upon.

RMAN> backup database plus archivelog;


Starting backup at 26-SEP-2017 16:54:06
. . . .
. . . .

Finished backup at 26-SEP-2017 16:55:20

Starting Control File and SPFILE Autobackup at 26-SEP-2017 16:55:20
piece handle=/u01/backup/rman/ctl_c-2294695355-20170926-01 comment=NONE
Finished Control File and SPFILE Autobackup at 26-SEP-2017 16:55:21

RMAN>

Corrupt few blocks using OS dd command.

[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=131 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.0129064 s, 635 kB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=132 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.00177635 s, 4.6 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=133 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000128699 s, 63.7 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=134 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000116262 s, 70.5 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=135 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000150806 s, 54.3 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=136 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000117566 s, 69.7 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=137 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000116422 s, 70.4 MB/s
[oracle@localhost trace]$  dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=138 conv=notrunc count=1 if=/dev/zero
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 0.000115229 s, 71.1 MB/s
[oracle@localhost trace]$


Flush buffer cache to force Oracle to read from disk.

SQL> alter system flush buffer_cache;

System altered.

Try to read the table which will fail. If it does not fail, flush the buffer cache couple more times.

SQL> select count(*) from testaj;
select count(*) from testaj
                     *
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: '/u01/app/oracle/oradata/ORADB1/tbs_test.df'



SQL> select * from v$database_block_corruption;

     FILE#     BLOCK#        BLOCKS CORRUPTION_CHANGE# CORRUPTIO
---------- ---------- ---------- ------------------ ---------
        6       131         8               0 ALL ZERO

Check the alert log for list of corrupted blocks.

[oracle@localhost trace]$ cat alert_ORADB1.log |grep -i "corrupt block"
Corrupt block relative dba: 0x01800083 (file 6, block 131)
Corrupt block relative dba: 0x01800084 (file 6, block 132)
Corrupt block relative dba: 0x01800085 (file 6, block 133)
Corrupt block relative dba: 0x01800086 (file 6, block 134)
Corrupt block relative dba: 0x01800087 (file 6, block 135)
Corrupt block relative dba: 0x01800088 (file 6, block 136)
Corrupt block relative dba: 0x01800089 (file 6, block 137)
Corrupt block relative dba: 0x0180008a (file 6, block 138)


You can either recover one block at a time using DATAFILE and BLOCK number. Or you can recover entire list using clause CORRUPTION LIST.

BLOCKRECOVER DATAFILE 6, BLOCK 131
BLOCKRECOVER DATAFILE 6, BLOCK 132
BLOCKRECOVER DATAFILE 6, BLOCK 133
BLOCKRECOVER DATAFILE 6, BLOCK 134
BLOCKRECOVER DATAFILE 6, BLOCK 135
BLOCKRECOVER DATAFILE 6, BLOCK 136
BLOCKRECOVER DATAFILE 6, BLOCK 137
BLOCKRECOVER DATAFILE 6, BLOCK 138

RMAN> blockrecover corruption list;

Starting recover at 26-SEP-2017 16:58:27
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=34 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=29 device type=DISK

channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00006
channel ORA_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/ORADB1/backupset/2017_09_26/o1_mf_nnndf_TAG20170926T165423_dwng78bv_.bkp
channel ORA_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/ORADB1/backupset/2017_09_26/o1_mf_nnndf_TAG20170926T165423_dwng78bv_.bkp tag=TAG20170926T165423
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:01

starting media recovery
media recovery complete, elapsed time: 00:00:03

Finished recover at 26-SEP-2017 16:58:32

Check whether corruption is still present.

SQL> select * from v$database_block_corruption;

no rows selected

Read from table to check whether all rows have been recovered.

SQL> select count(*) from testaj;

  COUNT(*)
----------
     86977

2nd Method: Repair corruption using Export / Import
We can recover the block using export/import as well.

Take a good export of the table which we are going to corrupt.
[oracle@localhost trace]$ expdp aj/aj tables=testaj dumpfile=testaj.dmp logfile=expdp_testaj.log

Export: Release 11.2.0.4.0 - Production on Tue Sep 26 17:01:01 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "AJ"."SYS_EXPORT_TABLE_01":  aj/******** tables=testaj dumpfile=testaj.dmp logfile=expdp_testaj.log
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 10 MB
Processing object type TABLE_EXPORT/TABLE/TABLE
. . exported "AJ"."TESTAJ"                               8.433 MB   86977 rows
Master table "AJ"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for AJ.SYS_EXPORT_TABLE_01 is:
  /u01/app/oracle/admin/ORADB1/dpdump/testaj.dmp
Job "AJ"."SYS_EXPORT_TABLE_01" successfully completed at Tue Sep 26 17:01:13 2017 elapsed 0 00:00:09

Corrupt few blocks again using dd command.

[oracle@localhost trace]$ dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=131 conv=notrunc count=1 if=/dev/zero
[oracle@localhost trace]$ dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=132 conv=notrunc count=1 if=/dev/zero
[oracle@localhost trace]$ dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=133 conv=notrunc count=1 if=/dev/zero
[oracle@localhost trace]$ dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=134 conv=notrunc count=1 if=/dev/zero
[oracle@localhost trace]$ dd of=/u01/app/oracle/oradata/ORADB1/tbs_test.df bs=8192 seek=135 conv=notrunc count=1 if=/dev/zero

Check whether database got affected with corruption.

SQL> alter system flush buffer_cache;

System altered.

SQL> /

System altered.

SQL> select count(*) from testaj;
select count(*) from testaj
                     *
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: '/u01/app/oracle/oradata/ORADB1/tbs_test.df'

Drop the table which will remove corruption.

SQL> drop table testaj;

Table dropped.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Import the table from dump to get back the rows.

[oracle@localhost trace]$ impdp aj/aj tables=testaj dumpfile=testaj.dmp logfile=impdp_testaj.log

Import: Release 11.2.0.4.0 - Production on Tue Sep 26 17:07:15 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "AJ"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "AJ"."SYS_IMPORT_TABLE_01":  aj/******** tables=testaj dumpfile=testaj.dmp logfile=impdp_testaj.log
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "AJ"."TESTAJ"                               8.433 MB   86977 rows
Job "AJ"."SYS_IMPORT_TABLE_01" successfully completed at Tue Sep 26 17:07:19 2017 elapsed 0 00:00:03

[oracle@localhost trace]$ sqlplus aj/aj

SQL*Plus: Release 11.2.0.4.0 Production on Tue Sep 26 17:07:23 2017

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

You have got all the rows back.

SQL> select count(*) from testaj;

  COUNT(*)
----------
     86977

You can find more information at:

http://docs.oracle.com/cd/E25054_01/backup.1111/e10642/rcmblock.htm