Connecting to Container Databases (CDB) and Pluggable Databases (PDB) in Oracle Database 12c

本文详细介绍了如何连接到Oracle数据库容器数据库的根容器,包括使用OS认证、SQL*Plus命令进行会话切换和使用lsnrctl工具查看服务状态。文章还展示了如何通过TNS配置文件管理不同容器和服务,并提供了在不同容器间切换的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Connecting to the root of a container database is the same as that of any previous database instance. On the database server you can use OS Authentication

[oracle@vmdb12c ~]$ sqlplus / as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:06:34 2013

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


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT

The V$SERVICES views can be used to display available services from the database.

SQL> select name,pdb from v$services order by 2;

NAME                 PDB
-------------------- ----------
zwcXDB               CDB$ROOT
SYS$BACKGROUND       CDB$ROOT
SYS$USERS            CDB$ROOT
zwc                  CDB$ROOT
zhongwc1             ZHONGWC1
zhongwc2             ZHONGWC2

6 rows selected.

The lsnrctl utility allows you to display the available services from the command line.

[oracle@vmdb12c ~]$ lsnrctl status

LSNRCTL for Linux: Version 12.1.0.1.0 - Production on 06-SEP-2013 22:10:41

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=vmdb12c)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 12.1.0.1.0 - Production
Start Date                06-SEP-2013 19:32:46
Uptime                    0 days 2 hr. 37 min. 54 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/vmdb12c/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vmdb12c)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "zhongwc1" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
Service "zhongwc2" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
Service "zwc" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
Service "zwcXDB" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@vmdb12c ~]$ lsnrctl service

LSNRCTL for Linux: Version 12.1.0.1.0 - Production on 06-SEP-2013 22:10:45

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=vmdb12c)(PORT=1521)))
Services Summary...
Service "zhongwc1" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:5 refused:0 state:ready
         LOCAL SERVER
Service "zhongwc2" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:5 refused:0 state:ready
         LOCAL SERVER
Service "zwc" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:5 refused:0 state:ready
         LOCAL SERVER
Service "zwcXDB" has 1 instance(s).
  Instance "zwc", status READY, has 1 handler(s) for this service...
    Handler(s):
      "D000" established:0 refused:0 current:0 max:1022 state:ready
         DISPATCHER <machine: vmdb12c, pid: 4294>
         (ADDRESS=(PROTOCOL=tcp)(HOST=vmdb12c)(PORT=34716))
The command completed successfully

Connections using services are unchanged from previous versions.

easy connect

[oracle@vmdb12c ~]$ sqlplus system/oracle@192.168.1.14:1521/zhongwc1

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:13:37 2013

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

Last Successful login time: Fri Sep 06 2013 22:13:31 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         3 ZHONGWC1                       READ WRITE NO
SQL> show con_name

CON_NAME
------------------------------
ZHONGWC1
[oracle@vmdb12c ~]$ sqlplus system/oracle@192.168.1.14:1521/zhongwc2

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:13:48 2013

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

Last Successful login time: Fri Sep 06 2013 21:26:00 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         4 ZHONGWC2                       READ WRITE NO
SQL> show con_name

CON_NAME
------------------------------
ZHONGWC2

TNS
[oracle@vmdb12c ~]$ sqlplus system/oracle@zhongwc1

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:15:42 2013

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

Last Successful login time: Fri Sep 06 2013 22:13:37 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         3 ZHONGWC1                       READ WRITE NO
SQL> show con_name

CON_NAME
------------------------------
ZHONGWC1
[oracle@vmdb12c ~]$ sqlplus system/oracle@zhongwc2

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:15:47 2013

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

Last Successful login time: Fri Sep 06 2013 22:13:48 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show con_name

CON_NAME
------------------------------
ZHONGWC2
SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         4 ZHONGWC2                       READ WRITE NO

tnsnames.ora

ZWC =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = vmdb12c)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = zwc)
    )
  )


zhongwc1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = vmdb12c)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = zhongwc1)
    )
  )


zhongwc2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = vmdb12c)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = zhongwc2)
    )
  )

LISTENER_ZWC =
  (ADDRESS = (PROTOCOL = TCP)(HOST = vmdb12c)(PORT = 1521))

When logged in to the CDB as an appropriately privileged user, the ALTER SESSION command can be used to switch between containers within the container database.

[oracle@vmdb12c ~]$ sqlplus / as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Fri Sep 6 22:17:52 2013

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


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> alter session set container=zhongwc1;

Session altered.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         3 ZHONGWC1                       READ WRITE NO
SQL> alter session set container=zhongwc2;

Session altered.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         4 ZHONGWC2                       READ WRITE NO
SQL> alter session set container=cdb$root;

Session altered.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ZHONGWC1                       READ WRITE NO
         4 ZHONGWC2                       READ WRITE NO
SQL> select sys_context('USERENV','CON_NAME') from dual;

SYS_CONTEXT('USERENV','CON_NAME')
--------------------------------------------------------------------------------
CDB$ROOT

SQL> alter session set container=zhongwc1;

Session altered.

SQL> select sys_context('USERENV','CON_NAME') from dual;

SYS_CONTEXT('USERENV','CON_NAME')
--------------------------------------------------------------------------------
ZHONGWC1

SQL> alter session set container=zhongwc2;

Session altered.

SQL> select sys_context('USERENV','CON_NAME') from dual;

SYS_CONTEXT('USERENV','CON_NAME')
--------------------------------------------------------------------------------
ZHONGWC2





<think>首先,用户的问题是:我想查询 Oracle 19c 中所有的 CDBPDB。用户需要查看所有 CDBPDB 的列表。 从系统级指令来看: - 所有行内数学表达式必须使用 $...$ 格式,但这个问题是关于 Oracle 数据库的,不涉及数学表达式,所以可能不需要。 - 独立公式必须使用 $$...$$ 格式,但同样,这里没有数学公式。 - LaTeX 语法正确:确保任何数学表达式正确,但这里不适用。 - 使用中文回答:我的回答必须用中文。 - 生成相关问题:在回答结束时,生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识:例如 [^1]。用户提供了几个引用,我需要参考它们。 用户提供的引用: - 引用[1]: 关于 CDB 安装、PDB 安装,查询 CDB 名称,创建 PDB 的步骤。 - 引用[2]: 关于 CDBPDB 的关系,创建 PDB 的方法。 - 引用[3]: 解释 CDBPDB,提到默认 PDBPDB$SEED。 - 引用[4]: 关于 CDBPDB 的转换。 这些引用是关于 Oracle 19c 的 CDBPDB 的安装和操作,但用户的问题是查询所有 CDBPDB 的列表。所以,我需要基于这些信息,提供查询方法。 关键点: - CDB (Container Database) 是容器数据库。 - PDB (Pluggable Database) 是可插拔数据库。 - 在 Oracle 19c 中,CDB 包含多个 PDB。 要查询所有 CDBPDB,我需要提供 SQL 查询语句。 从引用中: - 引用[1] 提到:SELECT PDB_ID, PDB_NAME, GUID, STATUS FROM DBA_PDBS ORDER BY PDB_ID; 这查询了 PDB 信息。 - 引用[4] 提到:在 sqlplus 下用 sys 登陆 CDB 数据库。 所以,查询 PDB 列表可以使用 DBA_PDBS 视图。 对于 CDB,通常只有一个 CDB per instance, but in Oracle, a CDB can have multiple PDBs. To see the CDB itself, we might query v$database or similar. Common ways: - To see the CDB name: SELECT NAME FROM V$DATABASE; - To see all PDBs: SELECT PDB_NAME, STATUS FROM DBA_PDBS; or using CDB_ views if connected to CDB. Since the user is asking for all CDB and PDB, I need to clarify that in a typical setup, there is one CDB containing multiple PDBs. So, querying the CDB name and then the list of PDBs. Steps to query: 1. Connect to the CDB as a privileged user (like SYS). 2. Query the CDB name. 3. Query the PDBs. SQL queries: - For CDB: SELECT NAME FROM V$DATABASE; -- This gives the CDB name. - For PDBs: SELECT PDB_ID, PDB_NAME, STATUS FROM DBA_PDBS; -- This lists all PDBs in the CDB. Also, from reference [3], it mentions that there is a default PDB and PDB$SEED. In Oracle 19c, when connected to CDB root, you can see all PDBs. To show all PDBs including their open status, you might use: SELECT NAME, OPEN_MODE FROM V$PDBS; But DBA_PDBS is also fine. Now, for the answer structure: - Answer should be clear and step-by-step. - Use Chinese. - Add references naturally. Structure my response: 1. Explain briefly what CDB and PDB are for context. 2. Provide step-by-step instructions to query: a. Connect to the CDB. b. Query CDB name. c. Query PDB list. 3. Include SQL examples. 4. Add references where applicable. 5. End with generated related questions. References to add: - When mentioning querying DBA_PDBS, refer to [^1]. - When explaining CDB and PDB, refer to [^2] or [^3]. From references: - [^1] has the query: SELECT PDB_ID, PDB_NAME,GUID,STATUS FROM DBA_PDBS ORDER BY PDB_ID; - [^3] explains CDB and PDB. - [^4] shows connecting as SYS. So, I can cite them. Finally, generate related questions at the end.</think>### 查询 Oracle 19c 中所有 CDBPDB 列表的方法 在 Oracle 19c 中,CDBContainer Database,容器数据库)是一个包含多个 PDBPluggable Database,可插拔数据库)的数据库容器。每个 CDB 实例通常包含一个根容器(CDB$ROOT)和多个 PDB。要查询所有 CDBPDB 的列表,您需要以特权用户(如 SYS)连接到 CDB 根容器,然后执行特定的 SQL 查询。以下是详细步骤和示例代码: #### 步骤 1: 连接到 CDB 根容器 首先,使用 SQL*Plus 或其他工具以 `SYS` 用户(或具有 `SYSDBA` 权限的用户)连接到 CDB 根容器。参考引用 [^4],连接命令如下: ```sql sqlplus sys/您的密码 as sysdba ``` - 确保您已正确设置 Oracle 环境变量(如 `ORACLE_SID`),指向目标 CDB 实例。 - 如果连接成功,您会看到类似输出:`Connected to: Oracle Database 19c Enterprise Edition`。 #### 步骤 2: 查询 CDB 名称 CDB 名称可以通过 `V$DATABASE` 视图获取。执行以下 SQL: ```sql SELECT NAME AS CDB_NAME FROM V$DATABASE; ``` - **输出示例**: ``` CDB_NAME --------- ORCLCDB ``` - 这表示当前连接的 CDB 名称(例如 `ORCLCDB`)。在 Oracle 19c 中,一个实例通常只运行一个 CDB,因此该查询返回当前活动的 CDB 名称 [^3]。 #### 步骤 3: 查询所有 PDB 列表 要查看该 CDB 中的所有 PDB,包括状态和名称,使用 `DBA_PDBS` 视图(或 `V$PDBS` 视图)。参考引用 [^1],推荐查询: ```sql SELECT PDB_ID, PDB_NAME, STATUS, GUID FROM DBA_PDBS ORDER BY PDB_ID; ``` - **输出示例**: ``` PDB_ID PDB_NAME STATUS GUID ------- -------------- --------- ---------------------------------- 1 PDB$SEED NORMAL 6F1B4A9C8E7F3D12E0530A000AFC4A1C 2 ORCLPDB1 NORMAL 7A2B5C3D9E8F1A23E0530A000AFC8B2D 3 ORCLPDB2 NORMAL 8B3C6D4F0A9B1C34E0530A000AFCD3E4 ``` - **关键字段解释**: - `PDB_NAME`: PDB 的名称(如 `ORCLPDB1`)。 - `STATUS`: PDB 的状态(`NORMAL` 表示正常,`NEW` 表示新创建)。 - `GUID`: PDB 的唯一标识符。 - 默认包含 `PDB$SEED`,这是 PDB 的模板数据库 [^3]。 如果需要更详细的状态(如 PDB 是否已打开),使用 `V$PDBS` 视图: ```sql SELECT PDB_ID, NAME AS PDB_NAME, OPEN_MODE FROM V$PDBS; ``` - **输出示例**: ``` PDB_ID PDB_NAME OPEN_MODE ------- -------------- --------- 2 ORCLPDB1 READ WRITE 3 ORCLPDB2 MOUNTED ``` #### 注意事项 - **权限要求**:执行这些查询需要 `SYSDBA` 或 `DBA` 权限。普通用户可能无法访问 `DBA_PDBS` 或 `V$PDBS` 视图。 - **多 CDB 环境**:Oracle 19c 通常一个实例只运行一个 CDB。如果您的系统有多个 CDB(例如通过不同实例),需要分别连接到每个 CDB 重复上述步骤。 - **PDB 状态**:`STATUS` 为 `NEW` 的 PDB 可能需要运行 `noncdb_to_pdb.sql` 脚本来完成转换 [^2]。 - **引用说明**:以上方法基于 Oracle 19c 的标准实践,确保您的数据库已正确安装和配置 [^1][^3]。 如果您在查询中遇到问题(如视图不存在),请检查数据库版本和用户权限。 ### 相关问题 1. 如何创建新的 PDB 数据库? 2. 如何切换或连接到特定的 PDB? 3. Oracle 19c 中 CDBPDB 的管理有哪些最佳实践? 4. 如何备份和恢复 PDB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值