Oracle

如何在不同的 PDB 中創建相同的使用者,而它存在於其他 PDB 中?

  • July 29, 2020

我在 CDB 中有 100 個 PDB 的列表,假設 account_user 存在於 50 個 PDB 中,並在其他 50 個 PDB 中創建相同的 account_user 而不是登錄每個 PDB 並創建使用者,有沒有辦法用單個命令或腳本來做到這一點?

當我從 CDB 創建使用者時,出現以下錯誤:

ORA-01920: 使用者名 ‘account_user’ 與另一個使用者名或角色名衝突

也不是 container=all 將起作用,因為我不想從前 50 個 PDB 中刪除使用者並重新創建。

當然。

SQL> conn / as sysdba
Connected.
SQL> select pdb_name from cdb_pdbs order by 1;

PDB_NAME
--------------------
PDB$SEED
PDB1
PDB2
PDB3

SQL> select u.username, p.pdb_name from cdb_users u 
    join cdb_pdbs p on (u.con_id = p.pdb_id) where u.username = 'BP';

USERNAME             PDB_NAME
-------------------- --------------------
BP                   PDB1

只需生成腳本:

SQL> select 'alter session set container=' ||pdb_name || ';' || chr(10) || 'CREATE USER BP IDENTIFIED BY BP;' as script from cdb_pdbs p where not exists (select 1 from cdb_users u where u.username = 'BP' and u.con_id = pdb_id) and pdb_name not in ('CDB$ROOT', 'PDB$SEED') ;

SCRIPT
--------------------------------------------------------------------------------
alter session set container=PDB3;
CREATE USER BP IDENTIFIED BY BP;

alter session set container=PDB2;
CREATE USER BP IDENTIFIED BY BP;

然後執行它:

SQL>

alter session set container=PDB3;
CREATE USER BP IDENTIFIED BY BP;

alter session set container=PDB2;
CREATE USER BP IDENTIFIED BY BP;

Session altered.

SQL>
User created.

SQL> SQL>
Session altered.

SQL>
User created.

SQL> alter session set container=CDB$ROOT;

Session altered.

SQL> select u.username, p.pdb_name from cdb_users u
    join cdb_pdbs p on (u.con_id = p.pdb_id) where u.username = 'BP';

USERNAME             PDB_NAME
-------------------- --------------------
BP                   PDB1
BP                   PDB2
BP                   PDB3

SQL>

引用自:https://dba.stackexchange.com/questions/271848