Wednesday, April 22, 2009

move/rename oracle datafiles

To properly move the datafile around or rename the datafile, follow this guide:

  1. Login to SQLPlus.
  2. Connect as SYS DBA with CONNECT / AS SYSDBA command.
  3. Shutdown the database instance with SHUTDOWN command.
  4. Rename or/and move the datafiles at OS level.
  5. Start Oracle database in mount state with STARTUP MOUNT command.
  6. Modify the name or location of datafiles in Oracle data dictionary using following command syntax:

    ALTER DATABASE RENAME FILE ‘’ TO ‘’;

  7. Open Oracle database instance completely with ALTER DATABASE OPEN command.

If the datafiles that need to be changed or moved do not belong to SYSTEM tablespaces, and do not contain active rollback segments or temporary segments, there is another workaround that does not require database instance to be shutdown. Instead, only the particular tablespace that contains the date files is taken offline.

  1. Login to SQLPlus.
  2. Connect as SYS DBA with CONNECT / AS SYSDBA command.
  3. Make offline the affected tablespace with ALTER TABLESPACE OFFLINE; command.
  4. Modify the name or location of datafiles in Oracle data dictionary using following command syntax:

    ALTER TABLESPACE RENAME DATAFILE ‘’ TO ‘’;

  5. Bring the tablespace online again with ALTER TABLESPACE alter tablespace ONLINE; command.

Tuesday, April 21, 2009

wtf is Oracle Varchar2? What are the difference between varchar and varchar2

I was kinda confused earlier on the differences but get a better explaination after hunting around. Below details each of their characteristics:

Char: Declare and used. If the size declared is 5 and assigned value is only length 2, the leftover will be stored with spaces. Some claimed this would be much faster, but some doesn't find any differences in performance compare to varchar.
VarChar: Declare in advance, size used only when assigning values. Depending on the values assigned, for those that have not been used or excessive size allocated will be freed. First few bites will store the length of the values assigned. ANSI SQL Standard.
VarChar2: It is an oracle's improve Varchar datatype version. Characteristics are mostly similar to varchar.

Do you have anything to addon?

Recycled from the Internet.

Friday, April 17, 2009

Use RMAN to copy a database to a different Server

Question: I want to use RMAN to copy a database to a different server, in a different disk directory. Can I use RMAN to restore a database to another server?


Applies to: Oracle Server - Enterprise Edition - Version: 10.2.0.0.0

The goal is restoring a database using RMAN on a different node with different backup directory structures and different database directory structures .

- You have a database backed up on NODE 1.
- You need to restore the database on NODE 2.
- The directory structure is different on NODE 2.
- You need to put the backups in a new directory structure in NODE 2, unlike as they were in NODE 1.
- You need to restore the database files into a new directory structure in NODE 2, unlike as they were in NODE 1.

Solution

Below is the procedure with an example of using RMAN to copy a database to another directory:

1) Connect to the target database using rman and backup the database ---> NODE 1


$ rman target /Recovery Manager: Release 10.2.0.1.0 - Production on Tue Feb 13 00:29:33
2007Copyright © 1982, 2005, Oracle. All rights reserved.connected to target database: ORA10G (DBID=3932056136)RMAN>
backup database plus archivelog;Starting backup at 13-FEB-07current log archivedusing channel ORA_DISK_1channel
ORA_DISK_1: starting archive log backupsetchannel ORA_DISK_1: specifying archive log(s) in backup setinput archive log thread=1
sequence=143 recid=109 stamp=614392105channel ORA_DISK_1: starting piece 1 at 13-FEB-07channel ORA_DISK_1: finished piece
1 at 13-FEB-07piece handle=/u01/oracle/product/oradata/ora10g/fra/ORA10G/backupset/2007_02_13/o1_mf_annnn_TAG20070213T002825_2x21kbds_.bkp
tag=TAG20070213T002825 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:02Finished backup at 13-FEB-07Starting
backup at 13-FEB-07using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backupsetchannel ORA_DISK_1: specifying
datafile(s) in backupsetinput datafile fno=00003 name=/u01/oracle/product/oradata/ora10g/data/sysaux01.dbfinput datafile
fno=00001 name=/u01/oracle/product/oradata/ora10g/data/system01.dbfinput datafile fno=00002 name=/u01/oracle/product/oradata/ora10g/data/undotbs01.dbfinput
datafile fno=00004 name=/u01/oracle/product/oradata/ora10g/data/users01.dbfinput datafile fno=00005 name=/home/oracle/1.dbfinput
datafile fno=00006 name=/u01/oracle/product/oradata/ora10g/data/sysaux02.dbfinput datafile fno=00007 name=/u01/oracle/product/oradata/ora10g/data/undotbs02.dbfchannel
ORA_DISK_1: starting piece 1 at 13-FEB-07channel ORA_DISK_1: finished piece 1 at 13-FEB-07piece handle=/u01/oracle/product/oradata/ora10g/fra/ORA10G/backupset/2007_02_13/o1_mf_nnndf_TAG20070213T002827_2x21kd12_.bkp
tag=TAG20070213T002827 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:55Finished backup at 13-FEB-07Starting
backup at 13-FEB-07current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archive log backupsetchannel
ORA_DISK_1: specifying archive log(s) in backup setinput archive log thread=1 sequence=144 recid=110 stamp=614392165channel
ORA_DISK_1: starting piece 1 at 13-FEB-07channel ORA_DISK_1: finished piece 1 at 13-FEB-07piece handle=/u01/oracle/product/oradata/ora10g/fra/ORA10G/backupset/2007_02_13/o1_mf_annnn_TAG20070213T002925_2x21m6ty_.bkp
tag=TAG20070213T002925 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:02Finished backup at 13-FEB-07Starting
Control File and SPFILE Autobackup at 13-FEB-07piece handle=/u01/oracle/product/ora10g/dbs/c-3932056136-20070213-02 comment=NONEFinished Control File and SPFILE Autobackup at 13-FEB-07RMAN> exit




2) Move the following files to the NODE 2 :

+ The database backup pieces
+ Controlfile backup piece
+ The parameter file i.e init.ora file

3) Edit the PFILE on NODE 2 to change the environment specific parameters like .

user_dump_dest =
background_dump_dest =
control_files =


4) Once the PFILE is suitably modified invoke Rman on the NODE 2 after setting the Oracle environment variables and start the database in nomount mode:


[oracle@test-br test]$ export ORACLE_HOME=/u01/oracle/product/ora10g
[oracle@test-br test]$ export ORACLE_SID=ora10g
[oracle@test-br test]$ export PATH=$ORACLE_HOME/bin:$PATH
[oracle@test-br test]$ rman target /
Recovery Manager: Release 10.2.0.1.0 - Production on Tue Feb 13 00:36:55 2007
Copyright © 1982, 2005, Oracle. All rights reserved.
connected to target database (not started)
RMAN> startup nomount
Oracle instance started
Total System Global Area 205520896 bytes
Fixed Size 1218508 bytes
Variable Size 75499572 bytes
Database Buffers 121634816 bytes
Redo Buffers 7168000 bytes




5) Restore the controlfile from the backup piece.



RMAN> restore controlfile from '/u01/oracle/product/ora10g/dbs/c-3932056136-20070213-02';
Starting restore at 13-FEB-07
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete,
elapsed time: 00:00:02
output filename=/u01/oracle/product/oradata/ora10g/cntrl/control01.ctl
Finished restore at 13-FEB-07



6) Mount the database.


RMAN> alter database mount ;



7) Now catalog the backup pieces that were shipped from NODE 1.

RMAN> catalog backuppiece '/home/oracle/test/backup/o1_mf_annnn_TAG20070213T002925_2x21m6ty_.bkp';Starting
implicit crosscheck backup at 13-FEB-07allocated channel: ORA_DISK_1channel ORA_DISK_1: sid=155 devtype=DISKCrosschecked
3 objectsFinished implicit crosscheck backup at 13-FEB-07Starting implicit crosscheck copy at 13-FEB-07using channel
ORA_DISK_1Finished implicit crosscheck copy at 13-FEB-07searching for all files in the recovery areacataloging files...no
files catalogedcataloged backuppiecebackup piece handle=/home/oracle/test/backup/o1_mf_annnn_TAG20070213T002925_2x21m6ty_.bkp
recid=41 stamp=614393265RMAN> catalog backuppiece '/home/oracle/test/backup/o1_mf_annnn_TAG20070213T002825_2x21kbds_.bkp';cataloged
backuppiecebackup piece handle=/home/oracle/test/backup/o1_mf_annnn_TAG20070213T002825_2x21kbds_.bkp recid=42 stamp=614393292RMAN>
catalog backuppiece '/home/oracle/test/backup/o1_mf_nnndf_TAG20070213T002827_2x21kd12_.bkp';cataloged backuppiecebackup
piece handle=/home/oracle/test/backup/o1_mf_nnndf_TAG20070213T002827_2x21kd12_.bkp recid=43 stamp=614393310



8) Get to know the last sequence available in the archivelog backup using the following command.


RMAN > list backup of archivelog all;



9) Rename the Redologfiles,so that they can be created in new locations when opened the database is opened in resetlogs.


SQL> alter database rename file '/u01/oracle/product/oradata/ora10g/log/redo01.log' to '/home/oracle/test/log/redo01.log';..................



10) Now restore the datafiles to new locations and recover.



RMAN> run { set until sequence set newname for datafile 1 to '/home/oracle/test/data/sys01.dbf';
set newname for datafile 2 to '/home/oracle/test/data/undotbs01.dbf'; set newname for datafile 3 to '/home/oracle/test/data/sysaux01.dbf';
set newname for datafile 4 to '/home/oracle/test/data/users01.dbf'; set newname for datafile 5 to '/home/oracle/test/data/1.dbf';
set newname for datafile 6 to '/home/oracle/test/data/sysaux02.dbf'; set newname for datafile 7 to '/home/oracle/test/data/undotbs02.dbf';
restore database; switch datafile all; recover database; alter database open resetlogs; }


This is just my note copy from Sorabh Harit. Thanks Sorabh.

Wednesday, April 15, 2009

brainstorming a new Document Management System

I am thinking to make myself a Document Management System (DMS).

I don't like any of  the DMS out there.

either they are not search friendly (KnowledgeTree).
hate the language it is written in (PHP)
too complicate to use. ( Alfresco)

I kind of like Docmgr DMS, it has a very clean interface and simply it just work.
the only think I don't like Docmgr is it is written in PHP.

I am not saying PHP is bad. but it just too troublesome to maintain the code and people will write bad code.

I am thinking to use Java and PostgreSQL as the main Technology components.

Java have been proven it is clean, well structure programming language.
PostgreSQL in my opinion, it is the best Open Source Database out there. (Don't convince me MySQL is good, to be honestly, I hate MySQL. Too many things can go wrong with MySQL).

The draft of the requirement is in http://docs.google.com/Doc?id=dsq74hw_55czc4zfd5&hl=en

Once I have time, I will finalize the requirements.

Tuesday, April 14, 2009

Purchase 2009 Ford Escape

Yesterday is my happy day. I went to Ford Dealer yesterday to browse some new Car. I was hooked by 2009 Escape.

the stylish, roomy and functional widgets make us to bring out our Credit card and reserved one of them.

I have never drove a Ford in my life so far and I has always thought American Car sucks. These statements are not true anymore.

I am looking forward to April 30. that is when my car is coming.

I will take some pix and keep you guys posted.

Sunday, April 12, 2009

Oracle Tips and Tricks

oracle auto generated drop objects :)

select 'Drop '||object_type||' '||object_name||
case
  when object_type = 'TABLE' then ' cascade constraints;' 
  else ';'
end
from user_objects;

This will generated all the drop table, sequence etc. statements for you.

Resume Mysql Replication

When MySQL slave db doesn't shutdown correctly, the next time you start it, the error may occur and complaints one of the log files is not found in mysql directory.

here is the procedure to fix it.

1). MYSQL > show slave status\G;

the output as the following
             Slave_IO_State:
                Master_Host: 127.0.01
                Master_User: remotereplicator
                Master_Port: 33061
              Connect_Retry: 60
            Master_Log_File: HP2-bin.000016
        Read_Master_Log_Pos: 819453275
             Relay_Log_File: mysqld-relay-bin.000020
              Relay_Log_Pos: 3311066
      Relay_Master_Log_File: HP2-bin.000016
           Slave_IO_Running: No
          Slave_SQL_Running: No
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 819453275
            Relay_Log_Space: 0
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: NULL
1 row in set (0.02 sec)

write down the Relay_Maste_Log_File and Read_Master_Log_File_Pos value. in this case, they are HP2-bin.000016 and 819453275 respectly.

2). MYSQL> stop slave;

3). MYSQL>reset slave;

4). MYSQL>start slave;

5). MYSQL>show slave status\G;

the output should be look like this

*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 127.0.0.1
                Master_User: remotereplicator
                Master_Port: 33061
              Connect_Retry: 60
            Master_Log_File: HP2-bin.000003
        Read_Master_Log_Pos: 3493
             Relay_Log_File: mysqld-relay-bin.000006
              Relay_Log_Pos: 233
      Relay_Master_Log_File: HP2-bin.000003
           Slave_IO_Running: Yes
          Slave_SQL_Running: No
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 1007
                 Last_Error: Error 'Can't create database 'manufacture';
database exists' on query. Default database: 'manufacture'. Query: 'create
database manufacture'
               Skip_Counter: 0
        Exec_Master_Log_Pos: 98
            Relay_Log_Space: 3628
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: NULL
1 row in set (0.00 sec)

ERROR:
No query specified

-- it tells you the database has exists.  this is a good sight. it means the
slave is working.


6). MYSQL> stop slave;

7). MYSQL>  CHANGE MASTER TO MASTER_HOST='127.0.01',
MASTER_USER='$remotereplicator',
MASTER_PASSWORD='$pass',MASTER_LOG_FILE='$log_file',
MASTER_LOG_POS=$log_pos;

in here,
$remotereplicator is the remote replication user
$pass is the remote replication user's password
$log_file is the remote replication file. in this case. it is HP2-bin.000016
$log_pos is the remote position, in this case, it is 819453275

8). MYSQL> start slave;

9). to confirm it is working.

    MYSQL> show slave status\G;

    the values of the Slave_IO_Running and the Slave_SQL_Running should be
"Yes"