Lab 2 - Quản lý người dùng trong Oracle

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

TRƯỜNG ĐẠI HỌC NGUYỄN TẤT THÀNH.

KHOA CÔNG NGHỆ THÔNG TIN.


MÔN HỌC: CSDL ĐA PHƯƠNG TIỆN

LAB 2: CƠ BẢN VỀ QUẢN LÝ NGƯỜI DÙNG


TRONG CSDL ORACLE 12c
(***)
❖ MÔ TẢ BÀI LAB:
- Tìm hiểu cách thức quản lý người dùng trong CSDL Oracle 12c
- Giải quyết các bài tập cụ thể tạo User trong CSDL Oracle 12c

❖ NỘI DUNG LÝ THUYẾT


Tìm hiểu về những nội dung trong Oracle 12c
✓ Tablespace
✓ Schema
✓ User
✓ Resource (tài nguyên)
✓ Profile

I – QUẢN LÝ USER
1. Tablespace
- Một CSDL Oracle được chia thành những đơn vị lưu trữ luận lý được gọi là
các tablespace, nhằm mục đích gom nhóm các cấu trúc luận lý có liên quan
với nhau.
- Mỗi CSDL có 1 hoặc nhiều các tablespace. Mỗi tablespace chứa 1 hoặc nhiều
các datafile. Datafile là các cấu trúc vật lý tương thích với hệ điều hành bên
dưới, dùng để lưu trữ dữ liệu của các cấu trúc luận lý trong tablespace chứa
nó. Kích thước tổng cộng của các datafile chính là dung tích lưu trữ tổng cộng
của tablespace đó.

MH: CSDL đa phương tiện -1- GV: Phạm Đình Tài


Một CSDL có 2 tablespace SYSTEM và USERS. Tablespace SYSTEM chứa 2 datafile
là DATA1.ORA và DATA2.ORA

Một tablespace chứa 2 datafile.


Bên trong các datafile là các đối tượng, như là table và index. Các đối tượng trong
tablespace có thể nằm trên vài datafile.

MH: CSDL đa phương tiện -2- GV: Phạm Đình Tài


1.1. Có 2 loại tablespace:
a) System tablespace:
- Mọi CSDL Oracle đều có 2 system tablespace là SYSTEM và SYSAUX,
được tạo ra một cách tự động.
- Chứa thông tin về các data dictionary views, các định nghĩa của
stored procedures, packages, và các database triggers dưới dạng PL/SQL
program units, SYSTEM rollback segment, …
- Không nên chứa dữ liệu người dùng trong tablespace này mặc dù có
thể.
b) Non-system tablespace:
- Dùng để chứa các loại dữ liệu còn lại, đặc biệt là các dữ liệu của
người dùng.
1.2. Một cách phân loại khác của tablespace:
- Temporary tablespace: được sử dụng để dành riêng cho các thao tác
sắp xếp dữ liệu.
- Permanent tablespace: Các tablespaces không phải là temporary
tablespaces được gọi là các permanent tablespaces. Các permanent
tablespace được sử
2. Schema
✓ Schema là một tập hợp các đối tượng cơ sở dữ liệu (vd: table, view,
index,...).
✓ Mỗi schema được sở hữu bởi một user và có cùng tên với user.
✓ Không có mối quan hệ nào giữa schema và tablespace. Các đối tượng
thuộc 1 schema có thể nằm trên các tablespace khác nhau và 1
tablespace có thể chứa các đối tượng thuộc nhiều schema khác nhau.

❖ NỘI DUNG THỰC HÀNH


1. Tạo mới User
a. Tạo 1 user mới với câu lệnh sau:
CREATE USER nttu IDENTIFIED BY nttu1;

MH: CSDL đa phương tiện -3- GV: Phạm Đình Tài


Ghi chú:
Nếu gặp phải lỗi sau: ORA-65096: invalid common user or role name, là do
Oracle yêu cầu phải có tiền tố c## ở đầu username.
Có 2 cách để xử lý vấn đề này
Cách 1: Tạo user name có tên c##nttu
Cách 2: Các bạn thực hiện command bên dưới trước, sau đó thực hiện lại
câu lệnh SQL CREATE USER:
#SQL> ALTER SESSION SET "_ORACLE_SCRIPT"=true;

Khi tạo mới 1 user, ta có thể quy định về default tablespace, temporary
tablespace, quota trên các tablespace, thời hạn hiệu lực của password,… ngay
trong câu lệnh tạo user hoặc sẽ chỉ định cụ thể sau này.

b. Hiển thị tablespace của user vừa mới tạo:

Tuy trong câu lệnh tạo user ta không chỉ định default tablespace và
temporary tablespace, Oracle đã tự gán các giá trị mặc định cho user này.
c. Hiển thị tablespace của user vừa mới tạo:

Log out ra khỏi user hiện tại và log in bằng user vừa mới tạo, sẽ nhận được
thông báo:

MH: CSDL đa phương tiện -4- GV: Phạm Đình Tài


Lý do: User vừa mới tạo chưa được cấp quyền CREATE SESSION, là quyền cho
phép 1 user kết nối đến database. Để user vừa mới tạo có thể login được, ta phải cấp quyền
CREATE SESSION.

Đăng nhập lại user ban đầu và cấp quyền cho user nttu như sau:

d. Khi user mới được tạo ra, nếu ta không cấp các quyền tạo các loại object
(table, index,…) thì user đó không thể tạo được các object. Tùy thuộc vào nhu cầu của
từng user, ta chỉ nên cấp những quyền cần thiết chứ không nên cấp dư.
Một điều kiện bắt buộc khác để user có thể tạo được các object là ta phải cấp quota
cho user trên các tablespace tương ứng. Một user có thể được cấp quota sử dụng trên 1
hoặc nhiều tablespace. Quota có thể limited hoặc unlimited.
Ví dụ: Cho thấy khi user nttu tạo mới 1 bảng sẽ hiển thị thông báo lỗi:

MH: CSDL đa phương tiện -5- GV: Phạm Đình Tài


Để khắc phục lỗi trên, log in lại vào user quản trị của mình và thực hiện các
câu lệnh sau:

Nếu muốn user có thể sử dụng tối đa 1 tablespace nào đó thì dùng cú pháp
sau:

e. Nếu muốn user có thể tạo object trên bất kỳ tablespace nào thì dùng cú pháp
sau:

MH: CSDL đa phương tiện -6- GV: Phạm Đình Tài


2. Thay đổi các đặc tính của user
a. Thay đổi password:

ALTER USER nttu IDENTIFIED BY nttu_passdathaydoi;

Hoặc

ALTER USER nttu IDENTIFIED BY nttu1 REPLACE


nttu_passdathaydoi;

b. Password expiration:
Ta có thể làm cho 1 password hết hạn bằng 2 cách:

ALTER USER nttu IDENTIFIED BY nttu1 PASSWORD EXPIRE;


User altered.

Hoặc

ALTER USER nttu PASSWORD EXPIRE;


User altered.

*Ta cũng có thể bắt buộc password expire ngay khi tạo mới một user:

CREATE USER nttu IDENTIFIED BY nttu1 PASSWORD EXPIRE;

MH: CSDL đa phương tiện -7- GV: Phạm Đình Tài


* Sau khi làm expire password các user trên, hãy log in vào các user nttu và tự
rút ra nhận xét.

c. Trạng thái account:


Ta có thể thay đổi trạng thái tài khoản (lock/unlock) của một use để cho
phép/không cho phép user đó truy xuất vào CSDL.

ALTER USER nttu ACCOUNT LOCK;


ALTER USER nttu_user2 ACCOUNT UNLOCK;

Xem trạng thái tài khoản (Account Status) của tất cả các user:

SELECT username, account_status FROM dba_users;

3. Xóa User
DROP USER nttu;
User Dropped.

MH: CSDL đa phương tiện -8- GV: Phạm Đình Tài


Lưu ý: Lệnh drop user không chỉ xóa user mà còn xóa tất cả object thuộc về user
đó. Khi user đã có object thì phải dùng thêm tùy chọn CASCADE:

DROP USER nttu CASCADE;


User Dropped.

II – User Profile

A. Lý thuyết
1. Profile
▪ Một profile là một tập hợp có tên của các giới hạn tài nguyên, được gán cho một
hay nhiều user trong CSDL Oracle.
▪ Profile cung cấp một cách quản lý dễ dàng việc giới hạn tài nguyên. Nó giúp giới
hạn việc sử dụng quá mức các tài nguyên của toàn hệ thống. Profile cũng là cách
để quản lý các chính sách về password.
▪ Trong một CSDL có thể tạo nhiều profile. Một profile mặc định sẽ được dùng để
gán cho những user không được gán profile một cách tường minh.
▪ Lưu ý rằng các giá trị mặc định đều được thiết lập là “unlimited”.
2. Các loại tài nguyên
▪ Một profile có thể mô tả các loại giới hạn tài nguyên sau:
✓ Số lượng các session đồng thời mà user có thể thực hiện.
✓ Thời gian xử lý CPU cho một session của user đó hoặc cho một cuộc gọi (call)
tới Oracle bởi 1 câu lệnh SQL.
✓ Số lần đọc luận lý I/O cho một session của user đó hoặc cho một cuộc gọi (call)
tới Oracle bởi 1 câu lệnh SQL.
✓ Lượng thời gian nhàn rỗi cho session của user.
✓ Lượng thời gian connect cho một session.
✓ Các quy định về password (số lần cố gắng login thất bại, thời gian hiệu lực của1
password , …)

A. Thực hành
1. Tạo mới Profile
a. Trước hết, để hệ thống có thể thi hành việc ràng buộc các giới hạn tài nguyên,
ta cần enable tham số hệ thống RESOURCE_LIMIT bằng câu lệnh sau:

ALTER SYSTEM SET RESOURCE_LIMIT = TRUE;

MH: CSDL đa phương tiện -9- GV: Phạm Đình Tài


Tham số RESOURCE_LIMIT có giá trị mặc định ban đầu là FALSE

b. Tạo mới 1 profile bằng câu lệnh sau:

CREATE PROFILE app_user LIMIT FAILED_LOGIN_ATTEMPTS 3


SESSIONS_PER_USER UNLIMITED CPU_PER_SESSION UNLIMITED
CPU_PER_CALL 3000 CONNECT_TIME 45 IDLE_TIME 60
LOGICAL_READS_PER_SESSION DEFAULT
LOGICAL_READS_PER_CALL 1000;

Lưu ý: Để tạo PROFILE, cần có quyền CREATE PROFILE.

2. Gán Profile
a. Có thể gán profile khi vừa tạo user:
CREATE USER nttu2 IDENTIFIED BY nttu1 PROFILE app_user;

User created.

b. Khi tạo mới user, nếu không gán tường minh thì user sẽ được gán
profile mặc định như sau:

CREATE USER nttu2 IDENTIFIED BY nttu1 PROFILE app_user;


SELECT profile FROM dba_users WHERE username = 'NTTU2';

MH: CSDL đa phương tiện - 10 - GV: Phạm Đình Tài


c. Để xem thông tin về profile mặc định:

SELECT DISTINCT resource_name, limit FROM dba_profiles


WHERE profile='DEFAULT';

d. Gán profile cho 1 user:

ALTER USER nttu1 PROFILE app_user;

e. Thay đổi profile

ALTER PROFILE app_user LIMIT SESSIONS_PER_USER 4


FAILED_LOGIN_ATTEMPTS 4;

MH: CSDL đa phương tiện - 11 - GV: Phạm Đình Tài


f. Xóa profile
DROP PROFILE app_user CASCADE;

III – BÀI TẬP

1. Tạo một profile “Password” thỏa mãn:

a) Thời hạn sử dụng là 60 ngày.

b) Số ngày mà sau đó password mới được sử dụng lại là 1 ngày.

c)Số ngày mà sau đó password mới được sử dụng lại là 1 ngày.

d) Số lần thay đổi password trước khi được sử lại password cũ là 5 lần.

e) Số lần nhập sai password là 3.

2. Kiểm tra profile vừa tạo:

a) Tạo mới user John với password p123.

b) Gán profile “Password” vừa tạo cho user này.

MH: CSDL đa phương tiện - 12 - GV: Phạm Đình Tài


HƯỚNG DẪN:

===================================================
CREATE PROFILE profile
LIMIT { resource_parameters
| password_parameters
}
[ resource_parameters
| password_parameters
]... ;
===================================================

MH: CSDL đa phương tiện - 13 - GV: Phạm Đình Tài


TRONG ĐÓ:

resource_parameters

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.

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.

LOGICAL_READS_PER_CALL
Specify the permitted number of data blocks read for a call to process a SQL statement (a parse, execute,
or fetch).

MH: CSDL đa phương tiện - 14 - GV: Phạm Đình Tài


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.

password_parameters

Use the following clauses to set password parameters. Parameters that set lengths of time are interpreted
in number of days. For testing purposes you can specify minutes (n/1440) or even seconds (n/86400).

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. If you also set a value for
PASSWORD_GRACE_TIME, the password expires if it is not changed within the grace period, and
further connections are rejected. If you do not set a value for PASSWORD_GRACE_TIME, its default
of UNLIMITED will cause the database to issue a warning but let the user continue to connect
indefinitely.

PASSWORD_REUSE_TIME and PASSWORD_REUSE_MAX


These two parameters must be set in conjunction with each other. 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. For these
parameter to have any effect, you must specify an integer for both of them.

*If you specify an integer for both of these parameters, then the user cannot reuse a password until the
password has been changed the password the number of times specified for
PASSWORD_REUSE_MAX during the number of days specified for PASSWORD_REUSE_TIME.

*For example, if you specify PASSWORD_REUSE_TIME to 30 and PASSWORD_REUSE_MAX


to 10, then the user can reuse the password after 30 days if the password has already been changed 10
times.

*If you specify an integer for either of these parameters and specify UNLIMITED for the other, then
the user can never reuse a password.

* If you specify DEFAULT for either parameter, then Oracle Database uses the value defined in the
DEFAULT profile. By default, all parameters are set to UNLIMITED in the DEFAULT profile. If you
have not changed the default setting of UNLIMITED in the DEFAULT profile, then the database treats
the value for that parameter as UNLIMITED

*If you set both of these parameters to UNLIMITED, then the database ignores both of them.

MH: CSDL đa phương tiện - 15 - GV: Phạm Đình Tài


PASSWORD_LOCK_TIME
Specify the number of days an account will be locked after the specified number of consecutive failed
login attempts.

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_VERIFY_FUNCTION
The PASSWORD_VERIFY_FUNCTION clause lets a PL/SQL password complexity verification script
be passed as an argument to the CREATE PROFILE statement. Oracle Database provides a default
script, but you can create your own routine or use third-party software instead.

*For function, specify the name of the password complexity verification routine.

*Specify NULL to indicate that no password verification is performed.

If you specify expr for any of the password parameters, the expression can be of any form except scalar
subquery expression.

https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6010.htm#i2066025

MH: CSDL đa phương tiện - 16 - GV: Phạm Đình Tài

You might also like