Oracle12c多租户如何连接到CDB或PDB、CDB与PDB容器切换

时间:2022-03-29 16:46:24
Oracle 数据库 12 c 多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB)。
那么我们如何连接到容器数据库 (CDB) 和可插拔数据库 (PDB)。

1. V$SERVICES视图可以显示数据库中的可用服务。
SQL> col pdb for a40
SQL> col name for a30;
SELECT name, pdb FROM v$services ORDER BY name;SQL> 
NAME       PDB
------------------------------ ----------------------------------------
SYS$BACKGROUND       CDB$ROOT
SYS$USERS       CDB$ROOT
andycdb       CDB$ROOT
andycdbXDB       CDB$ROOT
pdb01       PDB01

-- lsnrctl实用程序显示可用的服务。
[oracle@12c01 ~]$ lsnrctl status
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=12c01)(PORT=1521)))
Services Summary...
Service "4ecf8621e3da38eee0531019640aa598" has 1 instance(s).
  Instance "andycdb", status READY, has 1 handler(s) for this service...
Service "andycdb" has 1 instance(s).
  Instance "andycdb", status READY, has 1 handler(s) for this service...
Service "andycdbXDB" has 1 instance(s).
  Instance "andycdb", status READY, has 1 handler(s) for this service...
Service "pdb01" has 1 instance(s).
  Instance "andycdb", status READY, has 1 handler(s) for this service...
The command completed successfully

2.直接连接到一个容器数据库 (CDB)
连接到数据库的根容器和连接以前版本的单个数据库实例相同。在数据库服务器上,您可以使用操作系统身份验证。

方法一:

[oracle@12c01 ~]$ sqlplus / as sysdba   

方法二:

或者 tnsnames.ora 文件配置

ANDYCDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 12c01)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = andycdb)
    )
  )

C:\Users\andy>sqlplus sys/oracle@10.100.25.16:1521/andycdb as sysdba


3.直接连接到一个可插拔的数据库 (PDB)

 tnsnames.ora 文件配置:

PDB01 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.25.16)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdb01)
    )
  )

C:\Users\andy>sqlplus sys/oracle@10.100.25.16:1521/pdb01 as sysdba
SQL> show con_name
CON_NAME
------------------------------
PDB01
说明:PDB 具有 SYSDBA、 SYSOPER、 SYSBACKUP 或 SYSDG 特权的用户可以连接到已关闭的 PDB。
PDB 的所有其他用户(CONNECT SESSION权限的用户)仅可以连接已经打开的PDB。


4. 容器之间切换

SQL> ALTER SESSION SET container=pdb01;
Session altered.
SQL> show con_name
CON_NAME
------------------------------
PDB01
SQL> alter session set container=cdb$root;
Session altered.
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT