To view the records of user logins for an Oracle database

Source: Internet
Author: User

Record user logon information in Oracle

We can use the Oracle Audit function to record user login information, but if the Audit function is opened it will degrade Oracle performance and even cause Oracle to crash. So how can we record user login information? We can actually do this by creating a trigger. The method is as follows:
1. Log in to Oracle with SYS user
2. Create a table that records user logon information
CREATE TABLE log$information
(
ID Number (10),
USERNAME VARCHAR2 (30),
Logintime DATE,
TERMINAL VARCHAR2 (50),
Ipadress VARCHAR2 (20),
Osuser VARCHAR2 (30),
MACHINE VARCHAR2 (64),
Program VARCHAR2 (64),
SID number,
serial# number,
Ausid number
)
/

3. Create a Sequence as the primary key for the login information
CREATE SEQUENCE Login_seq
MinValue 1
MaxValue 9999999999
Start with 1
Increment by 1
Cache 20
/

4. Create triggers to record user logon information
CREATE OR REPLACE TRIGGER login_record_tr
After logon on DATABASE
DECLARE
Mtsession V$session%rowtype;
CURSOR CSession (Iiquerysid in number) is
SELECT * from V$session
WHERE audsid = Iiquerysid;
BEGIN
OPEN CSession (Userenv (' SESSIONID '));
FETCH csession into mtsession;
IF Csession%found and Sys_context (' USERENV ', ' ip_address ') is not NULL THEN
INSERT into Log$information (
Id
Username
Logintime,
Terminal
Ipadress,
Osuser,
Machine
Program
Sid
serial#,
Ausid
) VALUES (
Login_seq.nextval,
USER,
Sysdate,
Mtsession.terminal,
Sys_context (' USERENV ', ' ip_address '),
Mtsession.osuser,
Mtsession.machine,
Mtsession.program,
Mtsession.sid,
mtsession.serial#,
Userenv (' SESSIONID ')
);
End IF;
Close CSession;
EXCEPTION
When others THEN
RAISE;
End;
/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.