修改数据库名(db_name)及实例名(Instance_name or Service_name)的方法 - .net学习交流

时间:2024-03-09 08:17:28

修改数据库名(db_name)及实例名(Instance_name or Service_name)的方法

用oracle自带的工具nid改数据库名

在本例中,假设原来的数据库名为test,要改成testdb,原实例名(service_name,instance_name)test,要改成testdb.

nid是自带的工具,在oracle_home/bin目录中.以下方法假设登陆到数据库本机做。

1.1 sqlplus "sys/password as sysdba"

1.2 sql>shutdown immediate --先停掉数据库

1.3 sql>startup mount --nid需要在mount状态下才能做。

1.4sql>host nid target=sys/password dbname=new_dbname --nid是一个在操作系统下执行的命令,在sqlplus环境中用host 去调用操作系统环境下的命令。命令中,当你想将数据库名字改为TESTDB时,应写成dbname=testdb

1.5执行后程序会询问:

Change database ID and database name TEST to TESTDB? (Y/[N]) =>y

敲y确定。

之后程序会对控制文件,数据文件进行修改:

Control File C:ORACLEORADATATESTCONTROL01.CTL - modified
Control File C:ORACLEORADATATESTCONTROL02.CTL - modified
Control File C:ORACLEORADATATESTCONTROL03.CTL - modified
Datafile C:ORACLEORADATATEST YSTEM01.DBF - dbid changed, wrote new name
Datafile C:ORACLEORADATATESTUNDOTBS01.DBF - dbid changed, wrote new nam

Datafile C:ORACLEORADATATESTINDX01.DBF - dbid changed, wrote new name
Datafile C:ORACLEORADATATESTTOOLS01.DBF - dbid changed, wrote new name
Datafile C:ORACLEORADATATESTUSERS01.DBF - dbid changed, wrote new name
Datafile D:DATAFILEPORMALS_SPA.DBF - dbid changed, wrote new name
Datafile C:ORACLEORADATATESTOEM_REPOSITORY.DBF - dbid changed, wrote ne
name
Datafile D:DATAFILEPORMALS_SPA_01.DBF - dbid changed, wrote new name
Datafile C:ORACLEORADATATESTTEMP01.DBF - dbid changed, wrote new name
Control File C:ORACLEORADATATESTCONTROL01.CTL - dbid changed, wrote new
name
Control File C:ORACLEORADATATESTCONTROL02.CTL - dbid changed, wrote new
name
Control File C:ORACLEORADATATESTCONTROL03.CTL - dbid changed, wrote new
name

Database name changed to TESTDB.
Modify parameter file and generate a new password file before restarting.
Database ID for database TESTDB changed to 2321050327.
All previous backups and archived redo logs for this database are unusable.
Shut down database and open with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.

1.6 停库再到mount状态下改db_name:

sql>shutdown immediate;

sql>startup mount

sql>alter system set db_name=testdb scope=spfile;

sql>shutdown immediate;

1.7 重新创建sys的password文件:

passwd文件通常放在oracle_home/database目录下,文件命名形式为PWDsid.ora,sid为实例名(Service_name),如当前的数据库名及service_name为test,则passwd文件为PWDtest.ora

sql>host orapwd file=c:oracleora92databasepwdtest.ora password=123456 entries=5

要注意一下,此时虽然数据库名已经改成testdb了,但instance_name还是test,所以,passwd文件必须跟以前一样。否则会出错。

1.8 开启数据库:(要open resetlogs)

sql>startup mount

sql>alter database open resetlogs;

1.9 检查:

sql>select dbid,name from v$database;

2. 改好数据库名后,接着改instance_name

2.1如果是windows系统,要先把所有的oracle服务先关闭,否则会出错。

2.2先将原来的service_name删除:

在操作系统中,进入cmd,

oradim -delete -sid test

2.3创建密码文件

orapwd file=c:/oracle/ora92/database/pwdtestdb.ora password=....entries=

2.4创建一个新的sid,也就是你想改名的sid:

oradim -new -sid testdb -intpwd 密码 -startmode a -pfile c:oracleora92databaseinittestdb.ora

当数据库启动时,会在database目录中找spfile,如果spfile不在就找initSID.ora这个文件来顶。所以,可以把pfile直接创建在这里。方便数据库的启动。

2.5 进入oracle并创建spfile:

c: set oracle_sid=testdb

c:sqlplus "sys/password as sysdba"

sql>create spfile from pile=\'c:oracleora92databaseinittestdb.ora\';

2.6 reload listener:

c:lsnrctl reload

2.7 open resetlogs: