ORACLE_PDB_SID和ORACLE_SID的区别

本文探讨一个很小的问题,很容易被忽略的一个小问题,那就是ORACLE_PDB_SID和ORACLE_SID这两个环境变量;ORACLE_SID这个大家很熟悉了,ORACLE_PDB_SID是CDB模式下的一种连接到某个PDB的方式;当然也可以使用 alter session set container=pdb的方式。来我们看看实例:

复制代码
[oracle@hostname /home/oracle]$ export ORACLE_SID=CDB[oracle@hostname /home/oracle]$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Thu Dec 4 15:11:13 2025Version 19.11.0.0.0Copyright (c) 1982, 2020, Oracle.  All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0SQL> show con_nameCON_NAME------------------------------CDB$ROOTSQL> show pdbs;    CON_ID CON_NAME                       OPEN MODE  RESTRICTED---------- ------------------------------ ---------- -----------         2 PDB$SEED                       READ ONLY  NO         3 PDB1                           READ WRITE NOSQL> exitDisconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0[oracle@hostname /home/oracle]$ export ORACLE_PDB_SID=PDB1[oracle@hostname /home/oracle]$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Thu Dec 4 15:12:58 2025Version 19.11.0.0.0Copyright (c) 1982, 2020, Oracle.  All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0SQL> show con_name;CON_NAME------------------------------PDB1SQL> exitDisconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0

这一段很简单就是连接到19.11的CDB环境,CDB下有一个PDB1,设置完ORACLE_PDB_SID后可以切换至PDB下。

复制代码
[oracle@hostname /home/oracle]$ export ORACLE_SID=CDB[oracle@hostname /home/oracle]$ sqlplus / as sysdba

紧接上文,如果再执行export ORACLE_SID之后再次sqlplus 请问当前是连接到哪个库?

------分割线------

你会发现连接的还是PDB,除非你执行unset ORACLE_PDB_SID

复制代码
[oracle@hostname /home/oracle]$ export ORACLE_SID=CDB[oracle@hostname /home/oracle]$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Thu Dec 4 15:11:13 2025Version 19.11.0.0.0Copyright (c) 1982, 2020, Oracle.  All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0SQL> show con_nameCON_NAME------------------------------PDB1
复制代码
[oracle@hostname /home/oracle]$ unset ORACLE_PDB_SID[oracle@hostname /home/oracle]$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Thu Dec 4 15:14:11 2025Version 19.11.0.0.0Copyright (c) 1982, 2020, Oracle.  All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0SQL> show con_name;CON_NAME------------------------------CDB$ROOTSQL> show pdbs;    CON_ID CON_NAME                       OPEN MODE  RESTRICTED---------- ------------------------------ ---------- -----------         2 PDB$SEED                       READ ONLY  NO         3 PDB1                           READ WRITE NOSQL> exitDisconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.11.0.0.0

why?

这个其实很容易理解在 Oracle 18c/19c/21c 的多租户环境下做本地 bequeath 连接(sqlplus / as sysdba)时:ORACLE_SID 只负责"先找到哪个 CDB 实例",而 ORACLE_PDB_SID 负责"最终把我扔进哪个 PDB"。

只要 ORACLE_PDB_SID 存在且指向一个真实打开的 PDB,它就会完全覆盖 ORACLE_SID 的默认行为,把你直接带进那个 PDB。最终验证结果如下:

  • 从 18c 开始(包括所有 19c 版本)开始,Oracle 在数据库内部默认创建了一个隐藏的系统触发器:
    SYS.DBMS_SET_PDB(after logon on database)

  • 该触发器只对 SYS 和 SYSTEM 用户生效,核心代码如下:

    复制代码
    DBMS_SYSTEM.get_env('ORACLE_PDB_SID', pdb_name);if(pdb_name is not null) then    EXECUTE IMMEDIATE 'alter session set container = ' || pdb_name;end if;
  • 关于这两个环境变量官方文档明确写了一句关键话:
    "The environment variable ORACLE_SID's value must be declared as well for this direct PDB bequeath connection attempt to successfully work (after all, we initially are still connecting to the database root container)."翻译白话:
    ORACLE_SID 必须先设置(用来找到 CDB 实例),但真正决定你最终落在哪个容器里的,是 ORACLE_PDB_SID。

  • 参考文档

Performing bequeath direct connections to PDB as SYS and SYSTEM (Doc ID 2728684.1)

相关推荐
AIgorithmGEEK2 小时前
MySQL 索引全解(下):建、查、删实战与知识速查表
数据库·mysql·b+树·索引
唠点键盘之外的5 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
云和恩墨5 小时前
数据库监控简史:监控不止于告警——从图表大爆发到健康管理闭环
数据库
ly76895 小时前
MongoDB 事务开销实测:快照隔离下的写冲突、重试与分片事务边界
数据库·mongodb·事务·分片集群·快照隔离·写冲突
豆角焖肉5 小时前
Spring AOP 自定义注解实现系统操作日志
数据库·自定义注解·springaop·操作日志·aop 切面
金伟API10246 小时前
不会写复杂SQL怎么办?
数据库·sql
执笔为剑7 小时前
2.1.2-1、V8R3迁移到V8R6-Oracle兼容特性
数据库
江湖有缘7 小时前
Docker实战 | 使用Docker部署rmoments社交平台
jvm·docker·oracle
geovindu7 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构
for_ever_love__7 小时前
MySQL SELECT 与 WHERE 优化实战:可搜索条件、联合索引与执行计划
数据库·mysql·oracle·select·where·条件优化·查询优化技巧