一、数据库修改SID、DBNAME、PDB

实验环境

实验目的:有时候因特殊原因需要修改数据库的名称,以下详细演示整个过程;需要用到Oracle官方工具nid。
注意:注意最好要进行一次完整的备份,避免有些误操作。
分成两个步骤,先修改instance_name,在修改db_name。

1.1 先做rman全库备份

[oracle@ora-recover01 ~]$ rman target /

Recovery Manager: Release 19.0.0.0.0 - Production on Mon Jun 5 14:19:40 2023
Version 19.10.0.0.0

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

connected to target database: CFCDB (DBID=1601623831)

RMAN> backup as compressed backupset database include current controlfile plus archivelog delete all input ;


Starting backup at 2023/06/05 14:19:50
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=3854 device type=DISK
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=3 RECID=13 STAMP=1138717190
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:19:50
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:19:53
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/backupset/2023_06_05/o1_mf_annnn_TAG20230605T141950_l7tzn6rr_.bkp tag=TAG20230605T141950 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/oracle/app/db/fast_recovery_area/CFCDB/archivelog/2023_06_05/o1_mf_1_3_l7tzn66t_.arc RECID=13 STAMP=1138717190
Finished backup at 2023/06/05 14:19:53

Starting backup at 2023/06/05 14:19:53
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/oracle/app/db/oradata/CFCDB/system01.dbf
input datafile file number=00003 name=/oracle/app/db/oradata/CFCDB/sysaux01.dbf
input datafile file number=00004 name=/oracle/app/db/oradata/CFCDB/undotbs01.dbf
input datafile file number=00007 name=/oracle/app/db/oradata/CFCDB/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:19:54
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:20:19
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/backupset/2023_06_05/o1_mf_nnndf_TAG20230605T141954_l7tznb2q_.bkp tag=TAG20230605T141954 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00009 name=/oracle/app/db/oradata/CFCDB/CFPDB/system01.dbf
input datafile file number=00010 name=/oracle/app/db/oradata/CFCDB/CFPDB/sysaux01.dbf
input datafile file number=00011 name=/oracle/app/db/oradata/CFCDB/CFPDB/undotbs01.dbf
input datafile file number=00013 name=/oracle/app/db/oradata/CFCDB/CFPDB/cfuser01.dbf
input datafile file number=00012 name=/oracle/app/db/oradata/CFCDB/CFPDB/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:20:19
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:20:34
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/FCE11B3626437C32E053C96FA8C0B1A7/backupset/2023_06_05/o1_mf_nnndf_TAG20230605T141954_l7tzo360_.bkp tag=TAG20230605T141954 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/oracle/app/db/oradata/CFCDB/pdbseed/sysaux01.dbf
input datafile file number=00005 name=/oracle/app/db/oradata/CFCDB/pdbseed/system01.dbf
input datafile file number=00008 name=/oracle/app/db/oradata/CFCDB/pdbseed/undotbs01.dbf
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:20:34
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:20:49
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/FCE1031D37C774FCE053C96FA8C056FA/backupset/2023_06_05/o1_mf_nnndf_TAG20230605T141954_l7tzol8z_.bkp tag=TAG20230605T141954 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:20:50
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:20:51
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/backupset/2023_06_05/o1_mf_ncnnf_TAG20230605T141954_l7tzp2b1_.bkp tag=TAG20230605T141954 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2023/06/05 14:20:51

Starting backup at 2023/06/05 14:20:51
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=4 RECID=14 STAMP=1138717251
channel ORA_DISK_1: starting piece 1 at 2023/06/05 14:20:51
channel ORA_DISK_1: finished piece 1 at 2023/06/05 14:20:52
piece handle=/oracle/app/db/fast_recovery_area/CFCDB/backupset/2023_06_05/o1_mf_annnn_TAG20230605T142051_l7tzp3fv_.bkp tag=TAG20230605T142051 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/oracle/app/db/fast_recovery_area/CFCDB/archivelog/2023_06_05/o1_mf_1_4_l7tzp3c2_.arc RECID=14 STAMP=1138717251
Finished backup at 2023/06/05 14:20:52

Starting Control File Autobackup at 2023/06/05 14:20:52
piece handle=/oracle/backup/20230530/c-1601623831-20230605-00 comment=NONE
Finished Control File Autobackup at 2023/06/05 14:20:53

RMAN>

1.2 查看当前数据库的db_name和instance_name

SQL> select dbid,name from v$database;

      DBID NAME
---------- ---------
1601623831 CFCDB

SQL> show parameter name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cdb_cluster_name                     string
cell_offloadgroup_name               string
db_file_name_convert                 string
db_name                              string      CFCDB
db_unique_name                       string      CFCDB
global_names                         boolean     FALSE
instance_name                        string      CFCDB
lock_name_space                      string
log_file_name_convert                string
pdb_file_name_convert                string
processor_group_name                 string

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      CFCDB

1.3 使用oracle自带的nid工具修改数据库的db_name

1.3.1 需先停库,将库启动到mount状态

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount;
ORACLE instance started.

Total System Global Area 2080374576 bytes
Fixed Size                  9142064 bytes
Variable Size             637534208 bytes
Database Buffers         1426063360 bytes
Redo Buffers                7634944 bytes
Database mounted.

1.3.2 使用nid 修改db_name

SQL> !nid target=sys/Ora#2023_cle dbname=YZCDB

DBNEWID: Release 19.0.0.0.0 - Production on Mon Jun 5 14:36:11 2023

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

Connected to database CFCDB (DBID=1601623831)

Connected to server version 19.10.0

Control Files in database:
    /oracle/app/db/oradata/CFCDB/control01.ctl
    /oracle/app/db/fast_recovery_area/CFCDB/control02.ctl

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

Proceeding with operation
Changing database ID from 1601623831 to 1594212955
Changing database name from CFCDB to YZCDB
    Control File /oracle/app/db/oradata/CFCDB/control01.ctl - modified
    Control File /oracle/app/db/fast_recovery_area/CFCDB/control02.ctl - modified
    Datafile /oracle/app/db/oradata/CFCDB/system01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/sysaux01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/undotbs01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/pdbseed/system01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/pdbseed/sysaux01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/users01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/pdbseed/undotbs01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/system01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/sysaux01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/undotbs01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/users01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/cfuser01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/temp01.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/pdbseed/temp012023-05-30_10-32-18-659-AM.db - dbid changed, wrote new name
    Datafile /oracle/app/db/oradata/CFCDB/CFPDB/temp01.db - dbid changed, wrote new name
    Control File /oracle/app/db/oradata/CFCDB/control01.ctl - dbid changed, wrote new name
    Control File /oracle/app/db/fast_recovery_area/CFCDB/control02.ctl - dbid changed, wrote new name
    Instance shut down

Database name changed to YZCDB.
Modify parameter file and generate a new password file before restarting.
Database ID for database YZCDB changed to 1594212955.
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.

1.3.3 连接到空闲实例,启动到mount状态

[oracle@ora-recover01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Mon Jun 5 14:38:33 2023
Version 19.10.0.0.0

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

Connected to an idle instance.

SQL> startup mount;
ORACLE instance started.

Total System Global Area 2080374576 bytes
Fixed Size                  9142064 bytes
Variable Size             637534208 bytes
Database Buffers         1426063360 bytes
Redo Buffers                7634944 bytes
ORA-01103: database name 'YZCDB' in control file is not 'CFCDB'   
SQL>

1.3.4 ORA-01103: database name ‘YZCDB’ in control file is not ‘CFCDB’

提示控制文件中的数据库名与实际的数据库名不匹配
修改spfile

SQL> alter system set db_name=YZCDB scope=spfile;

System altered.

1.3.5 将库启动到mount状态

SQL> shutdown immediate;
ORA-01507: database not mounted


ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area 2080374576 bytes
Fixed Size                  9142064 bytes
Variable Size             637534208 bytes
Database Buffers         1426063360 bytes
Redo Buffers                7634944 bytes
Database mounted.

1.3.6 查看db_name,将库启动到open resetlogs

SQL> select dbid,name from v$database;

      DBID NAME
---------- ---------
1594212955 YZCDB

SQL> alter database open resetlogs;

Database altered.

注意:此时instance_name还未修改。

SQL> show parameter name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cdb_cluster_name                     string
cell_offloadgroup_name               string
db_file_name_convert                 string
db_name                              string      YZCDB
db_unique_name                       string      YZCDB
global_names                         boolean     FALSE
instance_name                        string      CFCDB
lock_name_space                      string
log_file_name_convert                string
pdb_file_name_convert                string
processor_group_name                 string

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      YZCDB

1.4 修改instance_name

1.4.1 停库

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

1.4.2 修改ORACLE_SID、重命名spfile文件名

[oracle@ora-recover01 ~]$ sed -i s/"ORACLE_SID=CFCDB"/"ORACLE_SID=YZCDB"/g  ~/.bash_profile
[oracle@ora-recover01 ~]$ source ~/.bash_profile
[oracle@ora-recover01 ~]$ env | grep ORACLE_SID
ORACLE_SID=YZCDB

[oracle@ora-recover01 dbs]$ mv spfileCFCDB.ora spfileYZCDB.ora
[oracle@ora-recover01 dbs]$ ll
total 18312
-rw-rw----. 1 oracle oinstall     1544 Jun  5 14:56 hc_CFCDB.dat
-rw-r--r--. 1 oracle oinstall      692 Jun  2 11:21 initCFCDB.ora
-rw-r--r--. 1 oracle oinstall     3079 May 14  2015 init.ora
-rw-r-----. 1 oracle oinstall       24 Jun  2 14:18 lkCFCDB
-rw-r-----. 1 oracle oinstall       24 Jun  5 14:50 lkYZCDB
-rw-r-----. 1 oracle oinstall 18726912 Jun  5 14:52 snapcf_CFCDB.f
-rw-r-----. 1 oracle oinstall     3584 Jun  5 14:52 spfileYZCDB.ora

1.4.3 启动数据库

SQL> startup
ORACLE instance started.

Total System Global Area 2080374576 bytes
Fixed Size                  9142064 bytes
Variable Size             637534208 bytes
Database Buffers         1426063360 bytes
Redo Buffers                7634944 bytes
Database mounted.
Database opened.

此时instance_name已更改。

SQL> show parameter name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cdb_cluster_name                     string
cell_offloadgroup_name               string
db_file_name_convert                 string
db_name                              string      YZCDB
db_unique_name                       string      YZCDB
global_names                         boolean     FALSE
instance_name                        string      YZCDB
lock_name_space                      string
log_file_name_convert                string
pdb_file_name_convert                string
processor_group_name                 string

NAME                                 TYPE        VALUE
------------------------------------ ----------- ----------------------

service_names string YZCDB

1.5 重新注册监听

SQL> alter system register;

System altered.

SQL> host lsnrctl reload

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 05-JUN-2023 15:33:22

Copyright (c) 1991, 2020, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ora-recover01)(PORT=1526)))
The command completed successfully

SQL> host lsnrctl status

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 05-JUN-2023 15:33:32

Copyright (c) 1991, 2020, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ora-recover01)(PORT=1526)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date                01-JUN-2023 14:16:14
Uptime                    4 days 1 hr. 17 min. 18 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /oracle/app/19.3.0/db_1/network/admin/listener.ora
Listener Log File         /oracle/app/db/diag/tnslsnr/ora-recover01/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ora-recover01)(PORT=1526)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1526)))
The listener supports no services
The command completed successfully

1.6 修改密码文件

[oracle@ora-recover01 dbs]$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password='password' entries=3
[oracle@ora-recover01 dbs]$ ll
total 18328
-rw-rw----. 1 oracle oinstall     1544 Jun  5 14:56 hc_CFCDB.dat
-rw-rw----. 1 oracle oinstall     1544 Jun  5 15:01 hc_YZCDB.dat
-rw-r--r--. 1 oracle oinstall      692 Jun  2 11:21 initCFCDB.ora
-rw-r--r--. 1 oracle oinstall     3079 May 14  2015 init.ora
-rw-r-----. 1 oracle oinstall       24 Jun  2 14:18 lkCFCDB
-rw-r-----. 1 oracle oinstall       24 Jun  5 14:50 lkYZCDB
-rw-r-----. 1 oracle oinstall     6144 Jun  5 15:40 orapwYZCDB
-rw-r-----. 1 oracle oinstall 18726912 Jun  5 14:52 snapcf_CFCDB.f
-rw-r-----. 1 oracle oinstall     4608 Jun  5 15:01 spfileYZCDB.ora

注意: 如果listener.ora、 tnsnames.ora文件中含有原来的 SERVICE_NAME 名称的话,用编辑器做相应的修改。
针对数据库自启动文件: /etc/oratab , /etc/rc.local 文件也需要做相应的修改。

06-05 21:07