如何在 Oracle 中限制每個會話的資料庫資源?
問題
您希望限制使用者在資料庫中可以消耗的資源量。
解決方案
要限制資源,我們可以按照以下步驟操作。
我們可以使用以下 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;