简体   繁体   English

如何为 Oracle SQL 中的用户添加 ALTER SESSION 权限

[英]How do i add ALTER SESSION privileges to a user in Oracle SQL

I created a user in SQL using,我使用 SQL 创建了一个用户,

CREATE USER opt IDENTIFIED BY pass;

User got created.用户已创建。 Then I try to grant certain privileges to the user,然后我尝试授予用户某些权限,

    SQL> grant unlimited tablespace ,create session to opt;

    Grant succeeded.
    SQL> grant alter session to opt;
 
    Grant succeeded.

I connect to OPT user and then an alter session, but I am getting insufficient privileges error,我连接到 OPT 用户,然后更改 session,但出现权限不足错误,

SQL> alter session set some_variable=0;
ERROR:
ORA-01031: insufficient privileges

I am new to this, please tell me where I am wrong.我是新手,请告诉我哪里错了。 Thanks in advance.提前致谢。 :) :)

Edit: After I read Roberto's answer, I tried adding编辑:在我阅读罗伯托的回答后,我尝试添加

SQL> alter session set optimizer_mode=first_rows ;

Session altered.

This is working fine.这工作正常。 But when I do但是当我这样做的时候

SQL> alter session set sql_trace=true ;
ERROR:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-20000: Missing database property for object store credentials

and

SQL> alter session set optimizer_dynamic_sampling=0;
ERROR:
ORA-01031: insufficient privileges

If your user is granted with the create session privilege, most of the alter session statements are possible, except the ones which require specifically the alter session privilege.如果您的用户被授予create session权限,则大多数alter session语句都是可能的,除了那些特别需要alter session权限的语句。

Demo演示

SQL> create user test6 identified by Oracle_1234 ;

User created.

SQL> grant create session to test6 ;

Grant succeeded.

SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0

We now connect with the new user我们现在与新用户建立联系

$ sqlplus test6/Oracle_1234

SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 7 14:03:57 2021
Version 19.6.0.0.0

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


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

SQL> alter session enable parallel dml ;

Session altered.

SQL> alter session enable parallel query ;

Session altered.

SQL> alter session set optimizer_mode=first_rows ;

Session altered.

SQL> alter session set sql_trace= true ;
ERROR:
ORA-01031: insufficient privileges

As the documentation states, in the following cases you would need to grant the privilege directly如文档所述,在以下情况下,您需要直接授予权限

  1. To enable and disable the SQL trace facility, you must have ALTER SESSION system privilege.要启用和禁用 SQL 跟踪工具,您必须具有ALTER SESSION系统权限。

  2. To enable or disable resumable space allocation, you must have the RESUMABLE system privilege.要启用或禁用resumable空间分配,您必须具有 RESUMABLE 系统特权。

  3. You do not need any privileges to perform the other operations of this statement unless otherwise indicated.除非另有说明,否则您不需要任何权限即可执行此语句的其他操作。

If we grant the privilege, the enable of trace will work如果我们授予权限,跟踪的启用将起作用

SQL> grant alter session to test6 ;

Grant succeeded.

Then we connect again with the user然后我们再次与用户连接

sqlplus test6/Oracle_1234

SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 7 14:11:22 2021
Version 19.6.0.0.0

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

Last Successful login time: Thu Oct 07 2021 14:11:03 +02:00

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

SQL> alter session set sql_trace=true ;

Session altered.

UPDATE更新

If you are using MULTITENANT, you need to create the user in the right pluggable database.如果您正在使用 MULTITENANT,则需要在正确的可插入数据库中创建用户。

CONN / AS SYSDBA -- you connect to the container root
-- Switch container while connected to a common user.
ALTER SESSION SET CONTAINER = yourpdb;
-- Create the local user using the CONTAINER clause.
CREATE USER xxxx IDENTIFIED BY xxxxx CONTAINER=CURRENT;
GRANT CREATE SESSION TO xxxx CONTAINER=CURRENT;

You can connect directly to the pdb ( pluggable database )您可以直接连接到 pdb(可插拔数据库)

CONN system/password@pdb1

-- Create the local user using the default CONTAINER setting.

CREATE USER xxxxxxxx IDENTIFIED BY xxxxxxxxxx;
GRANT CREATE SESSION TO xxxxxx;

Then, connect with the user to the pluggable database然后,将用户连接到可插拔数据库

conn xxxx/password@pdb1 

alter session set optimizer_dynamic_sampling=0 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM