1. 使用sqlplus登錄可插拔數據庫
[[oracle@node1 ~]$ sqlplus / as sysdba
2. 通過con_name查看CDB包含的pdb信息。
SQL> show con_name pdbs;
CON_NAME
------------------------------
CDB$ROOT
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ WRITE NO
3 QXY11PDB READ WRITE NO
4 PDB02 READ WRITE NO
SQL>
3. 創建公用用戶qxy, 需要使用C##或者c##作爲該用戶名的開頭。
SQL> create user c##qxy identified by tiger;
User created.
SQL>
如果使用傳統創建用戶的方法會報錯
SQL> create user qxy identified by tiger;
create user qxy identified by tiger
*
ERROR at line 1:
ORA-65096: invalid common user or role name
SQL>
4. 授權
SQL> grant dba to c##qxy identified by tiger container = all;
5. 下面使用公用用戶分別登錄CDB、PDB看看是否通用。 登錄PDB使用了tnsnames中已經配置好的鏈接字符串@pdb02
[oracle@node1 admin]$ sqlplus c##qxy/tiger
SQL*Plus: Release 12.2.0.1.0 Production on Mon Apr 9 13:01:07 2018
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>
--------連接pdb
[oracle@node1 admin]$ sqlplus c##qxy/tiger@pdb02
SQL*Plus: Release 12.2.0.1.0 Production on Mon Apr 9 13:04:04 2018
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Last Successful login time: Mon Apr 09 2018 13:03:46 +08:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> show con_name
CON_NAME
------------------------------
PDB02
SQL>
鏈接字符串如下:
pdb02 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.40.180)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pdb02.localdomain)
)
)
)
6. 在PDB中創建用戶和創建普通用戶就沒有什麼區別了
SQL> show con_name
CON_NAME
------------------------------
PDB02
SQL>
SQL> create user scott identified by tiger ;
User created.
SQL> grant resource,connect to scott;
Grant succeeded.
SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
[oracle@node1 admin]$ sqlplus scott/tiger@pdb02
SQL*Plus: Release 12.2.0.1.0 Production on Mon Apr 9 13:06:55 2018
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> show con_name
CON_NAME
------------------------------
PDB02
SQL>
7. CDB和PDB之間的切換方式
SQL > alter session set container = pdb02;
SQL > alter session set container = cdb$root;