修改oracle实例名(sid)和数据库名(db_name).doc
修改oracle实例名(sid)和数据库名(db_name)有时我们需要修改数据库的sid和dbname,除了使用rman进行备份恢复之外,也可以通过手工方式修改,主要由两个主要过程完成:1、修改实例名(SID)2、修改数据库名(dbname)下面演示将数据库sid和dbname由orcl修改为cnhtm的过程:1、修改实例名(sid)1.1、检查原来的数据库实例名(sid)oracleoracle/home/oracle> echo $ORACLE_SIDorcloracleoracle/home/oracle> sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:14:49 2009Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining optionssysORCL> select instance from v$thread;INSTANCE-orcl1.2、关闭数据库注意不能用shutdown abort,只能是shutdown immediate或shutdown normalsysORCL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.sysORCL> exitDisconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining options1.3、修改oracle用户的ORACLE_SID环境变量,如由orcl修改为cnhtmoracleoracle/home/oracle> cat /.bash_profile|grep -i sidORACLE_SID=cnhtmexport ORACLE_BASE ORACLE_HOME ORACLE_SID PATH1.4、修改/etc/oratab文件,将sid名由旧的修改为新的,如从orcl修改为cnhtmoracleoracle/home/oracle> cat /etc/oratab.cnhtm:/oracle/app/10.1:Y+ASM:/oracle/app/10.1:Y1.5、进入到$ORACLE_HOME/dbs目录将所有文件名中包含原来的sid的修改为对应的新sid的如我对如下文件修改为其后对应的文件hc_orcl.dat->hc_cnhtm.datlkORCL->lkCNHTMorapworcl->orapwcnhtmsnapcf_orcl.f->snapcf_cnhtm.fspfileorcl.ora->spfilecnhtm.ora1.6、使新修改的ORACLE_SID环境变量生效oracleoracle/oracle/app/10.1/dbs> . /.bash_profileoracleoracle/oracle/app/10.1/dbs> echo $ORACLE_SIDcnhtm1.7、重建口令文件因为口令文件改名后不能在新实例中使用,所以重建oracleoracle/oracle/app/10.1/dbs> orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=5 force=yoracleoracle/oracle/app/10.1/dbs> ls -lrt orapw*-rw-r- 1 oracle oinstall 2048 Dec 20 11:27 orapwcnhtm1.8、启动数据库oracleoracle/oracle/app/10.1/dbs> sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:29:53 2009Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to an idle instance.idle> startupORACLE instance started.Total System Global Area bytesFixed Size bytesVariable Size bytesDatabase Buffers bytesRedo Buffers bytesDatabase mounted.Database opened.1.9、检查数据库实例名通过如下语句检查数据库实例名,发现实例名已经由orcl变成cnhtmidle> select instance from v$thread;INSTANCE-cnhtm2、修改数据库名(dbname)虽然已经修改过了实例名(sid),但是数据库的名称(dbname还是原来的名称orcl)idle> conn / as sysdbaConnected.sysORCL> show parameter nameNAME TYPE VALUE- - -db_file_name_convert stringdb_name string orcldb_unique_name string orclglobal_names boolean FALSEinstance_name string cnhtmlock_name_space stringlog_file_name_convert stringservice_names string orcl可以通过如下步骤修改数据库名(dbname)2.1、首先切换一下在线日志,使数据库做checkpointsysORCL> alter system archive log current;System altered.2.2、生成重建控制文件的脚本sysORCL> alter database backup controlfile to trace resetlogs;Database altered.2.3、关闭数据库,需要干净关闭,不能shutdown abortsysORCL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.sysORCL> exitDisconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining options2.4、进入$ORACLE_BASE/admin/<sid>/udump目录中,找到最新生成的trc文件,这就是重建控制文件的脚本oracleoracle/oracle/admin/orcl/udump> ls -lrttotal 2608-rw-r- 1 oracle oinstall 577 Nov 7 13:37 orcl_ora_12020.trc.-rw-r- 1 oracle oinstall 4407 Dec 20 11:36 cnhtm_ora_7789.trc2.5、将找到的trc文件复制一份,并命名为ccf.sqloracleoracle/oracle/admin/orcl/udump> cp cnhtm_ora_7789.trc ccf.sql2.6、修改ccf.sql查找STARTUP NOMOUNT语句,将这一行上面的所有行都删除查找所有以-开始的行,把这些行删除查找所有的orcl修改为cnhtm,所有的ORCL修改为CNHTM找到CREATE CONTROLFILE REUSE DATABASE.语句,将其中的REUSE修改为SET找到RECOVER DATABASE USING BACKUP CONTROLFILE语句,将其用双横线(-)注释掉如果有精力,可以修改这个脚本中的datafile和logfile部分使用新的文件名称,其实这部分不修改也可以,我为了测试的目的进行了修改,修改后要记得去重命名数据文件和log文件,将对应的数据文件和log文件与这里的名称相对应我修改后的ccf.sql文件内容如下STARTUP NOMOUNTCREATE CONTROLFILE set DATABASE "cnhtm" RESETLOGS ARCHIVELOGMAXLOGFILES 16MAXLOGMEMBERS 3MAXDATAFILES 100MAXINSTANCES 8MAXLOGHISTORY 292LOGFILEGROUP 1 '+DATA/cnhtm/onlinelog/group_1.LOG' SIZE 100M,GROUP 2 '+DATA/cnhtm/onlinelog/group_2.LOG' SIZE 100M,GROUP 3 '+DATA/cnhtm/onlinelog/group_3.LOG' SIZE 100M,GROUP 4 '+DATA/cnhtm/onlinelog/group_4.LOG' SIZE 100MDATAFILE'+DATA/cnhtm/datafile/system01.DBF','+DATA/cnhtm/datafile/undotbs101.DBF','+DATA/cnhtm/datafile/sysaux01.DBF','+DATA/cnhtm/datafile/users01.DBF','+DATA/cnhtm/datafile/example01.DBF','+DATA/cnhtm/datafile/tbs_lmt01.DBF','+DATA/cnhtm/datafile/tbs_lmt_201.DBF','+DATA/cnhtm/datafile/tbs_lmt_301.DBF'CHARACTER SET ZHS16GBK;-RECOVER DATABASE USING BACKUP CONTROLFILE;ALTER DATABASE OPEN RESETLOGS;ALTER TABLESPACE TEMP ADD TEMPFILE '+DATA/cnhtm/tempfile/temp.269.'SIZE REUSE AUTOEXTEND ON NEXT MAXSIZE 32767M;2.7、修改数据文件、在线日志文件名如果上一步修改了ccf.sql文件中的datafile和logfile段的文件名,这里要将这些文件名重命令为与其一致。因为我的实验环境使用了ASM,在Oracle 10.2中ASM中不能重命名和复制文件,我采用了创建别名的方式,操作如下:oracleoracle/oracle/admin/cnhtm/udump> export ORACLE_SID=+ASMoracleoracle/oracle/admin/cnhtm/udump> sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 12:08:52 2009Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining optionsidle> alter diskgroup data add directory '+data/CNHTM'Diskgroup altered.idle> alter diskgroup data add directory '+data/CNHTM/DATAFILE'Diskgroup altered.idle> alter diskgroup data add alias2 '+data/CNHTM/DATAFILE/EXAMPLE01.DBF'3 for4 '+data/ORCL/DATAFILE/EXAMPLE.261.'Diskgroup altered.idle> alter diskgroup data add alias2 '+data/CNHTM/DATAFILE/USERS01.DBF'3 for4 '+data/ORCL/DATAFILE/USERS.266.'Diskgroup altered.idle> alter diskgroup data add directory '+data/CNHTM/ONLINELOG'Diskgroup altered.idle> alter diskgroup data add alias2 '+data/CNHTM/ONLINELOG/group_4.LOG'3 for4 '+data/ORCL/ONLINELOG/group_7.273.'Diskgroup altered.2.8、如果归档日志目录名中包含sid,那么修改归档目录名我的测试环境,归档目录使用的是flash_recovery_area,所以需要将这个目录中的ORCL目录重命名为CNHTM2.9、使用spfile生成pfile注意这里没有启动数据库,只是链接到idle状态oracleoracle/home/oracle> export ORACLE_SID=cnhtmoracleoracle/home/oracle> sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:49:20 2009Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to an idle instance.idle> create pfile='?/dbs/initcnhtm.ora' from spfile;File created.idle> exitDisconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining options2.10、进入$ORACLE_HOME/dbs目录2.11、编辑initcnhtm.ora文件搜索所有的orcl,重命令为cnhtm,搜索所有的ORCL,重命名为CNHTM2.12、删除控制文件 将原来的控制文件删除或重命名控制文件的位置名称可以通过查看2.11步骤中的*.control_files来确定如果控制文件在ASM中,可以进入asmcmd命令,然后用rm命令删除2.13、进入$ORACLE_BASE/admin目录将orcl目录重命名为cnhtm2.14、使用修改过的pfile生成spfileoracleoracle/oracle/admin/cnhtm/udump> sqlplus / as sysdbaSQL*Plus: Release 10.2.0.1.0 - Production on Sun Dec 20 11:59:56 2009Copyright (c) 1982, 2005, Oracle. All rights reserved.Connected to an idle instance.idle> create spfile from pfile='?/dbs/initcnhtm.ora'File created.2.15、调用2.6步骤修改好的ccf.sqlidle> /oracle/admin/cnhtm/udump/ccf.sqlORACLE instance started.Total System Global Area bytesFixed Size bytesVariable Size bytesDatabase Buffers bytesRedo Buffers bytesControl file created.Database altered.Database altered.Tablespace altered.2.16、检查数据库状态idle> conn / as sysdbaConnected.sysCNHTM>select open_mode from v$database;OPEN_MODE-READ WRITEsysCNHTM> show parameter nameNAME TYPE VALUE- - -db_file_name_convert stringdb_name string cnhtmdb_unique_name string cnhtmglobal_names boolean FALSEinstance_name string cnhtmlock_name_space stringlog_file_name_convert stringservice_names string cnhtm发现数据库名(db_name)已经修改为cnhtm