Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12038
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 54
  • 用 户 组: 普通用户
  • 注册时间: 2021-04-25 14:19
个人简介

专注于云计算

文章分类
文章存档

2021年(12)

我的朋友

分类: Oracle

2021-04-28 11:48:08

原文地址:优化多租户下的SQL提示符 作者:brjl


19c 多租户下为了好区分登录位置,可以修改$ORACLE_HOME/sqlplus/admin/glogin.sql,加入:

set serveroutput on size 1000000
set trimspool on
set long 5000
set linesize 150
set pagesize 1000

col plan_plus_exp format a80
col global_name new_value gname
col member for a60
col file_name for a60
col table_name format a30
col index_name format a30
col owner format a20

set heading off
set termout off
define gname=idle
col global_name new_value gname
col global_name noprint
select lower(sys_context('userenv','current_user'))||'@'||sys_context('userenv','con_name')||':'||sys_context('userenv','db_name')||':'||userenv('sid') global_name from dual;
set sqlprompt '&gname> '
set heading on
set termout on

但遗憾的是还不能对alter session set container=PDB2后进行动态修改,解决方法是:
@sw pdb2

sw.sql 脚本内容:
alter session set container = &1;
@$ORACLE_HOME/sqlplus/admin/glogin.sql

效果如下:

[oracle@db1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 9 00:16:13 2021
Version 19.9.0.0.0

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


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.9.0.0.0

sys@CDB$ROOT:orcl:226> sho pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB1                           READ WRITE NO
         4 PDBNEW                         READ WRITE NO
         5 PDBESB                         READ WRITE NO
         7 PDB3                           READ WRITE NO
sys@CDB$ROOT:orcl:226> show con_name

CON_NAME
------------------------------
CDB$ROOT
sys@CDB$ROOT:orcl:226> @sw pdb3
old   1: alter session set container = &1
new   1: alter session set container = pdb3

Session altered.

sys@PDB3:PDB3:226> show con_name

CON_NAME
------------------------------
PDB3
sys@PDB3:PDB3:226> 

sys@PDB3:PDB3:226> @sw pdbnew
old   1: alter session set container = &1
new   1: alter session set container = pdbnew

Session altered.

sys@PDBNEW:PDBNEW:226> show con_name

CON_NAME
------------------------------
PDBNEW
sys@PDBNEW:PDBNEW:226> @sw pdbesb
old   1: alter session set container = &1
new   1: alter session set container = pdbesb

Session altered.

sys@PDBESB:PDBESB:226> sho pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         5 PDBESB                         READ WRITE NO
sys@PDBESB:PDBESB:226> 

sys@PDBESB:PDBESB:226> @sw pdb1
old   1: alter session set container = &1
new   1: alter session set container = pdb1

Session altered.

sys@PDB1:PDB1:226> @sw pdb2
old   1: alter session set container = &1
new   1: alter session set container = pdb2
ERROR:
ORA-65011: Pluggable database PDB2 does not exist.

sys@PDB1:PDB1:226> sho con_name

CON_NAME
------------------------------
PDB1
sys@PDB1:PDB1:226> 

因此,sw.sql适合以 sys 身份登录容器数据库,然后随便切换到各pdb,不适合pdb里用户登录后再切到其他pdb上,因为pdb1里的用户a可能在pdb3里不存在。

各 pdb 用户建议用以下方式来登录。
SQL> conn 用户/密码@IP/PDB库名

sys@PDB3:PDB3:310> conn x/x@db1/pdb3

Good Luck!
阅读(2018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~