Consider the following scenario: I got a oracle database that reside in /db/mydb
. For some reason I need to copy or move the database to somewhere else, let’s say /db2/mydb2
.
To do this, follow the steps:
- Shutdown the database either using service or sql statement:
shutdown immediate
. - Copy or move the database to
/db2
and rename the new database directory tomydb2
. - Edit the initialize parameter file in the
mydb2
(something like initxxx.ora), change the all the paths to the new path (from/db/mydb
to/db2/mydb2
) - Log in to sqlplus with NOLOG (run in terminal):
sqlplus /nolog
- Connect to empty instance (run in SQLPLUS):
connection / as sysdba
- Create SPFILE, aware that the path of the initialize parameter file might be vary (run in SQLPLUS):
create spfile from pfile='/db2/mydb2/admin/pfile/initxxx.ora';
- Start database in mount mode (run in SQLPLUS):
startup mount;
- Change the data file path to the new path (run in SQLPLUS):
alter database rename file '/db/mydb/data.dbf', '/db/mydb/system.dbf' to '/db2/mydb2/data.dbf', '/db2/mydb2/system.dbf';
- Ensure you change all the data file path! Or else it will cause the orginal database cannot be startup! (Refer updates below)
- Open your database (run in SQLPLUS):
alter database open;
- Now it is good to go.
If you want to run both databases in the same time, you need to rename the new database. Follow the following steps:
- Rename the new database to mydb2
- Remember to change the database name in
mydb2
parameter file as well. - Log into SQLPLUS. At this point, you need to aware the
SID_NAME
in your environment parameter, need to be point to the database that you want to log in. Now we want to log in to the original, so we set theSID_NAME
tomydb
(terminal):export SID_NAME=mydb
- Connect to empty instance (SQLPLUS):
connect / as sysdba
- Create the SPFILE again using the original parameter file (SQLPLUS):
create spfile from pfile='/db/mydb/admin/pfile/initxxx.ora';
- Now you should able to start your database (SQLPLUS):
startup
Update 2013/2/24
You may find out the data files path using the following query (thanks to Concerned_Netizen):
select name from v$datafile
select member from v$tempfile
select member from v$logfile