如何在 Oracle 中限制每个会话的数据库资源?

oraclesoftware & codingprogramming

问题:

您想限制用户在数据库中可以使用的资源量。

解决方案

要限制资源,我们可以按照以下步骤操作。

我们可以使用以下 SQL 语句查看数据库中 RESOURCE_LIMIT 的当前设置。

select name, value from v$parameter where name='resource_limit';

创建一个配置文件来限制资源,并将其分配给用户。但它不会限制 CPU 利用率。

示例

CREATE PROFILE test_profile LIMIT    SESSIONS_PER_USER          2    CPU_PER_SESSION            UNLIMITED    CPU_PER_CALL               300000    CONNECT_TIME               45    IDLE_TIME                  15    LOGICAL_READS_PER_SESSION  DEFAULT    LOGICAL_READS_PER_CALL     1000    PRIVATE_SGA                15K    FAILED_LOGIN_ATTEMPTS      5    PASSWORD_LIFE_TIME         60    PASSWORD_GRACE_TIME        10    PASSWORD_LOCK_TIME         1    PASSWORD_REUSE_TIME        10    PASSWORD_REUSE_MAX         1 ;

输出

SESSIONS_PER_USER   -- Specify the number of concurrent sessions to which you want to limit the user. CPU_PER_SESSION     -- Specify the CPU time limit for a session, expressed in hundredth of seconds. CPU_PER_CALL        -- Specify the CPU time limit for a call (a parse, execute, or fetch), expressed in hundredths of seconds. Need to increase this or not required to mention CONNECT_TIME        -- Specify the total elapsed time limit for a session, expressed in minutes. IDLE_TIME           -- Specify the permitted periods of continuous inactive time during a session, expressed in minutes. Long-running queries and other operations are not subject to this limit. LOGICAL_READS_PER_SESSION   -- Specify the permitted number of data blocks read in a session, including blocks read from memory and disk. PRIVATE_SGA                 -- Specify the amount of private space a session can allocate in the shared pool of the system global area (SGA). Please refer to size_clause for information on that clause. FAILED_LOGIN_ATTEMPTS       -- Specify the number of failed attempts to log in to the user account before the account is locked PASSWORD_LIFE_TIME          -- Specify the number of days the same password can be used for authentication. PASSWORD_GRACE_TIME         -- Specify the number of days after the grace period begins during which a warning is issued and login is allowed. If the password is not changed during the grace period, the password expires.   PASSWORD_LOCK_TIME          -- Specify the number of days an account will be locked after the specified number of consecutive failed login attempts.   PASSWORD_REUSE_TIME         --specifies the number of days before which a password cannot be reused. PASSWORD_REUSE_MAX          --specifies the number of password changes required before the current password can be reused

执行时,test_profile 应该已经创建。

创建配置文件后,我们现在可以将其分配给用户。在下一个示例中,用户 test 被分配了 test_profile:

alter user test profile test_profile;

Oracle 数据库配置文件有几个用途:设置资源限制和强制密码安全设置。

创建用户时,如果未指定配置文件,则会将 DEFAULT 配置文件分配给新创建的用户。我们可以使用 ALTER PROFILE 语句修改配置文件。我们可以覆盖 DEFAULT 配置文件,将 CPU_PER_SESSION 限制为 360000(以百分之一秒为单位)。

alter profile default limit cpu_per_session 360000;

配置文件也用于强制执行密码安全设置。例如,假设您想修改 DEFAULT 配置文件,限制密码的最大使用天数。下一行代码将 DEFAULT 配置文件的 PASSWORD_LIFE_TIME 设置为 90 天。

alter profile default limit password_life_time 90;

PASSWORD_REUSE_TIME 和 PASSWORD_REUSE_MAX 设置必须结合使用。如果您为一个参数指定一个整数,然后为另一个参数指定 UNLIMITED,则当前密码永远不能重复使用。

如果您想指定 DEFAULT 配置文件密码必须在 100 天内更改 10 次才能重复使用,请使用类似如下的代码行:

alter profile default limit password_reuse_time 100 password_reuse_max 10;


相关文章