MySQL to user operation Plus audit function------Junior Edition

Source: Internet
Author: User

In some applications, you need to know who is working on the table, what has been done, and what is responsible for the pursuit. In MySQL, you can use a trigger implementation.

1: Create a test table

Mysql> CREATE Table A (a int);
Query OK, 0 rows affected (0.01 sec)

2: Create a Tracking table containing table name, operation type, operating time, operator, operating machine IP address

Mysql> CREATE TABLE Trace (tbname varchar (), dml_type varchar (), dml_time datetime,dml_user varchar (in), DML_IP varchar (30));
Query OK, 0 rows affected (0.01 sec)

3: Create a trigger in a table

Create trigger insert_a after insert on A1
For each row
INSERT into trace values (' A1 ', ' INSERT ', now (), Substring_index (Current_User (), ' @ ', 1), Substring_index (Current_User () , ' @ ',-1));

4: Inserting test data

mysql> INSERT INTO A values (1);
Query OK, 1 row Affected (0.00 sec)

Mysql> select * from Trace;
+--------+----------+---------------------+----------+-----------+
| Tbname | Dml_type | Dml_time | Dml_user | Dml_ip |
+--------+----------+---------------------+----------+-----------+
| A | INSERT | 2010-11-29 17:58:12 | Root | localhost |
+--------+----------+---------------------+----------+-----------+
1 row in Set (0.00 sec)

Mysql> select * from Trace;
+--------+----------+---------------------+----------+--------------+
| Tbname | Dml_type | Dml_time | Dml_user | Dml_ip |
+--------+----------+---------------------+----------+--------------+
| A | INSERT | 2010-11-29 17:58:12 | Root | localhost |
| A | INSERT | 2010-11-29 17:58:38 | CPC | 192.168.0.30 |
+--------+----------+---------------------+----------+--------------+
2 rows in Set (0.00 sec)

Mysql>

Thus, when 0.30 of the machine is logged in as a CPC user, an insert operation is made to the A, which is recorded in detail in this table.
The user who manages the database, query this table, can trace to the responsible person.

MySQL to user operation Plus audit function------Junior Edition

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.