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