how to get same dbid

本文介绍如何使用dbms_backup_restore而非nid来更改Oracle数据库名称和ID(DBID),包括打开数据库为只读模式、设置新的数据库名称和ID、进行更改以及更新数据库参数等步骤。

https://blog.pythian.com/how-to-choose-your-oracle-database-id-dbid

 

How To Choose Your Oracle Database ID (DBID)

by Grégory Guillou

 

February 18, 2009

Posted in: Technical Track

Tags: Oracle

You can choose a DBID when you rename your Oracle database. This is probably a bad, unsupported, and useless idea. I assume this hidden feature can help you to mess up all your backups. So my advice would be: “don’t use it.”

I performed this test with Oracle 11.1.0.7 on Linux x86. It consists in using dbms_backup_restoreinstead of nid to rename the database. You’ll find below the few steps require to get to it.

 

Step 1. Open The Database In Read-Only Mode

First, stop the instance with an immediate shutdown. If we were to use nid, we would mount the instance, but with dbms_backup_restore, we need to access the package. For this reason, we have to open the database in read-only mode. Here are the commands I ran:

sqlplus / as sysdba

shutdown immediate;

startup open read only;

Step 2. Get The Old Values; Set The New Ones…

Once we can access the database, we can check its NAME and DBID. The script below does these checks and prompts the user for the new NAME and DBID. To that result, it queries V$DATABASE:

var old_name varchar2(20)
var old_dbid number
var new_name varchar2(20)
var new_dbid number

exec select name, dbid -
       into :old_name,:old_dbid -
       from v$database

print old_name

accept new_name prompt "Enter the new Database Name:"
Enter the new Database Name:FRANCE

accept new_dbid prompt "Enter the new Database ID:"
Enter the new Database ID:1

exec :new_name:='&&new_name'
exec :new_dbid:=&&new_dbid

Step 3. Make The Changes

I won’t go into all the details of the package. Instead, here is PL/SQL block you can run to make the change:

set serveroutput on
exec dbms_output.put_line('Convert '||:old_name||  -
     '('||to_char(:old_dbid)||') to '||:new_name|| -
     '('||to_char(:new_dbid)||')')

Convert BLACK(361377223) to FRANCE(1)

declare
  v_chgdbid   binary_integer;
  v_chgdbname binary_integer;
  v_skipped   binary_integer;
begin
  dbms_backup_restore.nidbegin(:new_name,
       :old_name,:new_dbid,:old_dbid,0,0,10);
  dbms_backup_restore.nidprocesscf(
       v_chgdbid,v_chgdbname);
  dbms_output.put_line('ControlFile: ');
  dbms_output.put_line('  => Change Name:'
       ||to_char(v_chgdbname));
  dbms_output.put_line('  => Change DBID:'
       ||to_char(v_chgdbid));
  for i in (select file#,name from v$datafile)
     loop
     dbms_backup_restore.nidprocessdf(i.file#,0,
       v_skipped,v_chgdbid,v_chgdbname);
     dbms_output.put_line('DataFile: '||i.name);
     dbms_output.put_line('  => Skipped:'
       ||to_char(v_skipped));
     dbms_output.put_line('  => Change Name:'
       ||to_char(v_chgdbname));
     dbms_output.put_line('  => Change DBID:'
       ||to_char(v_chgdbid));
     end loop;
  for i in (select file#,name from v$tempfile)
     loop
     dbms_backup_restore.nidprocessdf(i.file#,1,
       v_skipped,v_chgdbid,v_chgdbname);
     dbms_output.put_line('DataFile: '||i.name);
     dbms_output.put_line('  => Skipped:'
       ||to_char(v_skipped));
     dbms_output.put_line('  => Change Name:'
       ||to_char(v_chgdbname));
     dbms_output.put_line('  => Change DBID:'
       ||to_char(v_chgdbid));
     end loop;
  dbms_backup_restore.nidend;
end;
/

ControlFile:
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/system01.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/sysaux01.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/undotbs01.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/users01.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/streams_tbs.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1
DataFile: /u01/app/oracle/oradata/BLACK/temp01.dbf
=> Skipped:0
=> Change Name:1
=> Change DBID:1

Step 4. Change Db_name And Open The Database

Before you can open the database, you have to change the db_name parameter in the spfile. Once you’ve done so, you should be able to open it with resetlogs. That’s the script I ran to get to that result:

create pfile from spfile;

!cat initBLACK.ora | \
   sed "s/db_name='BLACK'/db_name='FRANCE'/" \
   > initFRANCE.ora

shutdown immediate;

startup mount pfile=initFRANCE.ora

alter database open resetlogs;

create spfile from pfile='initFRANCE.ora';

startup force;

Conclusion

I probably have the coolest Database Name and ID in the world now:

select name, dbid from v$database;

NAME		DBID
--------- ----------
FRANCE		   1

But if you think about it, I also changed some information in the datafile, even though the database was opened in read-only mode. Interesting?

<think>嗯,用户问的是为什么在使用RMAN恢复数据库时需要指定DBID。首先,我得回忆一下RMAN的基本知识。DBID是数据库的唯一标识符,每个数据库都有一个独特的DBID,这个值在数据库创建时生成。 那为什么恢复的时候需要这个DBID呢?可能跟恢复过程中识别正确的备份文件有关。RMAN在备份的时候会把DBID包含在备份信息里,这样在恢复的时候,通过指定DBID,RMAN可以确认正在恢复的是正确的数据库,避免错误地使用其他数据库的备份。 另外,如果控制文件丢失或损坏,可能需要从自动备份中恢复控制文件。这时候,RMAN需要知道DBID来定位正确的控制文件备份,因为控制文件的自动备份通常包含DBID作为文件名的一部分。如果没有指定DBID,RMAN可能无法找到正确的备份,导致恢复失败。 还有,当使用恢复目录时,DBID帮助RMAN连接到正确的数据库记录。不过,即使没有恢复目录,直接使用目标数据库的控制文件,DBID仍然在恢复过程中起到关键作用,尤其是在控制文件丢失的情况下。 需要注意的一点是,如果用户不记得DBID,可能需要通过其他方式获取,比如查看历史备份的文件名,或者数据库的某些日志文件,因为DBID有时会记录在这些地方。此外,如果是在同一台服务器上有多个数据库,指定DBID可以避免混淆,确保恢复的是正确的数据库实例。 总结一下,DBID在RMAN恢复中的作用主要是确保使用正确的备份文件,尤其是在控制文件不可用的情况下,帮助RMAN定位和验证备份的有效性。这增加了恢复过程的安全性和准确性,防止数据恢复错误。</think>在使用Oracle RMAN进行数据库恢复时,指定**DBID**(Database Identifier)的主要原因如下: --- ### 1. **唯一标识数据库** 每个Oracle数据库在创建时都会生成一个**全局唯一的数字标识符(DBID)**。当出现以下场景时,必须通过DBID明确指定目标数据库: - **控制文件丢失**:如果数据库的控制文件全部丢失,RMAN无法自动识别数据库身份。 - **多数据库环境**:同一服务器存在多个数据库时,需通过DBID区分不同数据库的备份。 --- ### 2. **定位控制文件自动备份** 当使用`RMAN> restore controlfile from autobackup;`恢复控制文件时: - RMAN默认会在特定路径(如`$ORACLE_HOME/dbs`)中搜索**格式为`c-<DBID>-<日期>`的控制文件自动备份**。 - 如果不指定DBID,RMAN无法确定应使用哪个备份文件。 --- ### 3. **恢复验证** DBID确保了恢复操作针对**正确的数据库备份集**,避免因备份文件混淆导致数据错误。 --- ### 如何获取DBID? - **未丢失控制文件**: ```sql SQL> SELECT DBID FROM V$DATABASE; ``` - **仅剩备份文件**: 检查RMAN备份文件名(通常包含DBID),例如: ``` c-1234567890-20231015-00 ``` 其中`1234567890`即为DBID。 --- ### 恢复时的操作示例 ```rman RMAN> STARTUP NOMOUNT; RMAN> SET DBID 1234567890; -- 指定DBID RMAN> RESTORE CONTROLFILE FROM AUTOBACKUP; RMAN> ALTER DATABASE MOUNT; RMAN> RECOVER DATABASE; RMAN> ALTER DATABASE OPEN RESETLOGS; ``` --- ### 总结 | 场景 | 是否需要指定DBID | |--------------------------|------------------------| | 控制文件可用 | 不需要(RMAN自动识别) | | 控制文件丢失且无恢复目录 | **必须指定** | | 多数据库环境 | **必须指定** | 通过DBID,RMAN能精准定位备份文件,确保恢复操作的正确性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值